Page 1 of 1
Ouputting image results from database in rows of 5
Posted: Mon Feb 22, 2010 3:05 pm
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
Re: Ouputting image results from database in rows of 5
Posted: Mon Feb 22, 2010 3:32 pm
by mikosiko
similar question that topic
viewtopic.php?f=1&t=113157... answer is there
Re: Ouputting image results from database in rows of 5
Posted: Mon Feb 22, 2010 5:35 pm
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).
Re: Ouputting image results from database in rows of 5
Posted: Mon Feb 22, 2010 8:52 pm
by Benjamin
In addition to pickle's solution, you could ditch tables all together and just use floating divs inside of a container.
Re: Ouputting image results from database in rows of 5
Posted: Tue Feb 23, 2010 1:36 pm
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> ";
}
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.
Re: Ouputting image results from database in rows of 5
Posted: Tue Feb 23, 2010 2:12 pm
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> ";
$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
Re: Ouputting image results from database in rows of 5
Posted: Tue Feb 23, 2010 2:23 pm
by Parmenion
Cheers, lshaw. Just tested it and it seems to work well. Just need to get each photo name showing now.