I’m generating a list of categories, each with one or more subcategories (topics). There are currently two tables- one of categories, and one of subcategories (which has a key to the categories table).
The code I have (posted below) technically works, but it runs a query for every category that exists, which seems inefficient.
Does anybody know a more effective way to get the same results, perhaps with only a single query, or different database structure?
Thanks in advance,
Nick
[code=php]
function BuildTopicList()
{
$Q = “SELECT CategoryName, CategoryID
FROM Categories C
WHERE 1 = 1;”;
$R = SafeQuery($Q);
while($W = mysql_fetch_array($R))
{
$HTML .= SmallColumnCodeIndent .
“<span class=”Category”>” .
$W[‘CategoryName’] .
“</span>”;
$Q1 = “SELECT TopicName, TopicID, CategoryID
FROM Topics T
WHERE T.CategoryID = “” . $W[‘CategoryID’] . “”;”;
$R1 = SafeQuery($Q1);
while($W1 = mysql_fetch_array($R1))
{
// It’s a topic
$HTML .= SmallColumnCodeIndent .
“<a class=”” . TopicAClass . “” href=”#” .
$W1[‘TopicID’] . “”>” .
$W1[‘TopicName’] . “</a>nr”;
}
}
return $HTML;
}