Jump to content

[SOLVED] Counting results in an array to make a percentage....


Recommended Posts

I've just switched over to mySQL and I have a simple query that returns values:

 

<html>


<?php

$link = mysql_connect("localhost:3306", "", "")
          or die ("Couldn't connec");

$db = "reporting";
mysql_select_db($db) or die("Could not select the database '" . $db . "'.  Are you sure it exists?");


$query = "SELECT COUNT(Incident_Id) as IncidentId, Source FROM hd_reporting WHERE (Submitter = 'Me') GROUP BY Source";  //GET SOURCE OF TICKETS (ALL)

$query2 = "SELECT Incident_Id FROM hd_reporting WHERE (Submitter = 'Me')"; //GET TOTAL TICKETS

$result = mysql_query($query) or die("Query Failed");
$result2 = mysql_query($query2) or die("Query 2 Failed");


$tickettotal = mysql_num_rows($result2);


$results_array = array();


?>
<b>Tickets by source</b><br>
<table><tr><td><u># of Tickets</td><td><u>Source</td></tr>
<?php


while($row = mysql_fetch_assoc($result))

{
echo "<tr><td>{$row['IncidentId']}</td><td>{$row['Source']}</td></tr>";
}


?>
</table><br><br>

<?php

echo "Total number of tickets: $tickettotal";

?>

 

My result is:

 

# of Tickets  |  Source

-------------------------------

        1        |      E-Mail

      25        |      Phone

 

So far so good.  Now what I'd like to do is get a percentage next to the numbers.  I've already got the ticket total as $tickettotal.  So I thought I could do:

 

$percentage = number_format(mysql_num_rows($result) / $tickettotal * 100);

 

The problem is, of course, the number of rows that get returned is 2.  How can I have this count on the fly and do the percentage?  I'm thinking it needs to pull from the array, but I'm not sure.  The final output I'd like would be:

 

# of Tickets      |  Source

-------------------------------

        1  (4%)      |      E-Mail

      25  (96%)    |      Phone

 

 

Can anyone help?

I also tried...

 

$results_array = array();

while(mysql_fetch_array($result, $a))
{
$results_array[$a[1]] = $a[0];               
}
?>
<table><tr><td><u># of Tickets</td><td><u>Source</td></tr>
<?php
foreach ($results_array as $gp => $count)
{
    $total=array_sum($results_array);  //total # of tickets 
    $per = number_format($count / $total * 100);
    echo "<tr><td>$count ($per%)</td><td>$gp</td></tr>";
}

?>

 

 

But I seem to just get an empty result.  Am I doing something wrong here?

What about this?

<?php
  while($row = mysql_fetch_assoc($result)){
    $per = round(row['IncidentId'] / $tickettotal * 100);
    echo "<tr><td>{$row['IncidentId']} ({$per}%)</td><td>{$row['Source']}</td></tr>";
  }
?>

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.