homepage Welcome to WebmasterWorld Guest from 204.236.254.124
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Display data from DB - Formatting Issue
woldie




msg:1276057
 9:33 pm on Jan 13, 2005 (gmt 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)

 

mcibor




msg:1276058
 9:59 pm on Jan 13, 2005 (gmt 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

woldie




msg:1276059
 10:26 pm on Jan 13, 2005 (gmt 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)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved