Forum Moderators: coopster
I've got this simple problem I'm trying to resolve. Basically what I am trying to achieve is to display 4 categories on one row of the table and then the next four on the next row. I'm using a count variable to determine when to print the next row.
Any ideas?
Here's the code:
<table cellspacing="4" cellpadding="4" border=1>
<?
$result=mysql_query("select cat_name
from quiz_category
where parentid=1
order by cat_name");
$cnt=0;
while (list($DBcat_name)=mysql_fetch_row($result))
{
?>
<tr>
<td valign=top><? echo $DBcat_name?></td>
</tr>
<?
$cnt++;
}
?>
</table>
Many Thanks
W :o)
Classical Music¦Poetry¦ Literature ¦Pop
Wars 1800-2005 ¦Films ¦Space travel¦TV series
?
counter is a good idea. I presume code would be sth like:
<table><tr>
<?PHP
$result = mysql_query("SELECT cat_name FROM quiz_category WHERE parentid = '1' ORDER BY cat_name");
$cnt = 1;while($DBcat_name = mysql_fetch_row($result))
{
if($cnt == "4")
{
$cnt = 0;
echo "
<td valign=\"top\">".$DBcat_name[0]."</td>
</tr><tr>";
}
else echo "<td valign=\"top\">".$DBcat_name[0]."</td>";$cnt++;
}
?>
<td></td></tr></table>
it's not the nicest way to do this, but it works
best regards!
Michal Cibor
That does work, I did have a look at the code when its been parsed from the server, and it leaves '<tr><td></td></tr>' at the end, but I can work that out anyway, so thanks.
What I'm trying to do now is to display the subcategories beneath the main category. Now what I have done is created a function so that it calls itself so that it displays the subcategories.
function maketree($rootcatid,$level)
{
global $cnt;
$sql="select catid,cat_name
from quiz_category
where parentid=$rootcatid
order by cat_name";
$result=mysql_query($sql);
echo '<tr>';
while (list($DBcatid,$DBcatname)=mysql_fetch_row($result))
{
echo "<td valign=top>$DBcatname</td>";
$cnt++;
if ($cnt==4)
echo '</tr><tr>';
maketree($DBcatid,$level+1);
}
}
?>
<?
$cnt=0;
?>
<table cellpadding="2" cellspacing="2" border="1" width="70%">
<? maketree(0,0);?>
</table>
Is this the right way?
Cheers
Woldie :o)