0

所以我的问题是我正在从这个表中的一个名为“BikeImages”的表中导出图像,我们在同一个外键 BikeCode 下的图像。我如何确保图像被添加在一起而不是单独添加。

例如

自行车代码:123

自行车图像:IMAGE1、IMAGE2

自行车代码:14

自行车图像 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";
?>
4

1 回答 1

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>";
?>
于 2012-12-08T17:17:08.623 回答