Counting problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Counting problem

Post by therat »

I have the following code that list all the categories I have which works fine. I now want to count how many items belong to each category and show that after the categories name.

Code: Select all

function category() {
        global $skinz;
        $sql = "SELECT COUNT(cat_id) AS total FROM skinz_images WHERE status='1' ORDER BY cat_id";
        $result = mysql_query($sql, $skinz) or die(mysql_error());
        $row = mysql_fetch_assoc($result);
        $total = $row['total'];
        
		$query = "SELECT * FROM skinz_categories WHERE status='1' ORDER BY cat_name ASC";
		$result = mysql_query($query, $skinz) or die(mysql_error());
		$row = mysql_fetch_assoc($result);
	do
		echo "&middot;&nbsp;<a href='cats.php?id=".$row['cat_id']."'>".$row['cat_name']."</a>(".$total.")<br />";
	while ($row = mysql_fetch_array($result));
	}
The counting part is the problem. It currently show that there are 5 items in each category when there shouldnt be. I currently have 3 categories, two of which are empty and the last has 5 items in so it should show cat1(0), cat2(0), cat3(5) etc.
Any help appreciated.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Post by therat »

Thanks, I'll take a look at that.
Post Reply