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.
//Get total hours from other query
$hours = $row['total_hours'];
//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){
//Edits added
$itemVar += $v['hour_rate'];
if($hours >= 3){
if($v['hourly_rental'] == '1'){
$hours -= 2;
$itemVar += $v['day_rate'] * $hours;
}else{
$itemVar += $v['day_rate'];
}
}else{
if($v['hourly_rental'] == '1'){
$itemVar += $v['day_rate'];
}else{
// This is the line here that is affecting the value of both items.
//If $day_rate = $v['day_rate'] then the items with hourly_rentals == '1'
//have correct values. If I set $day_rate = 0; then the items with
//hourly_rentals != '1' have correct values
//but not both at the same time. Might need to figure out a better comparison.
$day_rate = $v['day_rate'];
print_r($day_rate);
}
}
}
}
$totalPrice = $itemVar + $day_rate + $delivery_cost;
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;
}