1

Hello guys I just need a little help here about inserting information in my table using a loop. I don’t know where’s my error.

Here’s my code: In my INSERT model

        $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    |                |
+------------------+---------------------+------+-----+---------+----------------+

My problem is only the first item from the loop is added. I can't get the next items from the loop.

1 Answer 1

1

IMHO, youre not getting the right value from $itemname = $this->input->post('item'); //array is expected to be return right?

try to see if it really does then.

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

what does sizeof($filtername) give you?

try

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

Sign up to request clarification or add additional context in comments.

2 Comments

Ok about the $itemname = $this->input->post('item') I got the array containing my inputted data. I think to error in my array. About sizeof($filtername) it gave me for loop will iterate "2" times because in my array name i only inputted 2 fields.
I can see that youre over writing your databases auto incrementing primary key "poid" in your array property 'poid' => null, to null. You must be having a double primary key issue in your database. So, just remove it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.