Jump to content

SELECT COUNT


honkmaster
 Share

Recommended Posts

Hi, I'm quite new to PHP so sorry if this is basic.I have a MySQL database with a table in called status. There is 6 status possibilitiesSTATUSIn ProgressOrder ReceivedWaiting for ArtworkWaiting for ApprovalCompleteOn HoldSo far this is what I have got, it counts the status and groups them and presents back as screen shot attached (Fig1) This is great but what I want it to do is present results in a table format like screen shot attached (Fig2). Where there is no result I would like a "0"Fig 1Fig 2Can anyone point me in the right direction

<?php$username="XXXX";$password="XXXX";$database="XXXX"; mysql_connect(localhost,$username,$password);@mysql_select_db($database) or die( "Unable to select database"); $query = "SELECT status, COUNT(Status) FROM main_data GROUP BY status";     $result = mysql_query($query) or die(mysql_error()); // Print out resultwhile($row = mysql_fetch_array($result)){    echo "There are ". $row['COUNT(Status)'] ." Records at ". $row['status'] ." Status.";    echo "<br />";}?>

Link to comment
Share on other sites

It's not going to return 0 for a missing status, so you need to figure out which ones are missing from the result. The easiest way is to store your counts in an array, then loop through the results and fill out the array, then loop through the array to print the table. If you start the array with all of the elements set to 0, then fill in anything from the result, you'll be left with an array that has a number filled in for anything that was in the result, and a 0 for everything else.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...