문제

<?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.

도움이 되었습니까?

해결책

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.

다른 팁

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;
 }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top