Question

<?php
    $connection = mysql_connect('###', '###', '###');
    mysql_select_db('msg360db1');
    $sql = "SELECT * FROM call_log where Pod = 'KNF'";                                                              
        $result = mysql_query($sql);
        echo 
        "<table cellspacing='0' cellpadding='0'>                                
            ";                                  
            while($row = mysql_fetch_array($result))
            {   
                echo "                                      
                <tr>                                        
                    <td style='margin-right:10px;'>" . $row['Rep'] . "</td>
                    <td style='margin-right:10px;'>" . $row['EWD'] . "</td>
                    <td style='margin-right:10px;'>" . $row['CalltimeThisWeek'] . "</td>
                    <td style='margin-right:10px;'></td>
                    <td style='margin-right:10px;'>" . $row['CallsThisWeek'] . "</td>
                    <td style='margin-right:10px;'></td>
                    <td style='margin-right:10px;'>" . $row['CalltimeToday'] . "</td>
                    <td style='margin-right:10px;'>" . $row['CallsToday'] . "</td>                                      
                </tr>
                ";
            }

            echo "
        </table>";
    mysql_close();
?>

<?php
    $row['CalltimeThisWeek'];
    $row['EWD'];
    $number = ($row['EWD'] / $row['CalltimeThisWeek']);
    echo 'Result: '.$number;
?>

I have connected to my database and successfully created a table with the information in it, however, i am wanting to divide call time this week by the EWD which is a value between 1 and 7, these values are stored in my database.

Was it helpful?

Solution

Try this:

if($row['CalltimeThisWeek']==0){
   $number = 0;
} else {
   $number = ($row['EWD'] / $row['CalltimeThisWeek']);
}

These aren't necessary after the loop:

$row['CalltimeThisWeek'];

$row['EWD'];

Also, you need to convert your timestamps to divisible integers: http://php.net/manual/en/function.mktime.php

Don't forget: You need to then place your $number math and echo statements within the while loop, otherwise it will only use the last number it gets in the variable and produce one result.

OTHER TIPS

you are trying to use $row array outside the scope. Explain a bit more do you need to do the division for every row or sum them all up first and do the division?

You can't divide by zero ; and that's the error you got,

Replace this

  <?php
        $row['CalltimeThisWeek'];
        $row['EWD'];
        $number = ($row['EWD'] / $row['CalltimeThisWeek']);
        echo 'Result: '.$number;
    ?>

by this:

   <?php
        $number=0;
        if($row['CalltimeThisWeek']!=0) $number = $row['EWD'] / $row['CalltimeThisWeek']);
        echo 'Result: '.$number;
    ?>

You need to check $row['EWD'] and $row['CalltimeThisWeek'] for 0 or negative values

 if ($row['EWD'] >= 1 && $row['CalltimeThisWeek'] >= 1) { 
    $number = ($row['EWD'] / $row['CalltimeThisWeek']);  
 } else {
    $number = 0;
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top