Counting problem
Posted: Sat Nov 06, 2004 10:43 am
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.
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.
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 "· <a href='cats.php?id=".$row['cat_id']."'>".$row['cat_name']."</a>(".$total.")<br />";
while ($row = mysql_fetch_array($result));
}Any help appreciated.