Page 1 of 1

Help with determining the percentage of values [Solved]

Posted: Tue Nov 11, 2003 6:45 pm
by infolock
Right now, I'm pulling information from a database. In this database is a column named PACKETS.

Right now, I'm sorting the table PER HOUR and posting the results of the amount of packets sent. However, I am also wanting to post the results of each query into a pie char, but in order to do that, I need to determine the overall percentage of bandwidth sent within each hour

In other words, let's say i have 5 different hours ( 1pm, 2pm, 3pm, 4pm, 5pm ).

between the hours 1pm and 2pm, 500000 packets are sent. between the hours 3pm and 4pm 30000 packets are sent, and between the hours of 4pm and 5pm 1000000 packets are sent.

What I would like to do , is say 30% of all packets sent was between the hours of 1pm and 2pm. 20% of all packets sent was between the horus 3pm and 4pm, and 50% of all packets sent was between the hours of 4pm and 5pm.

i know those percentages i just gave are incorrect, but it's just to give you an idea. perhaps there is a way to do this within my existing sql statement, or maybe i ahve to do it after the values are sorted out in my while loop.

Here is the code i'm using :

Code: Select all

<?php

   @mysql_pconnect ('localhost','username','password'); 
   mysql_select_db ('logparser'); 


   $sql = "select avg(packets), hour(time) FROM log group by hour(time) order by hour(time) desc"; 
   $result = mysql_query($sql) or die(MySQL_Error()); 
   $num_results = mysql_num_rows($result); 

   $row = mysql_fetch_assoc($result); 
   mysql_data_seek ($result, 0); 
   echo '<strong><p>'; 
   while ($row = mysql_fetch_assoc ($result)) 
   { 
      echo 'Time : '; 
      echo $row['hour(time)']; 
      echo '<br />'; 
      echo 'Average Packets : <br />'; 
      echo $row['avg(packets)']; 
      echo '<br /> <br />'; 
   } 
   echo '</strong></p>'; 
?>

currently, my output looks like this :

Code: Select all

Time : 13:00
Average Packets Sent :
44791.8481

Time : 12:00
Average Packets Sent :
45299.2857

Time : 11:00
Average Packets Sent :
146491.0714

Time : 10:00
Average Packets Sent :
16788.0000

Time : 09:00
Average Packets Sent :
349418.0000

Time : 07:00
Average Packets Sent :
95160.0000

Time : 06:00
Average Packets Sent :
20644.0882

Time : 05:00
Average Packets Sent :
24776.0968

Time : 04:00
Average Packets Sent :
36942.5625

I posted this here because i'm sure if it's a php mathematical formula that i need to write, or if it's just a mysql query i need to put.

If you'd rather not write out the answer and just have links I could look at, i'd appreciate that more then the answer because it helps me to learn php when i do it on my own ( well, after a little kick to get me started with some functions to peek at :P )

Thanks 8O

Posted: Tue Nov 11, 2003 8:16 pm
by DuFF
Heres an example:

You have 4 numbers

300 packets
400 packets
500 packets
600 packets

The first step is to add all the numbers up.
300+400+500+600 = 1800
Then divide the the number you want to find the percentage of (example: 300) by the overall number (1800) and multiply by 100.
100(300/1800) = 16.667
So 300 is 16.667% of 1800. Repeat with the rest.
Hope that helps a bit.

Posted: Tue Nov 11, 2003 8:26 pm
by infolock
THANKS :)