所以我的问题是我从这张桌子中名为“BikeImages”的表中导出图像,这是我们在同一个外键KeyCode下的图像。如何确保图像一起添加而不是单独添加。
例如
BikeCode:123
BikeImages:IMAGE1,IMAGE2
BikeCode:14
BikeImages IMAGE1
<?php
$sql1 = "SELECT BikeCode, Manufacturer, Model, SubType, Year, FrameMaterial, Description, Gender, Type, Price, Stock FROM Bike WHERE Stock > 0";
$result1 = mysqli_query($con, $sql1);
while(list($bikecode, $manufacturer, $model, $subtype, $year, $fmaterial, $desc, $gender, $type, $price, $stock) = mysqli_fetch_row($result1)) {
echo "
<table>
<tr><th>BikeCode:</th>
<th>Manufacturer:</th>
<th>Model:</th>
<th>Subtype:</th>
<th>Year:</th>
</tr>
<tr>
<td>$bikecode</td>
<td>$manufacturer</td>
<td>$model</td>
<td>$subtype</td>
<td>$year</td>
</tr>
<tr>
<th>FrameMaterial:</th>
<th>Gender:</th>
<th>Type:</th>
<th>Price:</th>
<th>Stock:</th>
</tr>
<tr>
<td>$fmaterial</td>
<td>$gender</td>
<td>$type</td>
<td>£$price</td>
<td>$stock</td>
</tr>
<tr><th>Description:</th><td colspan=\"4\">$desc</td></tr>
<tr><th>Bike Images:</th><td colspan=\"4\"><a href=\"$sourcepath\" title=\"$description\"><img src=\"$sourcepath\" width=\"72\" height=\"72\" /></a></td></tr>
<tr><th>Order Now:</th><td colspan=\"4\"><a href=\"basket.php?action=add&id=$bikecode\">Add To Cart</a></td></tr>
</table>";
}
?>
提前感谢您的帮助。
编辑:我必须将此代码与上面的代码结合起来:
<?php
$sql = "SELECT SourcePath, Description, BikeCode FROM BikeImages order by bikecode";
$result = mysqli_query($con, $sql);
$previous = -1;
echo "<table>";
while(list($sourcepath, $description, $bcode) = mysqli_fetch_row($result)) {
// if bikecode changed, append "Order now"
if ($previous != -1 && $previous != $bcode) {
echo "<tr><th>Order Now:</th><td colspan=\"4\"><a href=\"basket.php?action=add&id=$previous\">Add To Cart</a></td></tr>";
}
echo "<tr><th>Bike Images:</th><td colspan=\"4\">";
echo "<a href=\"$sourcepath\" title=\"$description\"><img src=\"$sourcepath\" width=\"72\" height=\"72\" /></a>";
echo "</td></tr>";
$previous = $bcode;
}
echo "</table>\n";
?>
答案 0 :(得分:0)
不要将表标记放在循环中,而是放在
之外<?php
// if bikecode changed, append "Order now"
if ($previous != -1 && $previous != $bikecode) {
echo "<tr><th>Order Now:</th><td colspan=\"4\"><a href=\"basket.php?action=add&id=$previous\">Add To Cart</a></td></tr>";
}
echo "<tr><th>Bike Images:</th><td colspan=\"4\"><a href=\"$sourcepath\" title=\"$description\"><img src=\"$sourcepath\" width=\"72\" height=\"72\" /></a></td></tr>";
$previous = $bikecode;
}
echo "</table>\n";
?>
使用两个选择,你有一个穿过自行车的外环和一个遍历图像的内循环。一个“简单但”昂贵的解决方案,可能是
<?php
$sql1 = "SELECT BikeCode, Manufacturer, Model, SubType, Year, FrameMaterial, Description, Gender, Type, Price, Stock FROM Bike WHERE Stock > 0";
$result1 = mysqli_query($con, $sql1) or die('Query1 failed: ' . mysqli_error($con));
echo "<table>";
while(list($bikecode, $manufacturer, $model, $subtype, $year, $fmaterial, $desc, $gender, $type, $price, $stock) = mysqli_fetch_row($result1)) {
echo "<tr><th>BikeCode:</th>
<th>Manufacturer:</th>
<th>Model:</th>
<th>Subtype:</th>
<th>Year:</th>
</tr>
<tr>
<td>$bikecode</td>
<td>$manufacturer</td>
<td>$model</td>
<td>$subtype</td>
<td>$year</td>
</tr>
<tr>
<th>FrameMaterial:</th>
<th>Gender:</th>
<th>Type:</th>
<th>Price:</th>
<th>Stock:</th>
</tr>
<tr>
<td>$fmaterial</td>
<td>$gender</td>
<td>$type</td>
<td>£$price</td>
<td>$stock</td>
</tr>
<tr><th>Description:</th><td colspan=\"4\">$desc</td></tr>\n";
$sql = "SELECT SourcePath, Description FROM BikeImages where bikecode = '$bikecode'";
$result = mysqli_query($con, $sql) or die('Query2 failed: ' . mysqli_error($con));
while(list($sourcepath, $description) = mysqli_fetch_row($result)) {
<tr><th>Bike Images:</th><td colspan=\"4\"><a href=\"$sourcepath\" title=\"$description\"><img src=\"$sourcepath\" width=\"72\" height=\"72\" /></a></td></tr>";
}
echo "<tr><th>Order Now:</th><td colspan=\"4\"><a href=\"basket.php?action=add&id=$bikecode\">Add To Cart</a></td></tr>\n";
}
echo "</table>";
?>