This is my category & products SQL/PHP schema.
I need to echo
the <hr>
for each of my categories and not for each products . This is my code :
$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/>";
}
thanks and sorry for my poor English...
You can write the following due to the fact that your SQL statement is sorted by the categories:
$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/>";
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.