Jump to content

*solved* embed loop in loop


SetToLoki

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?
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.