Results 1 to 2 of 2

Thread: Show three pictures on each row

  1. #1
    Join Date
    Aug 2009
    Location
    Florida
    Posts
    23
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Show three pictures on each row

    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
    Code:
     
    
    <?
    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');
    ?>
    Last edited by djr33; 05-05-2010 at 01:17 AM.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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:
    PHP Code:
    while ($row mysql_fetch_assoc($queryresult)) {
    $myimgs[] = $row['img'];

    (roughly)

    Now after this is where it gets complex:
    PHP Code:
    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...
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •