计算总值,然后在控制器代码点火器中添加增值税

I have some Data coming out of my DB Model. What I want to do is get the total amount of the PAYMENTS_total and then times it by VAT (0.20)

I'm assuming I need to creat some sort of FOR loop somewhere, But not too sure where?

My Code for the controller is :

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');
    }

Thanks

EDIT

Don't do this - don't use floating point arithmetic for dealing with monetary calculations. Use something that implements fixed-point (decimal) data types, there are several libraries available for this, such as https://github.com/mathiasverraes/money or https://github.com/sebastianbergmann/money. The original answer is left here for historical purposes only.


Without knowing the structure of your $my_results array I can't say for sure, but I'm guessing this is what you're after:

// ...

$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');

// ...

Try this:

    $my_results = $my_result_data->result();
    foreach ($my_results as $result) {
       number_format($result->PAYMENTS_total, 2, '.', '') * 0.20;
    }

The result of number_format can be returned back into $result\a new array\etc.