在foreach循环中不计算某些行

时间:2016-09-16 13:14:05

标签: php

我从9xx项目的数据库中获得价格。 我在show items页面上添加了这个。使用Foreach $ rows = $ row。 vprice是我的售价,dprice是我的经销商价格

$commisionrate = 30;
$commisionfee  = 100;
$fee  = $row['dprice'] + $commisionfee;//+100
$x = $row['dprice'];
$y = $x * $commisionrate / 100;
$z = $x + $y;
$rate = $z;//(100*30%)+100
if (($rate > $row['vprice']) && ($fee < $row['vprice'])){
    echo $fee;
}elseif (($fee > $row['vprice']) && ($rate < $row['vprice'])){
    echo $rate;
}elseif ($row['dprice']=$row['vprice']){
    echo $row['dprice'];
}

当我重新检查所有内容时,我发现$row['dprice']的一些项目没有计算在内,并且仍以旧价格显示。例子是假的:我发现vprice是188和80 dprice之后计算应该是104但是没有变化仍然保持在80.

1 个答案:

答案 0 :(得分:1)

$commisionrate = 30;
$commisionfee  = 100;
$fee  = $row['dprice'] + $commisionfee;//+100
$x = $row['dprice'];
$y = $x * $commisionrate / 100;
$rate = $x + $y;

// You don't need to put nested brackets, it very simple condition    
if ($rate > $row['vprice'] && $fee < $row['vprice']){
    echo "fee: " . $fee; //add some hint words, so you know which condition fires

// You don't need to put nested brackets, it very simple condition
} elseif ($fee > $row['vprice'] && $rate < $row['vprice']) {
    echo "rate: " . echo $rate;

// USE double `==`, because when single `=` used, the condition always returns true and you're confused by your result
} elseif ($row['dprice'] == $row['vprice']) { 
    echo "row[\'dprice\']: " . $row['dprice'];

// add last else
} else {
   // this is helpful for debugging of your possible output, regardless you are awaiting some output here
}