Percentage Between Two Arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
noctorum
Forum Commoner
Posts: 31
Joined: Fri Jun 13, 2008 10:46 am

Percentage Between Two Arrays

Post 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
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: Percentage Between Two Arrays

Post 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?
noctorum
Forum Commoner
Posts: 31
Joined: Fri Jun 13, 2008 10:46 am

Re: Percentage Between Two Arrays

Post 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.
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: Percentage Between Two Arrays

Post 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'];
noctorum
Forum Commoner
Posts: 31
Joined: Fri Jun 13, 2008 10:46 am

Re: Percentage Between Two Arrays

Post 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>";
                                }
 
noctorum
Forum Commoner
Posts: 31
Joined: Fri Jun 13, 2008 10:46 am

Re: Percentage Between Two Arrays

Post 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>";                                           
                                        }
                                }
                            }
 
 
?>
Post Reply