如何在codeigniter中插入多个数组?

时间:2017-12-20 04:40:46

标签: php codeigniter

请帮助我,我在如何在codeigniter中插入多个数组时遇到麻烦。如果我发布2或3个数组,它会成功保存到数据库中,但是如果我发布的数据超过3个,则数据不是始终保存到数据库中。

这是我的模特功能:

function add_booking($table,$data) { return $this->db->insert($table,$data); }

这是我的代码:

for($i =0 ; $i < count($data["alamat_tujuans[]"]) ;$i++){
    $tujuan =$data["alamat_tujuans[]"][$i];
    $ddetail = array(
        "id"           =>$delivery_id+$i,
        "address"      =>$tujuan,
        "sku_driver"   =>$data["sku[]"][$i],
        "contact"      =>$data["kontak_penerima[]"][$i],
        "instruction"  =>$data2["instruksi[]"][$i],
        "order_id"     =>$obj[0]->id.'-'.$transid,
        "name"         =>$data["nama_penerima[]"][$i],
        "email"        =>$data["email_penerima[]"][$i],
        "created_date" =>$date,
        "place_name"   =>$data['place_name'],
        "districts"    =>$data['districts'],
        "lat"          =>$data['tujuan1_lat[]'][$i],
        "lng"          =>$data['tujuan1_lng[]'][$i],
        "harga_muatan" =>$data['harga_muatanya[]'][$i],
        "status"       =>$data['status'],
        "provider_id"  =>$_SESSION['logged_in']['provider_id'],
        "city"         =>$data['city']
        );
    $insertData = $this->m_booking->add_booking("delivery_detail", $ddetail);
}

1 个答案:

答案 0 :(得分:1)

这是你的答案。

$data = array(
              array(
                'title' => 'My title',
                'name' => 'My Name',
                'date' => 'My date'
              ),
              array(
                'title' => 'Another title',
                'name' => 'Another Name',
                'date' => 'Another date'
              )
);

$this->db->insert_batch('mytable', $data);

首先,使用循环创建一个多维数组。数据准备好插入数据库后,使用函数insert_batchinsert_batch函数会立即将所有数据插入数据库 希望这可以帮到你。