Jump to content

LEFT JOIN problem


Aureole
 Share

Recommended Posts

Ok say there is a category called "Test Category" and it has two children forums. I want it to show like this:

Test CategoryForum 1Forum 2
But it is showing like this:
Test CategoryForum 1Test CategoryForum 2
My query is like...:
<?php$query ="SELECT * FROM forums LEFT JOIN categories ON forums.forum_parent_id=categories.cat_id";$result = mysql_query($query) or die(mysql_error());while($row = mysql_fetch_assoc($result)) {	$cid = $row['cat_id'];	$cname = $row['cat_name'];	$fname = $row['forum_name'];	$fdesc = $row['forum_description'];// .... etc...?>

To see exactly what I mean go here.Anyone know how I can fix this?

Link to comment
Share on other sites

I see no issues on the site itself. Also, side note about your SQL: you can do things like $query ="SELECT f.*,c.cat_id,c.cat_name FROM forums f LEFT JOIN categories c ON f.forum_parent_id=c.cat_id";Think of the "forums f" syntax as Your name is forums but i will give you the nickname of f. From then on you can use f. and forums. interchangably.

Link to comment
Share on other sites

It's actually a little more efficient to use the AS keyword. When you just do something like "FROM table t" it will make a copy of the table and assign it the new name. When you use AS it just uses the new name as an alias to the existing table. For large tables it can strain the database to make a copy of it.For your category issue, have a variable keep track of the last parent category that was displayed. If the current subcategory has the same parent as what is in the variable, don't show the parent. If they are different, update the variable with the new category and then show the parent before showing the child.

Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...