gpt4 book ai didi

php - 类别 - 产品 [PHP、MySQL PDO]

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

这是我的类别和产品 SQL/PHP 架构。

我需要echo <hr>对于我的每个类别,而不是对于每个产品。这是我的代码:

$sql = $db->query("SELECT * from cat,products where cat.cat_id = products.cat_id order by products.cat_id desc");
while ($row = $sql->fetch(PDO::FETCH_ASSOC)){
$ID = $row["id"];
$Title = $row["title"];
$CatID = $row["cat_id"];

$row["cat_id"] = $row["cat_name"];
switch ($row["cat_id"]){
case $row["cat_id"] == $row["cat_name"]:
echo "<hr>";
break;
default:

break;
}

echo $row["cat_id"];
echo "<a href='test.php?cat=$CatID&prod=$ID'>$Title</a><br/>";
}

图片: enter image description here

感谢并为我糟糕的英语感到抱歉...

最佳答案

由于 SQL 语句是按类别排序的,因此您可以编写以下内容:

        $tmp_category = '';    

$sql = $db->query("SELECT * from cat,products where cat.cat_id = products.cat_id order by products.cat_id desc");
while ($row = $sql->fetch(PDO::FETCH_ASSOC)){


$ID = $row["id"];
$Title = $row["title"];
$CatID = $row["cat_id"];

if ($row["cat_id"] <> $tmp_category){ //you can use != instead of <> as well

$tmp_category = $row["cat_id"];
echo "<hr>";
echo $row["cat_id"];
echo "<a href='test.php?cat=$CatID&prod=$ID'>$Title</a><br/>";

}
else
{

echo $row["cat_id"]; // this is optional, you can delete the cat ID from here and it will show you only the above link.
echo "<a href='test.php?cat=$CatID&prod=$ID'>$Title</a><br/>";
}


}

关于php - 类别 - 产品 [PHP、MySQL PDO],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36116397/

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