简体   繁体   中英

How to add images from a database together if they have the same foreign key

So my problem is i am exporting images from a table called "BikeImages" within this table theres images that our under the same foreign Key BikeCode. How do i make sure the images are added together instead of seperately.

For Example

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>";
            }
      ?>

Thank you in advance for your help.

EDIT: I have to combine this code with the above code:

<?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";
?>

Don't put the table tag in the loop, but outside

<?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";
?>

With the two selects, you have an outer loop going through bikes and an inner loop iterating over the images. An "easy" but expensive solution, could be

<?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>";
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM