Insert an array with codeIgniter and ajax

0

Hello

I'm trying to insert an array, but in this case, you can insert multiple lines in db.

How do I pass the pro codeIgniter data:

            var productsData = [];
        $(".productRow").each(function(i){
            var pData = { 
                id_product: $(this).find(".tdProductId").attr("id"),
                amount:  $(this).find(".tdProductAmount").text(),
                price: $(this).find(".tdProductPrice").text()
            };
            productsData.push(pData);
        });

        $.ajax({
            url: "<?php echo site_url('/StockController/createInputStock')?>",
            type: "POST",
            data: {
                id_people: $("#people option").attr("id"),
                type: $("#stock_type").val(),
                date: $("input[name=date]").val(),
                products: JSON.stringify(productsData)
            },
            success: function(data){
                Materialize.toast(data, 4000);
            },
            error: function(data){
                console.log(data);
                Materialize.toast('Erro ao adicionar uma nova entrada de estoque!', 4000);
            }
        });
    });

Controller:

    public function createInputStock(){
    $people = array(
        'input_date' => $this->input->post('date'),
        'input_type' => $this->input->post('type'),
        'id_people' => $this->input->post('id_people')
        );
    if($this->StockModel->createInputStockPeople('stock_input',$people)){
        $product = array(
            'id_product' => $this->input->post('id_people'),
            'unit_price' => $this->input->post('id_people'),
            'amount' => $this->input->post('id_people'),
            'id_stock' => '51'
            );
        foreach ($product as $products) {
            if($this->StockModel->createInputStockProduct('stock_input_products', $products)){
                echo "foi";
            }else{
                echo "deu pau";
            }
        }

    }else{
        echo "Erro ao adicionar a pessoa";
    }
}

Models:

    public function createInputStockPeople($table, $peopleData){
    $this->db->insert($table, $peopleData);
    return $this->db->insert_id();
}
public function createInputStockProduct($table, $produtctData){
    return $this->db->insert($table, $produtctData);
}

As you can see, only one line in the stock_input table is inserted (and this is already done) the problem is to insert several in the stock_input_products table

    
asked by anonymous 18.04.2016 / 01:50

0 answers