总和值未使用订单+ =计算

时间:2018-01-29 15:51:38

标签: php loops sum

我只是尝试在循环中使用sum 所以我创建了这个代码来运行第一个sql命令来从MYSQL中获取值

$money = 0;
foreach ( $fullticketsdetails as $fullticketdetails ) 
{
    echo $fullticketdetails->TKT_sold;
    echo $fullticketdetails->TKT_price;
    $moneydo = $fullticketdetails->TKT_sold *  $fullticketdetails->TKT_price;
    $money+= $moneydo;
}
echo $money;

现在结果必须是:ex = money1 + money2(1590 + 775 = 2365)

但结果是字符串的连接:ex = money1money2(1590775),而不计算任何值。

2 个答案:

答案 0 :(得分:0)

您在代码中遗漏了$。我在下面的代码中做了一个注释。

    $money = 0;
foreach ( $fullticketsdetails as $fullticketdetails ) 
{
echo $fullticketdetails->TKT_sold;
echo $fullticketdetails->TKT_price;
$moneydo = $fullticketdetails->TKT_sold *  $fullticketdetails->TKT_price;
$money+= $moneydo; //The missing $ is on this line for 'moneydo'
}
echo $money;

答案 1 :(得分:0)

请详细说明其为我工作

<?php

class aaa{

    function __construct($a,$b){
        $this->TKT_sold = $a;
        $this->TKT_price = $b;
    }
}

$fullticketsdetails = array();
$fullticketsdetails[] = new aaa(1,2);
$fullticketsdetails[] = new aaa(5,6);

$money = 0;
foreach ( $fullticketsdetails as $fullticketdetails ) 
{
    $moneydo = $fullticketdetails->TKT_sold *  $fullticketdetails->TKT_price;
    $money += $moneydo;
}

echo $money;
答案是 32

32 = 1*2 + 5*6