Php While循环中的Sum变量,基于IF语句的结果

Php While循环中的Sum变量,基于IF语句的结果,php,variables,if-statement,while-loop,sum,Php,Variables,If Statement,While Loop,Sum,还有一个问题 在循环工作时,我有以下几点: $activityreport = "SELECT $fieldname FROM $table WHERE QuoteID=$quoteid"; $activity = mysql_query($activityreport) or die(mysql_error()); while($data = mysql_fetch_array($activity)){ $amount = $data[$fieldname

还有一个问题

在循环工作时,我有以下几点:

    $activityreport = "SELECT $fieldname FROM $table WHERE QuoteID=$quoteid";

    $activity = mysql_query($activityreport) or die(mysql_error());

    while($data = mysql_fetch_array($activity)){

    $amount = $data[$fieldname];

    if($amount>0 && $table != 'OptionFees'){ 
        $amountc = abs($amount);
        }
        elseif($amount>0 && $table == 'OptionFees'){
        $amountd = abs($amount);
        }
        elseif($amount<0 && $table == 'OptionFees'){
            $amountc = abs($amount);
        }
        else{
            $amountd = abs($amount);
        }

    $totald = $totald + $amountd;
    $totalc = $totalc + $amountc;

    echo "<tr><td class='description'>$table</td>
            <td class='debit'>".money_format('%(#10n', $amountd)."</td>
            <td class='credit'>".money_format('%(#10n', $amountc)."</td></tr>";

    }
$activityreport=“从$table中选择$fieldname,其中QuoteID=$QuoteID”;
$activity=mysql\u query($activityreport)或die(mysql\u error());
而($data=mysql\u fetch\u数组($activity)){
$amount=$data[$fieldname];
如果($amount>0&&$table!='OptionFees'){
$amountc=abs(金额);
}
elseif($amount>0&&$table=='OptionFees'){
$amountd=abs(金额);
}

elseif($amount在开始循环之前初始化这些变量:

$activityreport = "SELECT $fieldname FROM $table WHERE QuoteID=$quoteid";

$activity = mysql_query($activityreport) or die(mysql_error());

$totald = 0;
$totalc = 0;

while($data = mysql_fetch_array($activity)){

$amount = $data[$fieldname];
$amountc = 0;
$amountd = 0;

if($amount>0 && $table != 'OptionFees'){ 
    $amountc = abs($amount);
    }
    elseif($amount>0 && $table == 'OptionFees'){
    $amountd = abs($amount);
    }
    elseif($amount<0 && $table == 'OptionFees'){
        $amountc = abs($amount);
    }
    else{
        $amountd = abs($amount);
    }

$totald = $totald + $amountd;
$totalc = $totalc + $amountc;

echo "<tr><td class='description'>$table</td>
        <td class='debit'>".money_format('%(#10n', $amountd)."</td>
        <td class='credit'>".money_format('%(#10n', $amountc)."</td></tr>";

}
$activityreport=“从$table中选择$fieldname,其中QuoteID=$QuoteID”;
$activity=mysql\u query($activityreport)或die(mysql\u error());
$totald=0;
$totalc=0;
而($data=mysql\u fetch\u数组($activity)){
$amount=$data[$fieldname];
$amountc=0;
$amountd=0;
如果($amount>0&&$table!='OptionFees'){
$amountc=abs(金额);
}
elseif($amount>0&&$table=='OptionFees'){
$amountd=abs(金额);
}

埃尔塞夫($Amount请停止使用古老的
mysql\u*
函数编写新代码。它们不再被维护,社区已经开始。相反,你应该了解并使用or。如果你不能决定,将帮助你选择。如果你想学习,谢谢你的建议。我会查看它。你的评论没有任何内容与我问的问题有关…不过我喜欢开车经过。嗯,有趣的文章,我会考虑的。无论如何谢谢。是的,我是用$activityreport上面的$totald=0;$totalc=0;$amountd=0;$amountc=0;$activity=0;它需要在$activity之后吗?不,它应该在循环之前。所以我我猜它不起作用,可以尝试在片刻之后(和if之前)初始化
$amountd
$amountc
?没有改变…这让我抓狂。我在特定的If语句中添加了$totald=$totald+$amountd;,以防万一,但这也没有改变任何东西…我发现,这个while循环在一个更大的循环中。我将$totald和$totalc的声明移到了while循环(和所有循环)之外瞧!现在效果很好。