I'm having problems trying to query the total number of topics in each of my forums.

Basically, I am getting the same number for each forum. I only have 2 forums in the database. For testing purposes, I have 1 topic in one and 0 in the other but I am getting 1 for each forum.

Here is my query:

$query_topics = "SELECT topic_id, count(*) FROM topics JOIN forums ON (topics.topic_forum = forums.forum_id) GROUP BY topic_id";
$topics = mysql_query ($query_topics) OR die ('Cannot execute the query.');
$row_topics = mysql_fetch_assoc ($topics);
$total_topics = mysql_num_rows ($topics);

Here is my PHP for the table:

<table width="725" border="0" cellspacing="1" cellpadding="6" bgcolor="#CCCCCC">
 <tr bgcolor="#E5E5E5">
  <td width="525"><strong>Forum</strong></td>
  <td width="100"><div align="center"><strong>Topics</strong></div></td>
  <td width="100"><div align="center"><strong>Posts</strong></div></td>
 </tr>
 <?php do { ?>
 <tr bgcolor="#FFFFFF">
   <td width="525"><a href="view_forum.php?forum_id=<?php echo $row_forums['forum_id']; ?>"><?php echo $row_forums['forum_name']; ?></a></td>
   <td width="100"><div align="center"><?php echo $total_topics ?></div></td>
   <td width="100"><div align="center"></div></td>
  </tr>
  <?php } while ($row_forums = mysql_fetch_assoc ($forums)); ?>
</table>

Any help is appreciated. Many thanks!

Recommended Answers

All 2 Replies

Hi Eiolin

I know a good resource of solving these types of issues.
<url snipped>

Hope it's helpful to you!

[wilkin]

I don't think advertisements were what he was looking for... and that site has to be the most ridiculous I have seen in a long time! Thanks for the laugh anyway! :)

Are you trying to find count against each topic or each forum.

If agianst each forum, then perhaps your query should be:

SELECT 
	forum_id, 
	count(*) 
FROM 
	topics 
LEFT JOIN forums 
	ON (topics.topic_forum = forums.forum_id) 
GROUP BY forum_id
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.