Log in

View Full Version : Show three pictures on each row



captainjustin
05-05-2010, 12:10 AM
Once again I'm stuck! I have a script that displays pictures by retrieving the picture names from my database. It works just fine but I would like to display 3 pictures per row. The code I have displays only one picture per row. I'm still very new to php and need a little more guidance than others so, any help would be greatly appreciated. Thanks in advance, Justin



<?
include_once "index.php";


$sb = "select * from my_pics";
$rsb = mysql_query($sb) or die(mysql_error());
if(mysql_num_rows($rsb) > '0')

{

echo "<table align=center width=470><caption align=center><b>Photo Album Control</b></caption><tr><br><td colspan=2><hr width=100% height=1px color=#000099></td></tr>";







while($ab = mysql_fetch_array($rsb))

{

echo " <table align=center width=120 border=1 cellspacing=0 bordercolor=black class=BlackText>

<tr style=\"background-color:#999999; font-family:verdana; font-size:11; font-weight:bold; color:white\">\n\t<td width=120 align=center>Posted by: $ab[by]<br>Date: $ab[date]</td>\n\t\n\t</tr>

<tr style=\"background-color:#dddddd\">

<td align=center width=120><a href=\"http://$_SERVER[HTTP_HOST]$dir/photo_album/$ab[fn]\" rel=\"lightbox\" title=\"The Picture\"><img src=\"http://$_SERVER[HTTP_HOST]$dir/photo_album/$ab[fn]\" width=\"120\" height=\"100\" /></a></</td></tr>


<tr> <td><a href=del_pic.php?b_id=$ab[b_id]&fn=$ab[fn]><center>Delete</center></a></td>



</tr>









</tr>





</table>



<br>";
}



//echo "</td></tr></table><br><br>";

}

else

{

echo "<br><br><center><span class=BlackLink>No pictures are listed at this time. <br><br>To Post One</span><br><br><a class=TNA href=upload_pics.php><center><b>click here.</b></center></a><br> </center>";

}





include_once('../footer.php');
?>

djr33
05-05-2010, 01:23 AM
You are echoing the entire table for each time you get a picture from the database.

To do this you will need to output the start of the table then the images then the end of the table.
<table><?php...?></table>

Additionally, you will need to do something more complex: create <td> EVERY time, and create <tr> every 3RD time.

So the easiest way to do this is to use the while loop like you have but to store the images into an array:

while ($row = mysql_fetch_assoc($queryresult)) {
$myimgs[] = $row['img'];
}
(roughly)

Now after this is where it gets complex:

echo '<table><tr>';
for($x=0;$x<count($myimgs);$x++) {
if ($x%3==0) { echo '</tr>'; }
echo '<td><img...src="'.$myimgs[$x].'"></td>';
}
echo '</tr></table>';

There are other ways to organize this, but that seems like (perhaps) the most straightforward. The tricky part is executing the <tr> every 3rd time. For this it's important to have a number you can count (here it's $x). Using just the while loop it's not that easy. You could integrate the two, but that would be more complex...