Jump to content

Recommended Posts

Hi all,

 

I've created a message board for my website. I am trying to display next to the list of post titles the number of reviews that have been posted for that particular post title.

The query below selects all the post titles for a user selected category:

 

SELECT DISTINCT post_title FROM tablename

WHERE post_category = $user_selected_post_category

 

What I want to do next is within a for loop display the post_titles from the above query and next to the post_titles put the number of replies. In my database, each reply as a unique id called post_id. What I think is to COUNT all the post_ids for that post_title. The below query shows this:

 

SELECT COUNT(post_ids) AS no_of_posts FROM tablename

WHERE post_title = $post_title_from_above query.

 

How can I combine these 2 queries on one page to display '$post_title' and '$no_of_posts' within one for loop also on the same page.

The message board can be found at [a href=\"http://www.mediacritic.co.uk/messageboard2.php?post_category=Films\" target=\"_blank\"]http://www.mediacritic.co.uk/messageboard2..._category=Films[/a]

 

Thanks

 

Forge

Link to comment
https://forums.phpfreaks.com/topic/3078-sql-query-help-for-php-message-board/
Share on other sites

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] DISTINCT post_title, COUNT(post_ids) AS no_of_posts FROM tablename WHERE post_category = '$user_[span style=\'color:blue;font-weight:bold\']selected_post_category'[/span] GROUP BY post_title [!--sql2--][/div][!--sql3--]

Why not try the following (UNTESTED):

 

SELECT post_title, 
( SELECT COUNT(post_ids) FROM tablename WHERE post_title = t1.post_title ) AS no_of_posts 
FROM tablename WHERE post_category = $user_selected_post_category GROUP BY post_title

 

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.