tags:

views:

24

answers:

1

forumcats: id, name
forums: id, name, cat_id

How can i join these together and print the forums assigned to the categories under them Would really appriciate if someone could give me a hand

This is what i mean:

A category

A forumA forum


A 2nd category

Another forum

Another forum

Another forum


A 3rd catagory

Another forum

Another forum

Another forum

$reslt = mysql_query("select id, name, cat_id from forums");

while ($row=mysql_fetch_assoc($reslt)) {

 echo "<h1>Category here</h1<";
 echo "<h3>$row[name]</h3>";

}
A: 

You want this query:

SELECT forums.*, forumcats.name AS category_name
FROM forums
INNER JOIN forumcats ON (forums.cat_id = forumcats.cat_id)

Then when you loop through the results, you want to identify when you've moved on to a new category. For example:

$last_cat_id = null;
while ($row=mysql_fetch_assoc($reslt)) {
  if ($last_cat_id != $row['cat_id']) {
    echo '<h1>' . $row['category_name'] . '</h1>';
    $last_cat_id = $row['cat_id'];
  }
  echo "<h3>$row[name]</h3>";
}
VoteyDisciple