Jump to content

query problems


doddsey_65

Recommended Posts

im trying to group all forums into their respective categories like so:

 

category 1

  forum 1

  forum 2

 

category 2

  forum 3

  forum 4

 

but im having problems with the query:

 

$forum_query = mysqli_query($link, "SELECT cid, c_name, 
                        group_concat(f_name seperator ', ') forums
                        FROM ".TBL_PREFIX."categories
                        LEFT JOIN ".TBL_PREFIX."forums 
                        using (cid)
                        group by ".TBL_PREFIX."categories")
                        or die(mysqli_error($link));

while($forum_info = mysqli_fetch_array($forum_query, MYSQLI_ASSOC))
{
    echo $forum_info['cat_name']."<br>";
    echo $forum_info['forums']."<br>"; 
}

 

i get an sql error before seperator.

 

the database tables are as follows(btw TBL_PREFIX is "asf_")

 

asf_categories

cid    c_name

 

asf_forums

fid    cid    f_name

 

so to summarise im trying to group all foums with cid of 1 into cateogry with cid of 1 and so on.

 

Any pointers?

Link to comment
Share on other sites

thanks for the replies. how would i go about not including anything from forums table where the pid != 0?

i tried adding a where clause but it didnt show any categories.

also is this the best way to do this? at the minute for tests im only pulling f_name but i will be wanting to pull alot more from the forum table later. heres my revised code:

 

$forum_query = mysqli_query($link, "SELECT cid, c_name, 
                        group_concat(f_name separator '<br />') forums
                        FROM ".TBL_PREFIX."categories
                        LEFT JOIN ".TBL_PREFIX."forums 
                        USING (cid)
                        GROUP BY ".TBL_PREFIX."categories.cid")
                        or die(mysqli_error($link));

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.