run two queries in a while loop.
<?php
$html = "<ul>\n";
$query = mysql_query("SELECT `cat_id`,`cat_name` FROM `tblCategory`");
while( list( $id,$name ) = mysql_fetch_row( $query ) ) {
$html .= "\t<li>{$name}";
$query2 = mysql_query("SELECT `topic_id`,`topic_name` FROM `tblTopics` WHERE `cat_id` = {$id}");
if ( mysql_num_rows( $query ) > 0 ) {
$html .= "\t\t<ul>\n";
while( list( $tid,$tname ) = mysql_fetch_row( $query2 ) ) {
$html .= "\t\t\t<li>{$tname}</li>\n";
}
$html .= "\t\t</ul>\n";
}
$html .= "</li>\n";
}
$html .= "</ul>";
echo $html;
?>
I know there is a recursive way to do it (didn't feel like thinking of it), but this will work if you only want 2-tier.
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
As you've mentioned, it is best to have 1 table as it will save recourses and space. So the table structure will be as follows:
tblCategory
cat_id as primary key
cat_name as nvarchar
topic_id as nvarchar
topic_name as nvarchar
Then to retrieve the data and display it as the html format shown in post #1, simply use the following:
<?
//mysql connections
$result=mysql_query('SELECT * FROM `tblCategory` ORDER BY cat_id');
$catid=false;
while ($row=mysql_fetch_array($result)) {
if ($catid!==$row['catname']) {
if ($catid==false) {
echo '<ul><li>'.$row['cat_name'].'</li><ul>';
} else {
echo '</ul><li>'.$row['cat_name'].'</li><ul>';
}
echo '<li>'.$row['topic_name'].'</li>';
}
$catid=$row['cat_name'];
}
Hope that helps.
cwarn23
Occupation: Genius
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
As you've mentioned, it is best to have 1 table as it will save recourses and space. So the table structure will be as follows:
tblCategory
cat_id as primary key
cat_name as nvarchar
topic_id as nvarchar
topic_name as nvarchar
Then to retrieve the data and display it as the html format shown in post #1, simply use the following:
<?
//mysql connections
$result=mysql_query('SELECT * FROM `tblCategory` ORDER BY cat_id');
$catid=false;
while ($row=mysql_fetch_array($result)) {
if ($catid!==$row['catname']) {
if ($catid==false) {
echo '<ul><li>'.$row['cat_name'].'</li><ul>';
} else {
echo '</ul><li>'.$row['cat_name'].'</li><ul>';
}
echo '<li>'.$row['topic_name'].'</li>';
}
$catid=$row['cat_name'];
}
Hope that helps.
Doesn't make sense to me. When there is two topics with the same category, you will run into problems. Maybe I am not thinking correctly as its 4:47am but I can't think of how that would work.
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
Doesn't make sense to me. When there is two topics with the same category, you will run into problems. Maybe I am not thinking correctly as its 4:47am but I can't think of how that would work.
Well I have designed it so that if there are two topics or more topics in the one category, the category field will always contain the same category name as like an identifier and that is what the following if statement is all about:
if ($catid!==$row['catname']) {
cwarn23
Occupation: Genius
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259