Page 1 of 1

Nested looping trouble... :(

Posted: Thu Aug 14, 2003 2:14 pm
by robster
Sorry for the title change, this one makes more sense to the current problem as this thread has moved on...


I am in essense, creating a gallery in php using mysql as the database.
I want to be able to display these thumbnails (referenced in the database) in table format.

I want them to say, be 5 columns wide and however many rows tall that are required.

The problem I envision, is when say, the database doesn't have a number that will match the five colums. here's some examples:

xxxxx
xxxxx
xxxxx

This one (above) would work perfecly as the database has 15 thumbnails.


xxxxx
xxx

This one (above) would be ideal also, but how do you get PHP to tell HTML to draw only the number of columns required (for any given row) based on the number of thumbnails it needs to draw.

It's got me stumped. There's obviously a way to do it due to all the galleries on the net but I'd really (REALLY) appreciate some theory on how you'd go about doing it.

Thanks SO much for anything you can offer.

:)

Rob

Posted: Thu Aug 14, 2003 2:44 pm
by JayBird
Wouldn't you just need to output two empty cells to complete the table?

Posted: Thu Aug 14, 2003 2:51 pm
by robster
Thank you :)

I guess... so.. (I'm really not terribly good at this sorry).

So would it then be.. an if then type loop for instance that says something like:

<tr> //start the row
x=1
if (x does not equal 5) and (there are still thumbnails to show)
{
create cell
show thumbnail
x=x+1
}
elseif (x DOES = 5)
{
</tr> //end last open row<tr>
<tr> //start the next open row <tr>
}
else (if there are no more thumbnails)
{
finish loop
</tr> //close all open rows
}



Does that make sense? I wonder if it would work... Again, apologies for this thinking out loud. I find it quite interesting, I hope you do too :)

Posted: Thu Aug 14, 2003 3:32 pm
by JayBird
you thinking right, but your not quite there

Code: Select all

// Get the number of thumbnails that are in the Database

// Add 1 to the number of thumbnails until it is divisable by five

// Start the table

// While $x is less than the number of thumnails and $x is not divisable by 5, output the <td> and thumbnails

// When $x is divisable by 5, start new row

// Repeat previous

// When $x equals actual number of thumnails, output enough cells to complete the table

// Finish table
I left my PHP head at work, maybe i'll be able to write some code for you tomorrow unless someone beats me to it, but try and do it yourself, it is the best way to learn :O

If you write your own code, and have some problems, post it here, people are more likely to help you if you have had a bash, and got stuck, rather than saying, i need to do this, do it for me! :lol:

Cheerz

Mark

Posted: Thu Aug 14, 2003 3:35 pm
by robster
I'm onto it!

Thanks so much for the guidance.
btw, I'm never after anyone to write my code for me as I'm truly enjoying the php experience. I often just need a little jab in the ribs or direction. I'm not a coder (I'm an animator/artist/photographer) but am getting SO addicted it's silly. I'm fighting years of art training to take on the programming side but finding it so rewarding.

That's why bits of code and/or logic help me.

:)

Thanks again, I'll try what you said now.

Rob

Posted: Sat Aug 16, 2003 7:17 am
by robster
OK :)

woah, what a mission for a beginer. Please don't laugh at my code (too hard ;) ).

I've so far accomplisehd successfully these parts that bech100 suggested:
// Get the number of thumbnails that are in the Database
// Add 1 to the number of thumbnails until it is divisable by five
// Start the table


You can see them here:

Code: Select all

 //Variables for the loops below 
   $monthlytotal = "0";  //This one holds the total number of entries for each month 
   $ShowMaxMonth = mysql_num_rows($content);   
   $ShowMax = mysql_num_rows($content); 
//   $x = 1; //This one used in the while loop for drawing the rows etc in the table 
    
    
   // Get the number of thumbnails that are in the Database  
   for ($y=1; $y<=$ShowMaxMonth; $y++) 
   {  
      $year = $Xcontent["year"]; 
      $month = $Xcontent["month"]; 
 
      //Only load images etc from the appropriate year 
      if ($year == "2001") 
      { 
         //Only load images etc from the appropriate month 
         if ($month == "12") 
         { 
            $monthlytotal = $monthlytotal + "1"; 
            print "$monthlytotal, "; 
         } 
      } 
      $Xcontent = mysql_fetch_array($content); 
   } 
 
    
 
    
   //Is monthlytotal divisible by 4?  If not, make it!!!   
   $mod = $monthlytotal % 4; 
   if ($mod != 0) 
   { 
     $monthlytotal += (4 - $mod); 
     echo "<br>rounded up to $monthlytotal"; 
   } 
 
 
 
   //Start the table to draw the thumbnails into 
   print "<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center" bordercolor="$sBColor">"; 
     print "<tr>";
