stevesimo Posted March 20, 2008 Share Posted March 20, 2008 Hi, I have a table which contains categories. These categories are output so the user can select which one they want to view. What I want to do is count how many records there are for each category and output this next to the category name. For example cars(5), bikes(3) etc. Whilst I have my code working which outputs the category, I am not sure how to use the SQL count operator. Can anyone offer any advice on the best way to achieve this. Here is my code: $query = "Select * From Categories Order By categoryTitle"; $result = mysql_query($query); echo '<table>'; echo '<tr>'; echo '<td width="150">'; while ($row = mysql_fetch_array($result)){//loop through dropdown row by row $categoryID = $row['categoryID']; $categoryTitle = $row['categoryTitle']; $count = $count + 1; echo "<a href='search.php?area=".$id."&category=".$categoryID."'>".$categoryTitle."</a><br>"; }//end of loop [code] thanks, Steve [/code] Quote Link to comment Share on other sites More sharing options...
huhn_m Posted March 20, 2008 Share Posted March 20, 2008 you can count the number of result lines of a MySQL Query with the folowing Syntax: "SELECT COUNT(*) FROM my_table WHERE Category=cat_to_count" (you can also replace the asteriks (*) with only one column name (like ID) but I'm not sure if it gets faster because of this. Quote Link to comment Share on other sites More sharing options...
stevesimo Posted March 20, 2008 Author Share Posted March 20, 2008 Thanks for your help, I have got it working now. Steve Quote Link to comment Share on other sites More sharing options...
conker87 Posted March 20, 2008 Share Posted March 20, 2008 $count++ does the same as your $count = $count + 1; Just a heads up Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.