Jump to content

Counting Array Occurances


tomtimms

Recommended Posts

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.

Link to comment
Share on other sites

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]++;
    }
}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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]++;
    }
    

    
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.