-
March 27th, 2009, 07:47 AM
#1
Neat layout with php
Hi! I'm currently working on a small photo-sharing website that allows user uploads,etc. My problem resides in designing a neat layout to display the thumbnails of the photos that have been uploaded by a particular use.
In the current system, they are displayed in different rows by using the <tr><td></td></tr>, with the photos in the cells. I would like to display them as follows:
Photo1 | Photo2 | Photo3 | Photo4
Photo5 | Photo6|
The above is for a situation where there a 6 photos and in general, I want it to be a n*4 layout where the number of columns is constant i.e. 4. Hence, if I have 2 photos, it would be as follows:
Photo1 | Photo2
How do I go about coding the above?
PS: To start, I can put 2 for loops, but where do I check for the number of photos,etc..?
-
March 27th, 2009, 08:57 AM
#2
Re: Neat layout with php
A long time ago I did something similar to this. It's very simple...Create a static variable for the number of columns you want. Then, you have another temporary incrementing variable telling you how many images you have outputted already. Once that variable is equal to the static variable, echo a new table row.
Now, you will need another if statement in that loop to make sure you don't echo a new table row if all the photos are used up as well.
Honestly, using a table is a poor implementation. You should just use <div> tags set to blocks with a set width and height.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
March 27th, 2009, 10:16 AM
#3
Re: Neat layout with php
I'll try with the table first and if everything goes well, i'll switch to the <div> way.
-
March 28th, 2009, 12:41 AM
#4
Re: Neat layout with php
I've come out with something like that, echo'ing letters for the moment:
Code:
$toprint=12;
for ($x=$toprint;$x>=0;$x--)
{
//echo "To print=".$toprint."<br>";
if ($toprint<=3)
{
echo "a ";
}
else
{
//echo "To print=".$toprint."<br>";
for ($printed=1;$printed<=3;$printed++)
{
echo "a ";
if ($printed==3) {
echo "<br>";
$toprint=$toprint-3;
echo "To print=".$toprint."<br>";
}
}
}
}
There's an error in the input and I get the following:
a a a
To print=9
a a a
To print=6
a a a
To print=3
a a a a a a a a a a
-
March 28th, 2009, 06:59 AM
#5
Re: Neat layout with php
I've got it.... Here it is:
Code:
<?php
$toprint=19; //number of 'records'
$desiredcolcount=3; //max columns to display
$colcount=0;
echo("<table><tr>");
for ($x=1;$x<=$toprint;$x++) //change to while ($row=mysql_fetch_array($rs))
{
if ($colcount==$desiredcolcount)
{
$colcount=0;
}
if (!$colcount)
{
echo("</tr>\n<tr>");
}
echo("<td>[your output here]</td>");
$colcount++;
}
echo("</tr></table>");
?>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|