Php 如果数据库中的图像具有相同的外键,如何将它们添加到一起
所以我的问题是,我正在从一个名为“BikeImages”的表中导出图像,在这个表中,我们的图像位于同一个外键BikeCode下。如何确保将图像添加到一起而不是单独添加 比如说 代码:123 比基图像:图像1,图像2 比基代码:14 比基梅图像1Php 如果数据库中的图像具有相同的外键,如何将它们添加到一起,php,html,mysql,database,Php,Html,Mysql,Database,所以我的问题是,我正在从一个名为“BikeImages”的表中导出图像,在这个表中,我们的图像位于同一个外键BikeCode下。如何确保将图像添加到一起而不是单独添加 比如说 代码:123 比基图像:图像1,图像2 比基代码:14 比基梅图像1 <?php $sql1 = "SELECT BikeCode, Manufacturer, Model, SubType, Year, FrameMaterial, Description, Gender, Type, P
<?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";
?>
不要将表标记放在循环中,而是放在外部
<?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>";
?>
按BikeCode排序
,因此所有相关图像都会一起显示在结果中?您是否有多个bikeimages在一行中以逗号分隔?@MarcB是的,我相信这就是我需要的,我将尝试一下。否不起作用。非常感谢您的帮助@OlafDietsche否,所有我的bikeImage都在单独的行中。创建函数,将bikecode
作为参数,并返回包含所有图像的HTML表(请选择bikecode=parameter的位置)。。。然后循环遍历所有要显示的自行车,并在look中调用函数,返回包含所有imagesHey的完整表格,这确实有效,但是当与其他代码组合时,它不起作用。我编辑了上面的帖子。它不起作用。我认为这是因为忘记了添加这些信息$sql=“按BikeCode顺序从BikeImages中选择SourcePath、Description、BikeCode”$结果=mysqli_查询($con,$sql);您已从Bike表中选择了信息,但未从BikeImages中选择。@Moe是的,我没有查看其他SQL语句。我想它只是比你的第一个稍微修改了一点。所以,这是一个不同的问题。您必须执行第二个查询,或者在这两个查询之间进行连接。不管怎样,它都比你原来的问题要复杂一点。这就是为什么我一直坚持这个问题的原因。我怎么做一个联合查询而第二个查询不起作用,因为我必须做另一个while循环,对吗?@Moe无论哪种方式,你都必须做一个内部循环,因为它似乎是一对多的关系。对于每辆自行车,你都有几个图像。