codeigniter从两个不同的循环中减去两个变量

时间:2017-05-27 11:11:56

标签: php codeigniter

你好我需要找到两个不同循环之间的差异,即求和循环和总循环,如下面的控制器代码所示

function summary() {

        echo '<table>';


            $summation=$this->select_model->sum_income("date_of_income BETWEEN '" . $start . "' AND '" . $last . "'");

                 foreach($summation as $sum){


                echo '<tr><td>Total Income</td> <td >'.$sum['total'].'</td></tr>';

                    } 




            $totals=$this->select_model->sum_expenditure("date_of_expenditure BETWEEN '" . $start . "' AND '" . $last . "'");

                    foreach($totals as $total){

                    echo '<tr><td>Total Expenditure</td><td ><strong>'.$total['total'].'<</td></tr>'; 

                    }
                    //display differences here:
                    echo '<tr><td>'.**$sum['total']-$total['total']**.'</td></tr>'

        echo '</table>';


        }

1 个答案:

答案 0 :(得分:0)

你必须在循环之外创建变量,并在循环进行时添加(或减去)。

$sum = 0;
$total = 0;

foreach... {
    $sum += $sum;
}

foreach... {
    $total += $total;
}

echo $sum - $total;

如果您正在添加负数,则数学运算无需执行任何特定操作即可。