1

This is my output code so far. It currently displays everything underneath each other, as expected.

<table id="dslrTable">

                <?php 

                while($row = mysqli_fetch_assoc($result)) { ?>

                            <?php echo "<tr><td> <a href=model.php?id=" . $row["ID"] . ">" . "<img src=images/" . $row["Image URL"] . " </a></td></tr>"; ?>
                            <?php echo "<tr><td> <a href=model.php?id=" . $row["ID"] . ">" . $row["Make"] . " " . $row["Model"] . " </a></td><tr>"; ?>
                            <?php echo "<tr><td> <a href=#.php>[add to compare] </a> </td></tr>"; ?>

                <?php } ?>

            </table> 

What I am trying to achieve is three tr's per row in the database. The image, make/model and then a hyperlink. After 4 "results", then make a new tr. Something like this.

<table>
                <tr>
                    <td>Image 1</td>
                    <td>Image 2</td>
                    <td>Image 3</td>
                    <td>Image 4</td>
                </tr>
                <tr>
                    <td>Make and Model 1</td>
                    <td>Make and Model 2</td>
                    <td>Make and Model 3</td>
                    <td>Make and Model 4</td>
                </tr>
                <tr>
                    <td>Hyperlink 1</td>
                    <td>Hyperlink 2</td>
                    <td>Hyperlink 3</td>
                    <td>Hyperlink 4</td>
                </tr>
                <tr>
                    <td>Image 5</td>
                    <td>Image 6</td>
                    <td>Image 7</td>
                    <td>Image 8</td>
                </tr>
                <tr>
                    <td>Make and Model 5</td>
                    <td>Make and Model 6</td>
                    <td>Make and Model 7</td>
                    <td>Make and Model 8</td>
                </tr>
                <tr>
                    <td>Hyperlink 5</td>
                    <td>Hyperlink 6</td>
                    <td>Hyperlink 7</td>
                    <td>Hyperlink 8</td>
                </tr>
            </table>

And for this to keep going until my while loop stops. I know I need to use modulo division as well as a counter variable, but I can't get it to work.

Any help is appreciated, thanks.

2 Answers 2

1
    <?php
         $i=0;
         while($row = mysqli_fetch_assoc($result)) {
            $image[$i] = $row["Image URL"];
            $model[$i] = $row["Model"];
            $hyper_link[$i] = $row["Hyperlink"];
            $i++;
        }

    ?>


    <table border="1">
         <?php 
         $k=0;
         $l=1;
         $m=2;
         $n=3;
         for($j=0;$j<count($image)/4;$j++){
             echo "<tr>";
             echo "  <td> {$image[$k]}</td>";
             echo "  <td> $image[$l]</td>";
             echo "  <td> $image[$m]</td>";
             echo "  <td> $image[$n]</td>";
             echo "</tr>";
             echo "<tr>";
             echo "  <td> $model[$k] </td>";
             echo "  <td> $model[$l]</td>";
             echo "  <td> $model[$m]</td>";
             echo "  <td> $model[$n]</td>";     
             echo "</tr>";
             echo "<tr>";
             echo "  <td> $hyper_link[$k] </td>";
             echo "  <td> $hyper_link[$l]</td>";
             echo "  <td> $hyper_link[$m]</td>";
             echo "  <td> $hyper_link[$n]</td>";
             echo "</tr>";
             $k +=4;
             $l +=4;
             $m +=4;
             $n +=4;
         } 
         ?>
    </table>
Sign up to request clarification or add additional context in comments.

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
0

I pretty sure that's not the appropriate use of the function mysqli_fetch_assoc(). You really should store your results in an array variable, count the result, and pass it all through a for loop.

$row = mysqli_fetch_assoc($result);

$result_count = count($row);

for($i = 0; $i <= $result_count; $i++){

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.