1

我有两张桌子:

category
id | name

products
id | cat_id | name..

就像标题一样,我想创建一个类别菜单,当我点击一个类别时,它将显示该类别中的所有产品。我尝试了几种方法,但都不行!这是我的 menu.php

    try { 

    $dbh = new PDO("mysql:host=$hostname;dbname=...", $username, $password);


    $sql = "select * from category";
    echo "<table>";
    echo "<tr><th>ID</th><th>Name</th></tr>";
    foreach ($dbh->query($sql) as $row)
        {
        $c_id=$row['c_id'];
        echo "<tr>";
        echo "<td>" . $row['c_id'] ."</td>";
        echo "<td><a href='deal_list.php?cat_id=$c_id'>" . $row['c_name'] . "</td>";



        echo "</tr>";
        }
    echo "</table>";

        $dbh = null;
    }
    catch(PDOException $e) { echo $e->getMessage(); }

?>

这是我的产品清单

try { 
    $cat_id = $_GET['cat_id'];
    $dbh = new PDO("mysql:host=$hostname;dbname=..", $username, $password);

    $sql = "select id,name,description,price,groupbuy_price, CEIL((groupbuy_price/price)*100) AS saving,current_buyer,maximum_buyer,expired_time,status,sum_img,cat_id,c_name from groupbuy,category where c_id=cat_id";
    echo "<table border=2px>";
    echo "<tr><th>Name</th><th>Description</th><th>Price</th><th>Group buy price</th><th>Saving</th><th>Number of current buyer</th><th>Maximum Buyer</th><th>Expired Time</th><th>Status</th><th>Category</th><th>Sumary image</th></tr>";
    foreach ($dbh->query($sql) as $row)
        {
        $id=$row['id'];
        echo "<tr>";
        echo "<td>" . $row['name'] . "</td>";
        echo "<td>" . $row['description'] ."</td>";
        echo "<td>" . $row['price'] . "</td>";
        echo "<td>" . $row['groupbuy_price'] ."</td>";
        echo "<td>" . $row['saving'] .'%' ."</td>";
        echo "<td>" . $row['current_buyer'] ."</td>";
        echo "<td>" . $row['maximum_buyer'] ."</td>";   
        echo "<td>" . $row['expired_time'] ."</td>";
        echo "<td>" . $row['status'] ."</td>";
        echo "<td>" . $row['c_name'] . "</td>";



?>
        <td><img src="<?php echo $row['sum_img']?>" width="50px" /></td>
<?php
        echo "<td><a href='delete_deal.php?id=$id'>Delete</a></td>"; 
        echo "<td><a href='deal_detail.php?id=$id'>Detail</a></td>"; 
        echo "<td><a href='edit_deal.php?id=$id'>Edit</a></td>"; 


        echo "</tr>";
        }

    echo "</table>";

        $dbh = null;
    }
    catch(PDOException $e) { echo $e->getMessage(); }

?>
4

1 回答 1

0

看起来你只需要更换

$sql = "select id,name,description,price,groupbuy_price, CEIL((groupbuy_price/price)*100) AS saving,current_buyer,maximum_buyer,expired_time,status,sum_img,cat_id,c_name from groupbuy,category where c_id=cat_id";

$sql = "select id,name,description,price,groupbuy_price, CEIL((groupbuy_price/price)*100) AS saving,current_buyer,maximum_buyer,expired_time,status,sum_img,cat_id,c_name from groupbuy,category where c_id={$cat_id}";

但出于安全原因,开始时也会发生变化

$cat_id = (int) $_GET['cat_id'];
于 2012-11-27T09:28:31.130 回答