gpt4 book ai didi

php - php MySQL多表显示

转载 作者:行者123 更新时间:2023-11-29 11:01:21 25 4
gpt4 key购买 nike

我正在构建一个包含许多专辑的网页。当我单击其中一张专辑时,它将显示图像。

问题 1:我现在遇到的问题是,当我仅单击其中一个相册时,将显示来自不同相册的所有图像。

问题2:在点击相册后的php文件中。我只想显示一种产品的一张图片,但我的代码似乎无法正常工作

t1.recordid = t2.categoryrecordid

t2.productrecordid = t3.productid

我的MySQL表设计的结构:类别: enter image description here

产品: enter image description here

产品图片:

enter image description here

我的专辑代码:

            <div class="row">
<?php
$stmt = $DB_con->prepare('SELECT recordid, catcode,title,imgfile,catdesc FROM category ORDER BY recordid DESC');
$stmt->execute();

if ($stmt->rowCount() > 0) {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
?>
<div class="col-xs-3">

<p><img src="./images/<?php echo $row['catcode']?>/<?php echo $row['imgfile']; ?>" class="img-rounded" width="190px" height="160px" /></p>
<p><a class="page-header" href="collectionGallery.php?cat= <?php echo $row['catcode']; ?>"><?php echo $row['title']; ?></a></p> <br/>
</div>
<?php
}
} else {
?>
<div class="col-xs-12">
<div class="alert alert-warning">
<span class="glyphicon glyphicon-info-sign"></span> &nbsp; No Data Found ...
</div>
</div>
<?php
}
?>
</div>
</div>

点击相册后的代码(显示该相册的图像):

       <div class="row">
<?php
$stmt = $DB_con->prepare('SELECT category.*, product.*, productimage.* FROM category JOIN product ON product.categoryrecordid=category.recordid JOIN productimage ON productimage.productid=product.productrecordid');
$stmt->execute();

if ($stmt->rowCount() > 0) {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
?>
<div class="col-xs-3">
<p><img src="./images/<?php echo $row['catcode'].'/'. $row['imagefilename']; ?>" class="img-rounded" width="190px" height="160px" /></p>
<p><?php echo $row['productcode'].' Price:'.$row['price']; ?></a></p>
</div>
<?php
}
} else {
?>
<div class="col-xs-12">
<div class="alert alert-warning">
<span class="glyphicon glyphicon-info-sign"></span> &nbsp; No Data Found ...
</div>
</div>
<?php
}
?>
</div>

最佳答案

所以有一个类似的代码示例可以帮助您理解。但这是在Mysqli面向对象中,因为我自己正在学习PDO。但我相信这会给人很好的理解。

这里我使用 PHP 面向对象和 Mysqli 准备好的语句

1)在数据库中创建一个表,名称为:albums

albums table

2)在数据库中创建一个表,名称为:productimg

productimg table

3) 索引页:index.php

    <?php
include('products.php');
$newprod = new products();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP MYSQL SHOW ALBUMS</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<ul class="breadcrumb" style="width:100px;">
<li><a href="#">d</a></li>
<li><a href="#">d</a></li>
<li><a href="#">d</a></li>
</ul>
<div class="row">
<?php $newprod->showAlbums(); ?>
</div>
</div>
</body>
</html>

4)此文件显示专辑和产品图片:products.php

 <?php
class products{
private $link;

function __construct(){
$this->link = new mysqli('localhost','root','admin','codexworld');
if(mysqli_connect_errno()){
die("connection failed".mysqli_connect_errno());
}
}

function showAlbums(){
$sql = $this->link->stmt_init();
if($sql->prepare("SELECT pname,album_name,product_code FROM albums")){
$sql->bind_result($pname,$albumname,$pcode);
$sql->execute();
while($sql->fetch()){
?>
<div class="col-md-4">
<a href="displproduct.php?pcode=<?php echo $pcode;?>"><img src="albumimages/<?php echo $albumname;?>" alt="<?php echo $pname; ?>" class="" style="width:200px;height:200px;">
<h4>ALBUM :<strong><?php echo $pname;?></strong></h4></a>
</div>
<?php
}
}
}

function showproducts($productcode){
$sql = $this->link->stmt_init();
if($sql->prepare("SELECT productname,productid,image FROM productimg WHERE productid = ?")){
$sql->bind_param('s',$productcode);
$sql->bind_result($pname,$pid,$img);
$sql->execute();
while($sql->fetch()){
?>
<div class="col-md-4">
<img src="productimg/<?php echo $img;?>" alt="<?php echo $pname; ?>" class="" style="width:200px;height:200px;">
<h4>Product Image :<strong><?php echo $pname;?></strong></h4>
</div>
<?php
}
}
}
}
?>

5)此文件显示产品:displproduct.php

 <?php
include('products.php');
$newprod = new products();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP MYSQL SHOW ALBUMS</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<?php if(isset($_GET['pcode'])){
$productcode = $_GET['pcode'];
$newprod->showproducts($productcode);
}
?>
</div>
</div>
</body>
</html>

关于php - php MySQL多表显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42179778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com