daveoffy Posted July 19, 2009 Share Posted July 19, 2009 I have tons of images like a gallery. I need to make it so I have 3 rows of 4 images. This has to fit into the code of $nsqry = "SELECT * from `show` WHERE display = '1' ORDER BY `id` DESC"; $nsresult = mysql_query($nsqry); while($nsrow = mysql_fetch_assoc($nsresult)){ $shows = $nsrow['name']; $image = $nsrow['image']; echo '<div class="thumbimage">'; echo '<img src="images/'.$image.'" width="150" height="150"> '; echo '</div>'; } Quote Link to comment https://forums.phpfreaks.com/topic/166517-solved-4-in-a-row-than-next-row/ Share on other sites More sharing options...
Gighalen Posted July 19, 2009 Share Posted July 19, 2009 I believe this is something like what you are looking for. I just wrote it for you, so I may have missed something minor, but the concept remains the same. <?php //Query $q = mysql_query("SELECT * from `show` WHERE display = '1' ORDER BY `id` DESC"); // count = 0 $i = 0; // start our table echo "<table>"; while($data = mysql_fetch_assoc($q)){ // if first column, start the row if($i == 0){ echo "<tr>"; } // echo out the image echo "<td><img src=\"images/\"".$data['image']."\" width="150" height="150"></td>"; // if 4th column, end the row if($i == 3){ echo "</tr>"; } } //increase i by 1 $i++; //if i = 4 (would be a 5th column), reset it to 0 to start a new row if($i == 4){ $i = 0; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/166517-solved-4-in-a-row-than-next-row/#findComment-878126 Share on other sites More sharing options...
Gighalen Posted July 19, 2009 Share Posted July 19, 2009 Oh, make sure you add </table> after the end of the WHILE loop Quote Link to comment https://forums.phpfreaks.com/topic/166517-solved-4-in-a-row-than-next-row/#findComment-878132 Share on other sites More sharing options...
.josh Posted July 19, 2009 Share Posted July 19, 2009 $nsqry = "SELECT * from `show` WHERE display = '1' ORDER BY `id` DESC"; $nsresult = mysql_query($nsqry); echo "<table><tr>"; while($nsrow = mysql_fetch_assoc($nsresult)){ $shows = $nsrow['name']; $image = $nsrow['image']; echo ($row % 4 == 0)? "</tr><tr>" : ""; $row++; echo "<td>"; echo '<img src="images/'.$image.'" width="150" height="150"> '; echo '</td>'; } echo "</tr></table>"; Quote Link to comment https://forums.phpfreaks.com/topic/166517-solved-4-in-a-row-than-next-row/#findComment-878153 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.