Home > Mobile >  how to get sum of dynamic values from array
how to get sum of dynamic values from array

Time:02-23

I have a list of requested items that are pulled from the database. What I am looking to do is to get the hourly_rate for each item that is selected and add those values together. I am getting the values I need from the database, but now I am looking to add those values.

//Items requested by user 
$requestedItems = "1,2,3";
$items = explode(',', $requestedItems);

//Query 
$priceQuery = "SELECT hour_rate,
                      day_rate,
                      item_id,
                      rental_status,
                      hourly_rental                                                     
                FROM
                      products
                WHERE
                      rental_status != 1
                AND
                      item_id = :var
                ";                              
$itemDisplay = array(); 

I loop through and get back the values, but now what I want is to add those returned values together...I tried turning them to integers and adding them together but could not seem to do it.

foreach($items as $var){
     $itemDisplay = $userFile->priceSelection($conn, $var, $priceQuery);
     foreach($itemDisplay as $key=>$v){
                                        
        $itemVar = '';  
        //Items returned that I would like to be added together                                     
        $itemVar = $v['hour_rate'];
                                        
        //$values = intval($itemVar);

        print_r($values);                                               
    }                                                                               
}   

The price selection function just grabs the values from database (code for clarity)

public function priceSelection($conn, $var, $priceQuery){       
    $stmt = $conn->prepare($priceQuery);
    $stmt->bindParam(":var", $var);
    $stmt->execute();
    $result = $stmt->fetchAll();
    if($stmt->rowCount() > 0){
        foreach($result as $row){
            $array[] = $row; 
        }
        return $array;
    }
    return false;
}

CodePudding user response:

Maybe you just get the amount, it will solve your problem?

$sumHourlyRental = "SELECT SUM(hourly_rental) FROM products WHERE rental_status != 1 AND item_id = :var";   

CodePudding user response:

Beside the option to Summarize the numbers within the SQL query you may do it in PHP as well.

Avoid resetting the value on each loop step and continuously add the value to $itemVar variable:

<?php
foreach($items as $var){
   $itemDisplay = $userFile->priceSelection($conn, $var, $priceQuery);
   foreach($itemDisplay as $key=>$v){
                                    
    //$itemVar = '';  // do not null the sum value
    //Items returned that I would like to be added together                                     
    $itemVar  = $v['hour_rate'];
                                    
    //$values = intval($itemVar);
                                       
  }                                                                            
} 
print_r($itemVar); // print final value
  •  Tags:  
  • php
  • Related