以下代码一次列出每行的每个caritems - 在每行显示carname :( echo mysql_result($ result,$ i,'carname')。“>”;
不是在每一行上显示carname,而是如何制作它以便通过carname将caritems组合在一起,然后在顶部显示carname,每个caritems与该carname相关联显示在下面...它会然后转到下一个车名,分组项目......
$result = mysql_query("SELECT * FROM cars LEFT JOIN caritems ON cars.carid = caritems.carid ORDER BY cars.carsortorder ASC");
$total_results = mysql_num_rows($result);
for ($i = 0; $i < 1500; $i++)
{
if ($i == $total_results) { break; }
$rowid = mysql_result($result, $i, 'caritems.caritemid');
echo mysql_result($result, $i, 'carname') . " > ";
echo "<div class='carved' />";
echo "<strong class='caritemheader'>" . mysql_result($result, $i, 'caritemname') . " " . mysql_result($result, $i, 'caritemprice') . "</strong><br />";
/* CHECK IF BLOB EXISTS AND IF SO DISPLAY IMAGE */
if (strlen(mysql_result($result, $i, 'caritempicture'))>0) { echo '<br />' . '<img src="data:image/png;base64,' . base64_encode(mysql_result($result, $i, 'caritempicture')) . '"/>' . '<br />'; }
echo mysql_result($result, $i, 'caritemdescription') . "<br /><br />";
echo "</div>";
}
答案 0 :(得分:0)
这是一个使用PDO的基本结构,并像其他人所建议的那样捕获独特的魔法。我没有整合你现有的所有代码。
try {
$db = new PDO("mysql:host=localhost;charset=utf8", "root", "root");
$cmd = $db->prepare("
SELECT * FROM cars
LEFT JOIN caritems ON cars.carid = caritems.carid
ORDER BY cars.carsortorder ASC;
");
if ($cmd->execute()) {
$carname = "";
while ($row = $cmd->fetch(PDO::FETCH_ASSOC)) {
if ($carname != $row['carname']) {
echo $row['carname'];
}
echo /* everything else */
$carname = $row['carname'];
}
}
} catch (Exception $e) { echo $e->getMessage(); return; }
答案 1 :(得分:-2)
您可以先选择不同的 carid ,如下所示:
select distinct carid from cars
然后在for循环中逐个查询每辆车的物品,如下所示:
select * from caritems where carid=$carid
此处 $ carid 来自第一个查询。