Ouputting image results from database in rows of 5

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Parmenion
Forum Commoner
Posts: 35
Joined: Sat Dec 12, 2009 8:29 am

Ouputting image results from database in rows of 5

Post by Parmenion »

Hi guys,

I've designed a website and am trying to output some images from a database into rows of 5 to form an image gallery. What I would like is something like the following:

Image Image Image Image Image
Name Name Name Name Name

Image Image Image Image Image
Name Name Name Name Name

Image Image Image Image Image
Name Name Name Name Name

Is there a way to output into tables echoing the photo and name in tables that are side by side and telling it to go onto a new row after 5 images?

Any help would be great.

Thanks
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Ouputting image results from database in rows of 5

Post by mikosiko »

similar question that topic viewtopic.php?f=1&t=113157... answer is there
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Ouputting image results from database in rows of 5

Post by pickle »

First, you create a loop. Then, using the modulus operator (%), you create a new row every 5 iterations. How I've done it in the past is to create 2 rows for each iteration - one that contains the photos and one that contains the name. At the end of the set of 5, output the photo row, then the name row. This saves you having to iterate through the data twice (once to output the photo row, once to output the corresponding name row).
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Ouputting image results from database in rows of 5

Post by Benjamin »

In addition to pickle's solution, you could ditch tables all together and just use floating divs inside of a container.
Parmenion
Forum Commoner
Posts: 35
Joined: Sat Dec 12, 2009 8:29 am

Re: Ouputting image results from database in rows of 5

Post by Parmenion »

Thanks for the replies.

At the moment my code is:

Code: Select all

 
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
    { 
        $id = $row['id'];
        echo "<a href='photo.php?photo&id=$id'><img class='photo' src='".($row['photo_link'])."' width='110' alt='".($row['photo_name'])."' /></a>&nbsp;&nbsp;&nbsp;";
    }
 
I used CSS to place the pictures side by side but didn't see a way to have the photo names under the photos or how to start new rows every 5 pictures so thought I'd try asking here.

I'll have a go at what Pickle has suggested and see if I can get the code right.
lshaw
Forum Commoner
Posts: 69
Joined: Mon Apr 20, 2009 3:40 pm
Location: United Kingdom

Re: Ouputting image results from database in rows of 5

Post by lshaw »

Code: Select all

 
$count=0; //set a count variable
$row_length=5; //set the length of each row
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
        $id = $row['id'];
        echo "<a href='photo.php?photo&id=$id'><img class='photo' src='".($row['photo_link'])."' width='110' alt='".($row['photo_name'])."' /></a>&nbsp;&nbsp;&nbsp;";
        $count++; //increase count
        if($count==$row_length)
             {
                   echo "<br /><br />"; //two line breaks, or anything you want
                   $count=0; //reset the count variable
             }
    }
 
Untested, but you get the idea
Parmenion
Forum Commoner
Posts: 35
Joined: Sat Dec 12, 2009 8:29 am

Re: Ouputting image results from database in rows of 5

Post by Parmenion »

Cheers, lshaw. Just tested it and it seems to work well. Just need to get each photo name showing now.
Post Reply