CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2004
    Location
    Mauritius Island
    Posts
    154

    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..?

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    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.

  3. #3
    Join Date
    Oct 2004
    Location
    Mauritius Island
    Posts
    154

    Re: Neat layout with php

    I'll try with the table first and if everything goes well, i'll switch to the <div> way.

  4. #4
    Join Date
    Oct 2004
    Location
    Mauritius Island
    Posts
    154

    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&nbsp;";
    	}
    	else
    	{
    		//echo "To print=".$toprint."<br>";
    		for ($printed=1;$printed<=3;$printed++)
    		{
    			echo "a&nbsp;";
    			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

  5. #5
    Join Date
    Oct 2004
    Location
    Mauritius Island
    Posts
    154

    Resolved 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
  •  





Click Here to Expand Forum to Full Width

Featured