Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 计算总价值,然后在控制器代码点火器中添加VAT_Php_Codeigniter - Fatal编程技术网

Php 计算总价值,然后在控制器代码点火器中添加VAT

Php 计算总价值,然后在控制器代码点火器中添加VAT,php,codeigniter,Php,Codeigniter,我有一些数据来自我的数据库模型。我想做的是得到付款总额,然后乘以增值税(0.20) 我假设我需要在某处创建某种FOR循环,但不太确定在哪里 我的控制器代码是: function vat_list() { if(isset($_POST['search_date'])) { $search_date = $this->input->post('search_date', TRUE); } e

我有一些数据来自我的数据库模型。我想做的是得到付款总额,然后乘以增值税(0.20)

我假设我需要在某处创建某种FOR循环,但不太确定在哪里

我的控制器代码是:

function vat_list()
    {
        if(isset($_POST['search_date']))
        {
            $search_date = $this->input->post('search_date', TRUE);
        }
        else
        {
            $search_date = date('D j M Y', strtotime('today - 7 days')) . ' to ' . date('D j M Y');
        }

        $my_result_data = $this->Invoices_model->read_vat($this->viewinguser->BUSINESSES_id, $search_date);
        $my_results = $my_result_data->result();

        $vat_total = $my_results[0]->PAYMENTS_total;
        $vat_total = number_format($my_results[0]->PAYMENTS_total, 2, '.', '') * 0.20;

        $this->template->set('title', 'View VAT Reports');
        $this->template->set('subtitle', $search_date);
        $this->template->set('vat_items', $my_results);
        $this->template->set('vat_total', $vat_total);
        $this->template->build('accounts/invoices/vat_list');
    }
谢谢

试试这个:

    $my_results = $my_result_data->result();
    foreach ($my_results as $result) {
       number_format($result->PAYMENTS_total, 2, '.', '') * 0.20;
    }
数字\u格式的结果可以返回到$result\a新数组\etc中。

尝试以下操作:

    $my_results = $my_result_data->result();
    foreach ($my_results as $result) {
       number_format($result->PAYMENTS_total, 2, '.', '') * 0.20;
    }
数字格式的结果可以返回到$result\a新数组\etc中。

编辑

不要这样做-不要使用浮点运算来处理货币计算。如果使用实现定点(十进制)数据类型的工具,则有几个库可用于此操作,例如或。最初的答案留在这里只是出于历史目的


在不知道您的
$my_results
数组的结构的情况下,我无法确定,但我猜这就是您想要的:

// ...

$my_results = $my_result_data->result();

// The VAT rate (you never know, it *might* go down at some point before we die)
$vatPercent = 20;

// Get the total of all payments
$total = 0;
foreach ($my_results as $result) {
  $total += $result->PAYMENTS_total;
}

// Calculate the VAT on the total
$vat = $total * ($vatPercent / 100);

// The total including VAT
$totalIncVat = $total + $vat;

// You can now number_format() to your hearts content

$this->template->set('title', 'View VAT Reports');

// ...
编辑

不要这样做-不要使用浮点运算来处理货币计算。如果使用实现定点(十进制)数据类型的工具,则有几个库可用于此操作,例如或。最初的答案留在这里只是出于历史目的


在不知道您的
$my_results
数组的结构的情况下,我无法确定,但我猜这就是您想要的:

// ...

$my_results = $my_result_data->result();

// The VAT rate (you never know, it *might* go down at some point before we die)
$vatPercent = 20;

// Get the total of all payments
$total = 0;
foreach ($my_results as $result) {
  $total += $result->PAYMENTS_total;
}

// Calculate the VAT on the total
$vat = $total * ($vatPercent / 100);

// The total including VAT
$totalIncVat = $total + $vat;

// You can now number_format() to your hearts content

$this->template->set('title', 'View VAT Reports');

// ...

谢谢,我遵循了这一点,并在我的数字格式中加入了$vat_total变量。总数应该是132。但我要回到10岁。我的值是:55,5,50,所以我预计总共是132。有什么想法吗?数字格式可分为每个值,如果你想求和,Dave的方法就是最好的方法。谢谢,我遵循了这一点,在我的数字格式中加入了$vat_total变量。总数应该是132。但我要回到10岁。我的值是:55,5,50,所以我预计总共是132。有什么想法吗?数字\ u格式可分离地为每个值运行,如果你想求和,Dave的方法就是最好的方法。这太完美了。谢谢@DaveRandom。我也很喜欢它,它也能经得起未来的考验,太完美了。谢谢@DaveRandom。我也很喜欢它。