Welcome to WebmasterWorld Guest from 54.224.253.82

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Display data from DB - Formatting Issue

     
9:33 pm on Jan 13, 2005 (gmt 0)

Full Member

10+ Year Member

joined:Feb 13, 2003
posts:220
votes: 0


Hi,

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)

9:59 pm on Jan 13, 2005 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 26, 2003
posts:1133
votes: 0


So you need sth like this?

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

10:26 pm on Jan 13, 2005 (gmt 0)

Full Member

10+ Year Member

joined:Feb 13, 2003
posts:220
votes: 0


Thanks mcibor,

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)

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members