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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.