Jump to content

Showing most active forum


doddsey_65

Recommended Posts

How would i get it so i can show the most active forum the user has posts in. I have a posts table which lists the posts aswell as the forum they are in by id. So how would i select all of the posts with their username(easy) but then from these results calculate which id shows up the most so i can echo that?

Link to comment
Share on other sites

Not knowing the exact table and field names, this is just an example:

 

$query = "SELECT u.username, u.userid,
                 f.forumname, COUNT(f.forumid) as forumcount
          FROM users u
          JOIN posts p ON u.userid = p.userid
          JOIN forums f on p.forumid = f.forumid
          GROUP BY f.forumid
          WHERE u.userid = $userID";

Link to comment
Share on other sites

hopefully you will be kind enough to add my tables into that code as i have never used joins before.

 

table:

 

forum_posts

 

rows

 

forum_posts.forumid -> the id of the forum so i know which forum the post belongs to

forum_posts.postedby -> holds the username

Link to comment
Share on other sites

hopefully you will be kind enough to add my tables into that code as i have never used joins before.

 

table:

 

forum_posts

 

rows

 

forum_posts.forumid -> the id of the forum so i know which forum the post belongs to

forum_posts.postedby -> holds the username

 

I'm not too sure what he was doing with that code, but I think i can help you :). so the forumid would be like. well for instance:

 

Forum name = Php Test

Forum id = 1

 

Forum name = Php Test #2

Forum id = 2

 

etc. however you have yours.

 

and then in the database if the user, John let's say, posts in Forum "Php Test", then "Php Test #2", then "Php Test"

the database would look like:

 

forumid---+----postedby

1----------+-------John

2----------+-------John

1----------+-------John

 

is that correct?

Link to comment
Share on other sites

Can't do it with what you provided. I assumed there are three tables: users, posts and forums. You stated the posts table included an id to the forum.

 

Just replace "users", "posts" and "forums" with the respective table names. Don't forget to change the field names to their respective values as well.

 

Red = table names, blue = field names

$query = "SELECT u.username, u.userid,

                f.forumname, COUNT(f.forumid) as forumcount

          FROM users u

          JOIN posts p ON u.userid = p.userid

          JOIN forums f on p.forumid = f.forumid

          GROUP BY f.forumid

          WHERE u.userid = $userID";

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.