SetToLoki Posted October 30, 2006 Share Posted October 30, 2006 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)[code] 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>'); }[/code]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 [code]<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>[/code]printing the database contents out 5 times, instead of just once.can anybody out there spread a little lihgt on my prediciment? Link to comment https://forums.phpfreaks.com/topic/25608-solved-embed-loop-in-loop/ Share on other sites More sharing options...
sasa Posted October 30, 2006 Share Posted October 30, 2006 [code]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; } }[/code] Link to comment https://forums.phpfreaks.com/topic/25608-solved-embed-loop-in-loop/#findComment-116992 Share on other sites More sharing options...
SetToLoki Posted October 31, 2006 Author Share Posted October 31, 2006 cheers that works a treat Link to comment https://forums.phpfreaks.com/topic/25608-solved-embed-loop-in-loop/#findComment-117221 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.