我有一个示例数据库:
brand(id, name)
(1, 'Apple')
(2, 'Dell')
model(id, name, brand_id)
(1, 'Macbook', 1)
(2, 'Iphone', 1)
(3, 'Vostro', 2)
和 php mysql 代码:
<?php
// Make a MySQL Connection
$query = "SELECT * FROM model";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
$brand_id = array();
$brand_id = array_unique($row['brand_id']);
?>
<table>
<?php for($i=0; $i<count(brand_id); $i++) {?>
<tr>
<td><?php echo $row['name'] ?></td>
<tr/>
<?php }?>
</table>
结果返回 2 tr of 2 brand_id,但结果显示所有型号名称
<table>
<tr><td>Macbook, Iphone, Vostro</td></tr>
<tr><td>Macbook, Iphone, Vostro</td></tr>
</table>
如何修复它的结果是:
<table>
<tr><td>Macbook, Iphone</td></tr>
<tr><td>Vostro</td></tr>
</table>