Jump to content


Photo

*solved* embed loop in loop


  • Please log in to reply
2 replies to this topic

#1 SetToLoki

SetToLoki
  • Members
  • PipPipPip
  • Advanced Member
  • 123 posts

Posted 30 October 2006 - 06:15 PM

Wha I am trying to do is crete a dynamic table,

it should print the correct number of rows/coloums dependant on what needs to be entered into it (pulled from a database)
	function LoadCatagories ($columns = 3)
	{
		$result = mysql_query('SELECT * FROM catagories ORDER BY "Name"') or die(mysql_error());
		$noresults = mysql_num_rows($result);
		$rows = $noresults/$columns;
		$rows = ceil($rows);
		$i = 0;
		
		print ('<table width="100%" border="0" id="content_list">');
		while ($row = mysql_fetch_array($result))
		{
			// Now we need to add rows and coloums to the database
			print ('<tr>'); // open a new coloum
				while ($i <= $rows)// print results as many times as there is rows
				{
					printf('<td><a href="%s?cat=%s">%s</a></td>', $_SERVER['PHP_SELF'],$row['catID'], $row['Name']);
					$i++;
				}
			print ('</tr>');// close the coloum
			$i = 0;
		}
		print ('</table>');
             }


There is a copy of the code I am using to do it, on the principle it will run the first while loop, stopping at the first and repeating until it has furfilled the second one and continue with the first until it has generated all the database results.

the html result is as follows
<table width="100%" border="0" id="content_list"><tr><td><a href="/index.php?cat=3">General / Miscellaneous</a></td><td><a href="/index.php?cat=3">General / Miscellaneous</a></td><td><a href="/index.php?cat=3">General / Miscellaneous</a></td></tr><tr><td><a href="/index.php?cat=5">Hire Companies</a></td><td><a href="/index.php?cat=5">Hire Companies</a></td><td><a href="/index.php?cat=5">Hire Companies</a></td></tr><tr><td><a href="/index.php?cat=2">Transport</a></td><td><a href="/index.php?cat=2">Transport</a></td><td><a href="/index.php?cat=2">Transport</a></td></tr><tr><td><a href="/index.php?cat=1">Warehousing</a></td><td><a href="/index.php?cat=1">Warehousing</a></td><td><a href="/index.php?cat=1">Warehousing</a></td></tr><tr><td><a href="/index.php?cat=4">Workshops</a></td><td><a href="/index.php?cat=4">Workshops</a></td><td><a href="/index.php?cat=4">Workshops</a></td></tr></table>

printing the database contents out 5 times, instead of just once.

can anybody out there spread a little lihgt on my prediciment?
[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]
while ($HORSE == $DEAD) beat();
[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

#2 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 30 October 2006 - 10:18 PM

while ($row = mysql_fetch_array($result))
	{
		// Now we need to add rows and coloums to the database
		if($i == 0) print ('<tr>'); // open a new coloum
		$i++; //while ($i <= $rows)// print results as many times as there is rows
		printf('<td><a href="%s?cat=%s">%s</a></td>', $_SERVER['PHP_SELF'],$row['catID'], $row['Name']);
		//$i++;
		if($i == $rows) {
                         print ('</tr>'); // close the coloum
		         $i = 0;
                }
	}


#3 SetToLoki

SetToLoki
  • Members
  • PipPipPip
  • Advanced Member
  • 123 posts

Posted 31 October 2006 - 11:47 AM

cheers that works a treat
[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]
while ($HORSE == $DEAD) beat();
[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users