View Single Post
Old 03-26-2006, 01:46 AM   #1 (permalink)
bayruni
Registered User
 
Join Date: Mar 2006
Posts: 9
bayruni is on a distinguished road
Question Counting and displaying total articles in specific category

I need help learning how I can display the total number of articles in each category, I need something sort of like this:
  • General (11)
  • Tutorials (5)
  • Downloads (7)

Info:
Two tables: np_articles and np_categories

np_articles has many columns, the most important for this topic are article_id and article_category_id

np_categories only has three columns: category_id, category_name, and category_description.

On my current page, I have a list of my categories. I use the following query: $query_rsCategories = "SELECT * FROM np_categories ORDER BY category_name ASC";

I have had no problems with this query and have used it often. It lives at the very top of my document.

To display a list of the categories, I use something similar to this,
Code:
<p>Total Categories: <?php echo $totalRows_rsCategories ?></p>
   <?php do { ?>
	  <p><?php echo $row_rsCategories['category_name']; ?></p>
   <?php } while ($row_rsCategories = mysql_fetch_assoc($rsCategories)); ?>
I tried adding this query: $query_rsCountArticles = "SELECT COUNT(np_articles.article_id) AS articlecount, np_categories.category_name FROM np_articles LEFT JOIN np_categories ON np_articles.article_category_id = np_categories.category_id GROUP BY np_categories.category_name"; and adding this line:
Code:
<?php echo $row_rsCategories['articlecount'];?>
in brackets after my category_name bit (shown above), but no luck.

*sigh*. Help me. anyone. I've searched other forums, but haven't found a working solution yet.
bayruni is offline   Reply With Quote