PHP添加两个数组

时间:2018-05-03 03:25:00

标签: php arrays pdo

我目前正在尝试制作一个进度条,显示用户与其最高限额相比花了多少钱。 我已经计算了用户费用的总和,由$ sumOfCosts给出,但是,我试图从用户的最大限额中减去费用的总和,但是,下面的代码并不是&n #39;工作。有人有建议吗?

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;

1 个答案:

答案 0 :(得分:1)

您没有正确引用$ maximumCost或$ sumOfCosts变量。它应该分别是$ maximumCost ['maxAmount']和$ sumOfCosts ['costSum']。

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

您查询成本总和也缺少userId参数。尝试:

$query = "SELECT SUM(value) AS costSum FROM costs WHERE user_id = :user_id";