1

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;
7
  • Can u show the case with some example data, current result & expected result? Commented May 3, 2018 at 3:28
  • The current result is Fatal error: Unsupported operand types on line the line that says $amountRemaining = array($maximumCost - $sumOfCosts); Commented May 3, 2018 at 3:30
  • 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 :) Commented May 3, 2018 at 3:31
  • There are two types of data that each user can select: input and output. Eg. input of £20 and output of £50 so overall the $sumOfCosts = -30.00 (values are recorded as floats). The user can set their maximum amount, so say for instance, they set it to £100, then this would suggest that they have £100 + (-£30) left = £70.00 which is equal to $amountRemaining. However, this is currently outputting the error above. Any suggestions? Commented May 3, 2018 at 3:33
  • Excuse me... Sorry to say, it is a typical spaghetti code... I have some suggestions to help you clean your code and further find out the root cause: 1) Test the fetched result return $pdo->fetch(PDO::FETCH_ASSOC);, perhaps no result was found. 2) var_dump($_SESSION['userData']['user_id']) <-- test if the session is set. Commented May 3, 2018 at 3:38

1 Answer 1

1

Your not referencing your $maximumCost or $sumOfCosts variables correctly. It should be $maximumCost['maxAmount'] and $sumOfCosts['costSum'] respectively.

echo $maximumCost['maxAmount'] - $sumOfCosts['costSum'];

You query for the cost sum is also missing an userId param. Try:

$query = "SELECT SUM(value) AS costSum FROM costs WHERE user_id = :user_id";
Sign up to request clarification or add additional context in comments.

7 Comments

No problem, we have all been there. Please mark as accepted answer if it solved you problem.. Cheers!
I am quite interested how you can get $maximumCost['user_max_amount'], this is from $maximumCost = $appQueriesObject->getMaxAmount($_SESSION['userData']['user_id']);, however, the SQL query in this function is $query = "SELECT maxAmount FROM users WHERE user_id = :userid";, the fetched column should be maxAmount instead of user_max_amount?
@CK Wong You are absolutely correct. I edited my answer. Maybe OP saw what happened with the first query and noticed the mistakes in the second where similar and fixed that too. But anyways thanks for critique.
Apologies, but if I wished to use these values in a "progress bar" which shows the user how much they have spent of their monthly budget, how would I go about doing so?
<div class="progress"> <div class="progress-bar progress-bar-success" role="progressbar"> <script> $(function() { $( "#progress-bar progress-bar-success" ).progressbar({ max: <?php echo maximumCost; ?>, value: <?php var_dump($sumOfCosts); ?> }); }); </script> <?php echo $sumOfCosts; ?> spent </div> </div>
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.