I was wondering if someone could help me simplify my current counting method. I´m trying to learn more in PHP/MySQL so please be positive :)
I have a table with multiple articles that are categorized into sections. What I would like to do is count the number of articles in each section and be able to call that value to be displayed on my web page.
This is my current solution (simplified version without extra query filters) but I would think it fairly resource heavy for a simple task:
$query = mysql_query("SELECT category FROM table");
$list = "";
while($item = mysql_fetch_array($query) ) {
$list = $list.",".$item['category'];
}
// Then to count how many results for each category:
$categoryA = substr_count($list, 'Category A');
$categoryB = substr_count($list, 'Category B');
...
I have about 20 categories that are counted in this manner.