Using count(*) beside elements

I would like to add some filtering controls on my homepage so they look like this:

  • Queue (1)
  • Development (2)
  • Underway (8)
  • Complete (1)

I have working so far:

  • Print the category name with ()
  • Successfully loop through the first element and print the correct value

It seems to fail after the second element? If I am to get an error I would have expected to reach my trigger error statement but I am not?

Notice: Undefined offset: 1 in C:\wamp\www\ab-tests\index.php on line 36


        <?php
        $fArr = array('Queue','Development','Underway','Complete');
        for ($i = 0; $i < count($fArr); $i++)
        {
            $fSQL = @mysql_query("select count(*) as tally from experiments where status = '$fArr[$i]' group by status");
            if ($fSQL)
            {
                $fCount = mysql_fetch_array($fSQL);
                echo "<p>" . $fArr[$i] . " (" . $fCount[$i] . ")" . "</p>";        
            }
            else
            {
                trigger_error("<p>Unable to show filter counter at this time.</p>");
            }
            
        }
        ?>

Could anybody advise?

You would probably be better off collecting all the counts in one go from the database; here’s a quick example. :slight_smile:


<?php
$res = mysql_query('SELECT COUNT(*) AS count, status FROM experiments GROUP BY status;');
while($row = mysql_fetch_assoc($res)){
  printf('<p>%s (%d)</p>', $row['status'], $res['count']);
}

Whoa that works a treat!!

Thank you so much for a quick reply, I am pretty new to this.

How did you come up with that solution?