简体   繁体   中英

how to insert multiple arrays in codeigniter?

Please help me, I'm in trouble with how to insert multiple array in codeigniter.. If I post 2 or 3 arrays, it successfully saves to the database, but if I post more than 3 arrays, the data is not always saved to the database.

Here's my model function :

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

Here my code :

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);
}

Here is your answer.

$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);

First, create a multidimensional array using a loop. Once the data is ready to insert into the db, use the function insert_batch . The insert_batch function will insert all your data at once into the database.
Hope this can help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM