mySQL查询中只有一个选项没有显示

时间:2012-11-03 11:58:55

标签: php mysql

我无法在http://www.mattmaclennan.co.uk/a2的“新摩托车”部分显示所有子菜单项。例如,冒险应该有三个选项,而不是一个。这是我用过的代码。

<?
$output = mysqli_query("SELECT * FROM bikes, bikeTypes WHERE bikes.model_id = bikeTypes.model_id GROUP BY bikeTypes.model_id");
while($row = mysqli_fetch_array($output))
 {
   echo "<li><a href='test.php?id=" . $row['model_id'] . "'>".$row['model'].'</a><ul>';
   echo "<li><a href='details.php?id=" . $row['bike_id'] . "'>" . $row['bikeName'] . "</a></li>";
   echo '</ul>';   
   echo '</li>';
 }
//var_dump($result);
?>

任何想法的家伙?谢谢!

编辑:以下是来自phpMyAdmin的两个表的结构:

bikeTypes表: bike types table http://www.mattmaclennan.co.uk/a2/images/bikeTypesTable.jpg

自行车表: bikes table http://www.mattmaclennan.co.uk/a2/images/bikesTable.jpg

1 个答案:

答案 0 :(得分:2)

试试这个:

  

SELECT bikeTypes.id,bikeTypes.name,bikes.name,bikes.id FROM bikeTypes LEFT JOIN bikes ON   bikes.model_id = bikeTypes.model_id

如果您看到我写的查询中包含LEFT JOIN。您正在做什么被数据库引擎视为INNER JOIN

但你需要的是

 $output = mysqli_query("SELECT * FROM bikeTypes");
    while($row = mysqli_fetch_array($output))
     {

       echo "<li><a href='test.php?id=" . $row['model_id'] . "'>".$row['model'].'</a><ul>';
       $subcat = mysqli_query("SELECT * FROM bikes WHERE model_id = $row['model_id']");

       while($rowSub = mysqli_fetch_array($subcat)){    
       echo "<li><a href='details.php?id=" . $rowSub['bike_id'] . "'>" . $rowSub['bikeName'] . "</a></li>";
       }
       echo '</ul>';   
       echo '</li>';

 }

只需看看你想要关闭ul和li的位置。