tomtimms Posted May 13, 2010 Share Posted May 13, 2010 I am trying to count how many times a certain result is returned in my while loop. My query is "SELECT * FROM accounts WHERE status > 1" I use a while loop to display my data and the data when parsed looks like this. Company 1 - Status 2 - Green Company 2 - Status 2 - Yellow Company 3 - Status 3 - Blue Company 4 - Status 3 - Orange Company 4 - Status 2 - Black Status 2 occurs ? Status 3 occurs ? I want to count how many times Status 2 occurs and how many times status 3 occurs. Is this possibly with my Query? Right now I am using 2 queries and changes my where clause to "WHERE status =2 and status =3" I want to only use 1 query for this. Quote Link to comment https://forums.phpfreaks.com/topic/201669-counting-array-occurances/ Share on other sites More sharing options...
Mchl Posted May 13, 2010 Share Posted May 13, 2010 SELECT status, COUNT(*) FROM accounts WHERE status > 1 GROUP BY status Quote Link to comment https://forums.phpfreaks.com/topic/201669-counting-array-occurances/#findComment-1057908 Share on other sites More sharing options...
Psycho Posted May 13, 2010 Share Posted May 13, 2010 This will create an array with the keys as the status description and the values as the count $statusCount = array(); while($row = mysql_fetch_assoc($result)) { //Code to display the record $status = $row['status']; //Add to the status count if(!isset($statusCount[$status])) { $statusCount[$status] = 1; } else { $statusCount[$status]++; } } Quote Link to comment https://forums.phpfreaks.com/topic/201669-counting-array-occurances/#findComment-1057910 Share on other sites More sharing options...
tomtimms Posted May 13, 2010 Author Share Posted May 13, 2010 This will create an array with the keys as the status description and the values as the count $statusCount = array(); while($row = mysql_fetch_assoc($result)) { //Code to display the record $status = $row['status']; //Add to the status count if(!isset($statusCount[$status])) { $statusCount[$status] = 1; } else { $statusCount[$status]++; } } thanks for the response, what would I echo out to get the count of how many times status 2 occured? Quote Link to comment https://forums.phpfreaks.com/topic/201669-counting-array-occurances/#findComment-1057917 Share on other sites More sharing options...
Psycho Posted May 13, 2010 Share Posted May 13, 2010 echo $statusCount['status 2']; Quote Link to comment https://forums.phpfreaks.com/topic/201669-counting-array-occurances/#findComment-1057924 Share on other sites More sharing options...
Mchl Posted May 13, 2010 Share Posted May 13, 2010 echo $statusCount['status 2']; echo $statusCount['Status 2']; It'll be case sensitive Quote Link to comment https://forums.phpfreaks.com/topic/201669-counting-array-occurances/#findComment-1057926 Share on other sites More sharing options...
tomtimms Posted May 13, 2010 Author Share Posted May 13, 2010 not sure what I am doing wrong, here is the compiled code. Nothing is displaying. $sql = "SELECT status, COUNT(*) FROM accounts WHERE status > 0 GROUP BY status"; $result = mysql_query($sql); $statusCount = array(); while($row = mysql_fetch_assoc($result)) { //Code to display the record $status = $row['status']; //Add to the status count if(!isset($statusCount[$status])) { $statusCount[$status] = 1; echo $statusCount['Status 1']; } else { $statusCount[$status]++; } } Quote Link to comment https://forums.phpfreaks.com/topic/201669-counting-array-occurances/#findComment-1057940 Share on other sites More sharing options...
Mchl Posted May 13, 2010 Share Posted May 13, 2010 You basically compiled our two solutions into one, while mjdamato's would work with your previous code. Quote Link to comment https://forums.phpfreaks.com/topic/201669-counting-array-occurances/#findComment-1057953 Share on other sites More sharing options...
iblood Posted May 13, 2010 Share Posted May 13, 2010 You basically compiled our two solutions into one, while mjdamato's would work with your previous code. yeah!! put this-> echo $statusCount['Status 1']; out of the loop Quote Link to comment https://forums.phpfreaks.com/topic/201669-counting-array-occurances/#findComment-1057957 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.