Page 1 of 1

Percentage Between Two Arrays

Posted: Wed Jun 18, 2008 10:58 am
by noctorum
I have one portion of code that returns the total amount for a requested query (how many calls for the timespan), and one portion that returns the total amount of a subset of that (voicemails). I'm not sure how to get a percentage of those however, i.e.

Total incoming calls is 10, there 2 voicemails, so return 20% (and repeat this process for each day requested).

The two portions of code here;

Code: Select all

<?php
                        $getDistinctDaysForIn = mysql_query("SELECT DISTINCT Date
                                                        FROM phonelog2
                                                        WHERE Date>='$start_date'
                                                        AND Date<='$end_date'
                                                        ");
                        
 
                        while ($row = mysql_fetch_array($getDistinctDaysForIn))
                            {
                            $countTotalIn = mysql_query("SELECT COUNT( * ) 
                                                            FROM phonelog2
                                                            WHERE `Date`='{$row['Date']}'
                                                            AND Line<>1
                                                            AND Line<>2
                                                            AND Line<>3
                                                            AND Line<>4
                                                            AND time>='$hours1' 
                                                            AND time<='$hours2'
                                                            AND In_Out='I'
                                                            ");
                                                
                                while ($row = mysql_fetch_array($countTotalIn))
                                {
                                echo "<tr align=center>";
                                echo "<td>";
                                echo $row[0];
                                echo "</td>";
                                echo "</tr>";
                                }
                                            
                            }
?>

Code: Select all

<?php
                        $getDistinctDaysForVmail = mysql_query("SELECT DISTINCT Date
                                                        FROM phonelog2
                                                        WHERE Date>='$start_date'
                                                        AND Date<='$end_date'
                                                        ");
                        
 
                        while ($row = mysql_fetch_array($getDistinctDaysForVmail))
                            {
                            $countVmailTotal = mysql_query("SELECT COUNT( * ) 
                                                            FROM phonelog2
                                                            WHERE `Date`='{$row['Date']}'
                                                            AND Line<>1
                                                            AND Line<>2
                                                            AND Line<>3
                                                            AND Line<>4
                                                            AND time>='$hours1' 
                                                            AND time<='$hours2'
                                                            AND (Station=20
                                                            OR Station=21
                                                            OR Station=22
                                                            OR Station=23)
                                                            ");
                                                
                                while ($row = mysql_fetch_array($countVmailTotal))
                                {
                                echo "<tr align=center>";
                                echo "<td>";
                                echo $row[0];
                                echo "</td>";
                                echo "</tr>";
                                }
                                            
                            }
?>
Any pointers here?

Thanks

Re: Percentage Between Two Arrays

Posted: Wed Jun 18, 2008 12:14 pm
by WebbieDave
So the first script gets the total number of calls and the second gets the total number of voicemails? And you want to calculate the percentage? If so, it seems you have all the pertinent data you need to perform the calculation.

Where exactly are you running into problems?

Re: Percentage Between Two Arrays

Posted: Wed Jun 18, 2008 12:41 pm
by noctorum
WebbieDave wrote:So the first script gets the total number of calls and the second gets the total number of voicemails? And you want to calculate the percentage? If so, it seems you have all the pertinent data you need to perform the calculation.

Where exactly are you running into problems?
Yes, thats correct. I'm not sure how to properly form the loop so that it will sync properly and the right syntax for division with arrays.

Re: Percentage Between Two Arrays

Posted: Wed Jun 18, 2008 2:36 pm
by WebbieDave
Well, in that case you'll need to familiarize yourself with the basics of PHP looping structures (for, foreach, while, do-while):
http://us.php.net/manual/en/language.co ... ctures.php

As far as math with array elements, it's not different than with regular old scalars:

Code: Select all

$quotient = $arr['dividend']/$arr['divisor'];

Re: Percentage Between Two Arrays

Posted: Wed Jun 18, 2008 2:47 pm
by noctorum
WebbieDave wrote:Well, in that case you'll need to familiarize yourself with the basics of PHP looping structures (for, foreach, while, do-while):
http://us.php.net/manual/en/language.co ... ctures.php

As far as math with array elements, it's not different than with regular old scalars:

Code: Select all

$quotient = $arr['dividend']/$arr['divisor'];
Well the issue that I had (besides the division) is that I understand the loops, but I'm not sure how to fetch the array for two queries and loop them simultaneously.

Code: Select all

 
                                while ($row = mysql_fetch_array(can't fit two queries here))
                                {
                                echo "<tr align=center>";
                                echo "<td>";
                                echo $row[0];
                                echo "</td>";
                                echo "</tr>";
                                }
 

Re: Percentage Between Two Arrays

Posted: Wed Jun 18, 2008 3:42 pm
by noctorum
Got it.

Here it is for posterity (the return value hasn't been cleaned or anything);

Code: Select all

<?php
                        $getDistinctDays = mysql_query("SELECT DISTINCT Date
                                                        FROM phonelog2
                                                        WHERE Date>='$start_date'
                                                        AND Date<='$end_date'
                                                        ");
                        
 
                        while ($row = mysql_fetch_array($getDistinctDays))
                            {
                            $countVmailTotal = mysql_query("SELECT COUNT( * ) 
                                                            FROM phonelog2
                                                            WHERE `Date`='{$row['Date']}'
                                                            AND Line<>1
                                                            AND Line<>2
                                                            AND Line<>3
                                                            AND Line<>4
                                                            AND time>='$hours1' 
                                                            AND time<='$hours2'
                                                            AND (Station=20
                                                            OR Station=21
                                                            OR Station=22
                                                            OR Station=23)
                                                            ");
                                                            
                            $countTotalIn = mysql_query("SELECT COUNT( * ) 
                                                            FROM phonelog2
                                                            WHERE `Date`='{$row['Date']}'
                                                            AND Line<>1
                                                            AND Line<>2
                                                            AND Line<>3
                                                            AND Line<>4
                                                            AND time>='$hours1' 
                                                            AND time<='$hours2'
                                                            AND In_Out='I'
                                                            ");                         
                                                            
                                while ($row = mysql_fetch_array($countVmailTotal))
                                {
                                    while ($row2 = mysql_fetch_array($countTotalIn))
                                        {
                                            $vmailPercent = $row[0]/$row2[0];
                                            echo "<tr align=center>";
                                            echo "<td>";
                                            echo $vmailPercent;
                                            echo "</td>";
                                            echo "</tr>";                                           
                                        }
                                }
                            }
 
 
?>