0

我按类别和子类别将产品分开。通过查询和 while 循环,我能够正确地回显列表中的所有类别。

$query = mysql_query("SELECT * FROM category WHERE `enabled`='1'");

while ($query_row = mysql_fetch_assoc($query)) {
echo '$query_row[catname]';
}

我想要完成的是在各自的类别名称下列出子类别名称:

$query = mysql_query("SELECT * FROM category WHERE `enabled`='1'");

while ($query_row = mysql_fetch_assoc($query)) {
echo '<h1>'.$query_row["catname"].'</h1>';

     $sql = mysql_query("SELECT * FROM subcategory WHERE `catid`='$query_row[catid]'");

     while ($row = mysql_fetch_assoc($sql)) {
     echo '$row["subcatname"]';
     }
}

我得到的是每个类别的列表(这很棒) - 但在它下面是每个子类别的列表。我只需要与他们自己的类别一起使用的子类别。我应该在某处使用 for 循环吗?什么是最好的解决方案?

最终结果应该是:

CARS
mazda
chevy
toyota

BIKES
yamaha
harley
suzuki
honda

BATTERIES
duracel
energizer

就像现在一样。我越来越

CARS
mazda
chevy
toyota
yamaha
harley
suzuki
honda
duracel
energizer

BIKES
mazda
chevy
toyota
yamaha
harley
suzuki
honda
duracel
energizer

BATTERIES
mazda
chevy
toyota
yamaha
harley
suzuki
honda
duracel
energizer
4

2 回答 2

0

尝试改变

$sql = mysql_query("SELECT * FROM subcategory WHERE catid='$query_row[catid]'");

$sql = mysql_query("SELECT * FROM subcategory WHERE catid='" . $query_row["catid"] . "'");

于 2013-10-24T15:09:40.633 回答
0
$query = mysql_query("SELECT * FROM category WHERE `enabled`='1'");

while ($query_row = mysql_fetch_assoc($query)) {
echo '<h1>'.$query_row["catname"].'</h1>';

     $sql1 = mysql_query("SELECT catid FROM subcategory WHERE `catname`='$query_row[catname]'");
     while($row1 = mysql_fetch_assoc($sql1)){

     $sql = mysql_query("SELECT * FROM subcategory WHERE `catid`='row1[catid]'");

     while ($row = mysql_fetch_assoc($sql)) {
     echo '$row["subcatname"]';
     }
     }
     } 

试试这个。

于 2013-10-24T15:39:01.980 回答