如何传递两个数组以在codeigniter中查看

时间:2017-06-01 05:56:21

标签: php arrays codeigniter loops

我不是编程专家

我有一个foreach循环,它从一个计算总计和小计

的表单中获取值
 public function salecal()
{
    if ($this->input->post())
    {

        $i = 0;
        $data = array();
        $subtotal = 0;

        foreach($this->input->post('pname') as $d){


            $data[] = array(
                'pid' => $this->input->post('pid[]')[$i],
                'pname' => $this->input->post('pname[]')[$i],
                'quantity' => $this->input->post('qty[]')[$i],
            );

            foreach ($data as $entry) {

                    $qty = $entry['quantity'];
                    $pid = $entry['pid'];
                    $proname = $entry ['pname'];

            }

            $value = $this->insert_model->get_price($pid); ///pasing the product id to get the the price from database

            foreach ($value->result() as $row)
            {
                $price = $row->price;
            }

            $total = $price * $qty; ////Total calculation


            $subtotal = $subtotal + $total;/////Sub Total Calculation

            $i++;


        }

        $result = compact("proname", "price", "qty", "total","i", "subtotal");

        $this->load->view("bill", $result);

    }

}

当我运行此代码时,我只获取最终输入的产品详细信息,但小计是正确的

但数据插入表单是动态的

我想要的结果是

用户将插入数个或一个数量为

的项目

应计算数据并将计算值传递给视图

但目前我只是为最后插入的数据获取它,请帮助我如何捕获用户插入表单的所有数据以及如何将它们传递给视图

1 个答案:

答案 0 :(得分:0)

因为你因此覆盖了变量,所以它只传递了最后一个覆盖值。你应该为每一个做数组

这是您需要如何使用数组

发送所有数据
    <?php

    public function salecal()
    {
        if ($this->input->post())
        {

            $i = 0;
            $data = array();
            $subtotal = 0;

            $final_array_collection =array();

            foreach($this->input->post('pname') as $d){

                $total =0; //reset the total to zero for each product

                $pid = $this->input->post('pid[]')[$i];
                $pname = $this->input->post('pname[]')[$i];
                $quantity = $this->input->post('qty[]')[$i];

                $value = $this->insert_model->get_price($pid); ///pasing the product id to get the the price from database

                foreach ($value->result() as $row)
                {
                    $price = $row->price;
                }

                $total = $price * $quantity; ////Total calculation


                $subtotal = $subtotal + $total;/////Sub Total Calculation


                $final_array_collection[] =array("proname"=>$pname, "price"=>$price, "qty"=>$quantity, "total"=>$total,"i"=>$i, "subtotal"=> $subtotal);

                $i++;


            }

            $result = compact("final_array_collection");

            $this->load->view("bill", $result);

        }

    }



    ?>