Jump to content

[SOLVED] 4 in a row, than next row.


daveoffy

Recommended Posts

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>';
}

Link to comment
https://forums.phpfreaks.com/topic/166517-solved-4-in-a-row-than-next-row/
Share on other sites

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.