-2

I am trying to select a column from a table, not only display each row with its value but also display the total of the column according to the query.
As shown in the code provided below, each row is displayed perfectly, however, when comes to the total of the column, only the value of the first called row is displayed as a total.
Please look at the picture provided below. Only the first row selected with value of 25:18 is displayed as a total.
The expected total value at the bottom of the table should be 51:19+50:41+25:18, but the total again is just the first row (DESC). Please advise

table display after selected from sql

if (($user != "") && ($date !="")){
    $mysql=("SELECT inet_ntoa(conv(hex(location_ip), 16, 10)) as ip, conv(hex(idvisitor), 16, 10) as visitorId, 
    visit_last_action_time, visit_first_action_time, visitor_localtime,  user_id, visit_total_time
    FROM piwik_log_visit WHERE user_id='".$user."' AND date(visit_first_action_time) ='".$date."'
    Order by visit_first_action_time DESC");}

$query= mysqli_query($conn, $mysql) or die ("SQL Error: ".mysqli_error($conn));
while ($row = mysqli_fetch_array($query))
        {
            $time =date('F d, Y | h:i:s a', strtotime($row['visitor_localtime']));
            $time =date('h:i:s a', strtotime($row['visitor_localtime']));
            $idaction_url = $row['idaction_url'];
            $ip =$row['ip'];
            $visitor = $row['visitorId'];

            $first = date('F d, Y |', strtotime($row['visit_first_action_time']));
            $date = $first.' '.$time;

            ///total time added
            $sum = array($row['visit_total_time']);
            $sum_total = array_sum($sum);
            $sum_time = gmdate('H:i:s', $sum_total);    

            $total = gmdate('i:s', $row['total_time']); 
9

2 Answers 2

0

You could try

$sum_total = 0;
foreach($sum as $key=>$value)
{
   $sum_total+= $value;
}
echo $sum_total;
1
  • Thanks, I get the same results
    – A. Kiyoshi
    Commented Sep 6, 2017 at 7:48
0

I got the total time to work through this code:

$sql=("SELECT SUM(total_visit_time) as total_time....);

while ($row=mysqli_fetch_assoc($sql)){

$total_time_spent += $row['total_time'];
}

echo $total_time_spent;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.