Jump to content

Archived

This topic is now archived and is closed to further replies.

SetToLoki

*solved* embed loop in loop

Recommended Posts

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?

Share this post


Link to post
Share on other sites
[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]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.