I'm currently attempting to produce a progress bar which shows how much a user has spent in comparison to their maximum allowance. I have calculated the sum of the user's costs, as given by $sumOfCosts, however, I'm attempting to subtract the $sum of costs from the user's maximum allowance however, the code below doesn't work. Does anyone have any advice?
appQueries.php
<?php
class appQueries {
protected $db = null;
public function __construct($db){
$this->db = $db;
}
public function costsSum($user_id){
$query = "SELECT SUM(value) AS costSum FROM costs";
$pdo = $this->db->prepare($query);
$pdo->bindParam(':user_id', $user_id);
$pdo->execute();
return $pdo->fetch(PDO::FETCH_ASSOC);
}
public function getMaxAmount($userid){
$query = "SELECT maxAmount FROM users WHERE user_id = :userid";
$pdo = $this->db->prepare($query);
$pdo->bindParam(':userid', $userid);
$pdo->execute();
return $pdo->fetch(PDO::FETCH_ASSOC);
}
}
homepage.php
$appQueriesObject = new appQueries($DBH);
$maximumCost = $appQueriesObject->getMaxAmount($_SESSION['userData']['user_id']);
$sumOfCosts = $appQueriesObject->costsSum($_SESSION['userData']['user_id']);
$amountRemaining = array($maximumCost + $sumOfCosts);
echo $maximumCost['user_max_amount'];
echo array_sum($sumOfCosts);
echo $amountRemaining;
array($maximumCost - $sumOfCosts)makes no sense, as you're trying to form an array from only one value. Are you simply looking for$amountSpent = $maximumCost - $sumOfCosts;? That would seem like what you want based on the variable names :)return $pdo->fetch(PDO::FETCH_ASSOC);, perhaps no result was found. 2)var_dump($_SESSION['userData']['user_id'])<-- test if the session is set.