takisis666 Posted August 30, 2015 Share Posted August 30, 2015 I am populating a table from a database. The data is being retrieved correctly however it is not being displayed correctly. This is how I want it to be displayed: +----------------------------------------------------+ | vehicle 1 | vehicle 2 | vehicle 3 | vehicle 4 | |-----------------------------------------------------| | vehicle 5 | vehicle 6 | vehicle 7 | vehicle 8 | |-----------------------------------------------------| | vehicle 9 | veh 10 | veh 11 | veh 12 | +----------------------------------------------------+ This is what I am getting: +----------------------------------------------------+ | vehicle 1 | vehicle 1 | vehicle 1 | vehicle 1 | |-----------------------------------------------------| | vehicle 2 | vehicle 2 | vehicle 2 | vehicle 2 | |-----------------------------------------------------| | vehicle 3 | vehicle 3 | vehicle 3 | vehicle 3 | +----------------------------------------------------+ Here is my code: <?php error_reporting(E_ALL); ini_set('display_errors', '1'); $conn=mysqli_connect("localhost","root","","ezwayautos"); if($conn) { $sql="SELECT * FROM vehicles ORDER BY RAND() LIMIT 12"; $result=mysqli_query($conn,$sql); } if(! $result ) { die('Could not get data: ' . mysqli_error($conn)); } $rows = 3; $cols = 4; echo "<table>"; while($row = mysqli_fetch_assoc($result)) { for($tr=1;$tr<=$rows;$tr++) { echo "<tr>"; for($td=1;$td<=$cols;$td++) { echo "<td height='160' valign='top' class='featured'>"; echo "<div class='Image-Thumbnail'>"; echo "<a href=''>"; echo "<img src='".$row['image']."' width='160' height='54' alt='".$row['vehicle_name']."'>"; echo "</a>"; echo "</div> <a href=''>" .$row['vehicle_name']. "</a>"; echo "</td>"; } echo "</tr>"; } } echo "</table>"; ?> Please help Quote Link to comment Share on other sites More sharing options...
iarp Posted August 30, 2015 Share Posted August 30, 2015 That's because $row's value don't change until the next iteration happens in the while () loop. Both of your for statements are executing within the while so $row doesn't change. What you're looking for is a counter that checks if the current iteration of the while loop is divisible by 4 (because you want 4 columns) to then break and start a new row. This is untested but I think this is what you're after. echo '<tr>'; $counter = 0; while($row = mysqli_fetch_assoc($result)) { echo "<td height='160' valign='top' class='featured'>"; echo "<div class='Image-Thumbnail'>"; echo "<a href=''>"; echo "<img src='".$row['image']."' width='160' height='54' alt='".$row['vehicle_name']."'>"; echo "</a>"; echo "</div> <a href=''>" .$row['vehicle_name']. "</a>"; echo "</td>"; if ($counter % 4 === 0) echo '</tr><tr>'; $counter++; } echo '</tr>'; Quote Link to comment Share on other sites More sharing options...
takisis666 Posted August 30, 2015 Author Share Posted August 30, 2015 That fixed the the issue somewhat, now I am getting on the first row just one cell, on the second row 4 cells, and on the final row 3 cells. Quote Link to comment Share on other sites More sharing options...
iarp Posted August 30, 2015 Share Posted August 30, 2015 Try putting the $counter++ to just above the "if ($counter..." statement. Quote Link to comment Share on other sites More sharing options...
takisis666 Posted August 30, 2015 Author Share Posted August 30, 2015 Thank you iarp, that did the trick. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.