1

大家好,我只需要一点帮助,关于使用循环在我的表中插入信息。我不知道我的错误在哪里。

这是我的代码:在我的 INSERT 模型中

        $poid = $this->input->post('po_id');
        $cash_delivery = $this->input->post('cash_delivery');
        $cash_check = $this->input->post('cash_check');
        $bank_transfer = $this->input->post('bank_transfer');

        $itemname = $this->input->post('item'); // array of item name
        $quantity = $this->input->post('qty'); // array of quantity
        $price = $this->input->post('price'); //array of price
        $total = $this->input->post('total'); //array of total

       //CHECK IF TRANSACTION IS CHECKED

        $val_delivery = NULL;
        $val_check = NULL;
        $val_transfer = NULL;

        if(isset($_POST['cash_delivery'])){
            $val_delivery = 'Y';
        }else{
            $val_delivery = 'N';
        }

        if(isset($_POST['cash_check'])){
            $val_check = 'Y';
        }else{
            $val_check = 'N';
        }

        if(isset($_POST['bank_transfer'])){
            $val_transfer = 'Y';
        }else{
            $val_transfer = 'N';
        }

        $filtername = array_filter($itemname);
        $filterquantity = array_filter($quantity);
        $filterprice = array_filter($price);
        $filtertotal = array_filter($total);

        //INSERT ORDERS 

        for($x = 0; $x < sizeof($filtername); $x++){

            $orders = array(
                'poid' => null,
                'order_id' => $poid,
                'item_desc' => $filtername[$x],
                'item_qty' => $filterquantity[$x],
                'item_price' => $filtertotal[$x],
                'total' => $filtertotal[$x],
                'cash_on_delivery' => $val_delivery,
                'is_check' => $val_check,
                'bank_transfer' => $val_transfer,
                'transaction_date' => $dateorder
            );

            $this->db->insert('po_order',$orders); //Only first item (index[0]) is added
            echo "<pre>";
            print_r($orders); //This will print my array values 'NO ERROR HERE'
            echo "<hr />";

        }

My database table design:

mysql> desc po_order;
+------------------+---------------------+------+-----+---------+----------------+
| Field            | Type                | Null | Key | Default | Extra          |
+------------------+---------------------+------+-----+---------+----------------+
| poid             | int(10) unsigned    | NO   | PRI | NULL    | auto_increment |
| order_id         | varchar(50)         | NO   | UNI | 0       |                |
| item_desc        | varchar(50)         | NO   |     | NULL    |                |
| item_qty         | int(10) unsigned    | NO   |     | NULL    |                |
| item_price       | float(7,2) unsigned | NO   |     | NULL    |                |
| total            | float(7,2) unsigned | NO   |     | NULL    |                |
| cash_on_delivery | enum('Y','N')       | NO   |     | NULL    |                |
| is_check         | enum('Y','N')       | NO   |     | NULL    |                |
| bank_transfer    | enum('Y','N')       | NO   |     | NULL    |                |
| transaction_date | datetime            | NO   |     | NULL    |                |
+------------------+---------------------+------+-----+---------+----------------+

我的问题是只添加了循环中的第一项。我无法从循环中获取下一个项目

4

1 回答 1

1

恕我直言,您没有从 $itemname = $this->input->post('item'); //数组应该是返回的吧?

试着看看它是否真的如此。

还有你的 for($x = 0; $x < sizeof($filtername); $x++){

sizeof($filtername) 给你什么?

尝试

die('for loop will iterate "'.sizeof($filtername).'" times');

于 2013-08-22T03:07:52.410 回答