Home > Software engineering >  How to get array values and add up total
How to get array values and add up total

Time:03-01

So what I want to do is get all values from query and add them together to get a total amount. The following function grabs all values necessary. (Added for clarity)

public function priceTotal($conn, $var, $hours){    
    $query = "SELECT hour_rate, day_rate, item_id, hourly_rental  FROM products WHERE item_id = :id";
    $stmt = $conn->prepare($query);
    $stmt->bindParam(":id", $var);
    $stmt->execute();
    $result = $stmt->fetchAll();
    if($stmt->rowCount() > 0){
        $total = 0;
        foreach($result as $row){
            $hour_rate = $row['hour_rate'];
            $day_rate = $row['day_rate'];
            if($hours == '2'){                                      
                $total  = $hour_rate;
            }else{
                if($row['hourly_rental'] == '1'){
                    $hours -= 2;
                    $total  = $hour_rate   $day_rate * $hours;
                }
                else{
                    $total = $hour_rate   $day_rate;
                }
            }
            $array[] = $total;
        }
        return $array;
    }
    return false;
}

Now what I need to be able to do is grab the values from the array and add them all together. This was my latest attempt. But only the items in the last array get added together.

$weeklyGross = $chart->getChartInfo($conn, $weekly);
if(!empty($weeklyGross)){
    foreach($weeklyGross as $row){                          
        $hours = $row['total_hours'];
        $totalItems = $row['requested_items'];
        $delivery_cost = $row['delivery_cost'];
                        
        //Store total items in array
        $items = explode(',', $totalItems);
        $item_name = array();
                        
        $totalPrice = 0;
        foreach ($items as $var){
        //Get items from price total function
        $addItems = $chart->priceTotal($conn, $var, $hours);
                            
        //$completeOrder = array_merge($addItems);
        print_r($addItems);
                            
        foreach($addItems as $k){
            $totalPrice  = $k;
        }
    }                           
    $totalPrice  = $delivery_cost;                          
}
print_r($totalPrice);
}

Here is the printout of the arrays, and the values that I would like to add all together.

Array ( 
[0] => 420 
[1] => 200 
) 

Array ( 
[0] => 270 
) 

Array ( 
[0] => 350 
[1] => 350 
) 

Array ( 
[0] => 270 
) 

Array ( 
[0] => 220 
) 

Array ( 
[0] => 280 
) 

Array ( 
[0] => 270 
[1] => 300 
) 

Array ( 
[0] => 700 
[1] => 380 
) 

CodePudding user response:

See what I have done with $totalPrice

$weeklyGross = $chart->getChartInfo($conn, $weekly);
if(!empty($weeklyGross)){

    $totalPrice = 0; // << Put this here


    foreach($weeklyGross as $row){                          
        $hours = $row['total_hours'];
        $totalItems = $row['requested_items'];
        $delivery_cost = $row['delivery_cost'];
                        
        //Store total items in array
        $items = explode(',', $totalItems);
        $item_name = array();
                        
        /// $totalPrice = 0; // << not here, as it resets the total price on each loop

        foreach ($items as $var){
        //Get items from price total function
        $addItems = $chart->priceTotal($conn, $var, $hours);
                            
        //$completeOrder = array_merge($addItems);
        print_r($addItems);
                            
        foreach($addItems as $k){
            $totalPrice  = $k;
        }
    }                           
    $totalPrice  = $delivery_cost;                          
}
print_r($totalPrice);
}
  •  Tags:  
  • php
  • Related