OK, so they work (it was fun and a challenge, I'll clean it up more later).
Now I'm moving onto the bit where I was suggested to do these things:
// While $x is less than the number of thumnails and $x is not divisable by 5, output the <td> and thumbnails

// When $x is divisable by 5, start new row
// Repeat previous
// When $x equals actual number of thumnails, output enough cells to complete the table
// Finish table


I've got the whole code here, but am having real trouble with it, so far I'm only trying to get the new row to start at (4, not 5). It's just not working. I think I'm temporarily overstressing my artist braincells... :(

Please if you ahve time, look through my code and perhaps point to what I might be doing wrong? I'd REALLY appreciate it :)

Thanks heaps,

Rob




Code: Select all

<?
	$connection = mysql_connect($dbhost, $dbusername, $dbpassword);
	
	$content = mysql_db_query($dbname, "SELECT * FROM archives ORDER BY id ASC");
	$Xcontent = mysql_fetch_array($content);

	//Variables for the loops below
	$monthlytotal = "0";  //This one holds the total number of entries for each month
	$ShowMaxMonth = mysql_num_rows($content);  
	$ShowMax = mysql_num_rows($content);
	$x = 1; //This one used in the while loop for drawing the rows etc in the table
	
	
	// Get the number of thumbnails that are in the Database 
	for ($y=1; $y<=$ShowMaxMonth; $y++)
	{ 
		$year = $Xcontent["year"];
		$month = $Xcontent["month"];

		//Only load images etc from the appropriate year
		if ($year == "2001")
		{
			//Only load images etc from the appropriate month
			if ($month == "12")
			{
				$monthlytotal = $monthlytotal + "1";
				print "$monthlytotal, ";
			}
		}
		$Xcontent = mysql_fetch_array($content);
	}

	

	
	//Is monthlytotal divisible by 4?  If not, make it!!!  
	$mod = $monthlytotal % 4;
	if ($mod != 0)
	{
	  $monthlytotal += (4 - $mod);
	  echo "<br>rounded up to $monthlytotal";
	}



	//Start the table to draw the thumbnails into
	print "<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center" bordercolor="$sBColor">";
  	print "<tr>";



	$content = mysql_db_query($dbname, "SELECT * FROM archives ORDER BY id ASC");
	$Xcontent = mysql_fetch_array($content);

	$ShowMax = mysql_num_rows($content);
	for ($y=1; $y<=$ShowMax; $y++)
	{ 
		$year = $Xcontent["year"];
		$month = $Xcontent["month"];

		//Only load images etc from the appropriate year
		if ($year == "2001")
		{
			//Only load images etc from the appropriate month
			if ($month == "12")
			{
			
			for ($x=1; $x<=$monthlytotal; $x++)
			{	
				
									
				// While $x is less than the number of thumnails and $x is not divisable by 4, output the <td> and thumbnails 
				while ($x < $monthlytotal and ($x % 4) != 0)
				{
					print "<td>";
					print "month - $month";
					print "</td>";
				}
				
				// When $x is divisable by 4, start new row 
				if ($x = ($x % 4) != 0)
				{
				print "<tr>";
				}
				
			} //end for loop
				
			}
		}
		
		

	  	print "</tr>";
		$Xcontent = mysql_fetch_array($content);
	}



  	print "</table>";	
	mysql_free_result($content);		

?>

Posted: Sat Aug 16, 2003 8:31 am
by pootergeist
http://www.teckis.com/scriptix/tutorial ... /teck.html

the second page of that tutorial has a ternery for NN-cells per tr output using modulus conditionals - end with a <td colspan="< empty cells >"> so should be what you're after.

Posted: Sat Aug 16, 2003 8:55 am
by robster
thanks so much for the link, it was just a bit too much like rocket science for me. Sorry I just couldn't get it.

also, a problem I'm having is my while loop sits in the middle of a for loop that descerns if the month is the one i want and if the year is the one i want. this gives wierd results on any while loop i seem to stick in there...

I'm starting to go blurry over this :(