多个表显示php MySQL

时间:2017-02-11 18:43:26

标签: php mysql phpmyadmin

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

问题1:我现在遇到的问题是,当我只点击其中一个专辑时,将显示来自不同专辑的所有图像。

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

t1.recordid = t2.categoryrecordid

t2.productrecordid = t3.productid

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

产品: enter image description here

Productimage:

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>

1 个答案:

答案 0 :(得分:1)

所以有一个类似的代码示例可以帮助您理解。 但这是Mysqli Object oriented,因为我自己在学习PDO。但我相信这会让人更好地理解。

这里我使用面向PHP对象的Mysqli编写了Statement

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

的表

albums table

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

的表

productimg table

3)INDEX PAGE: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>