V Posted June 16, 2010 Share Posted June 16, 2010 Well, I'm sure it's cake for you. I'm just trying to display the total number of posts under a category. But for example instead of 4 I get 1111. My code is, ////////////////Category Query $category_table = mysql_query("SELECT * FROM categories", $connection); if (!$category_table) { die("Database query failed: " .mysql_error()); } while ($row = mysql_fetch_array($category_table)) { $cat_id=$row['car_id']; $cat_name=$row['cat_name']; echo "<a href=\"single_category.php?cat=" . urlencode($cat_id) . "\"> $cat_name</a><br />"; ////////////////Total Posts Query $total_posts = mysql_query("SELECT post_title FROM posts WHERE cat_id = {$row["cat_id"]}", $connection); if (!$total_posts) { die("Database query failed: " .mysql_error()); } while ($row = mysql_fetch_array($total_posts)) { $posts_nr = count($row['post_title']); echo $posts_nr; }//total_posts loop }//topics BTW, I'm sorry for posting so many questions these past 2 days.. PHP is fun Link to comment https://forums.phpfreaks.com/topic/204994-simple-count-issue/ Share on other sites More sharing options...
Alex Posted June 16, 2010 Share Posted June 16, 2010 $total_posts = mysql_query("SELECT COUNT(post_title) as total_posts FROM posts WHERE cat_id = {$row["cat_id"]}", $connection); $row = mysql_fetch_assoc($total_posts); echo $row['total_posts']; Link to comment https://forums.phpfreaks.com/topic/204994-simple-count-issue/#findComment-1073175 Share on other sites More sharing options...
V Posted June 16, 2010 Author Share Posted June 16, 2010 Ooh I see! Associative arrays and no loop. Brilliant! Thanks, I'll keep that in mind. Link to comment https://forums.phpfreaks.com/topic/204994-simple-count-issue/#findComment-1073177 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.