简体   繁体   中英

associative array into columns of database

I need help on storing associative array into database columns in codeigniter.

I have a input field named "vehicle[]", actually this one this consist of 8 fields like Make, Model, color etc.

also the user can add one more more vehicles using "Add another" option which the input fields same as vehicle[]

here's my code for Model of codeigniter

public function vehicle($new){

    $new = $this->input->post('vehicle');

    $data = array(
        'vehicle_number' => json_encode($new),
        'make'       => json_encode($new),
        'color'       => json_encode($new),
         'color'       => json_encode(model),
    );
    $this->db->insert('vehicles_tbl', $data); 

this way the all he data stored in a one column of the database, how can i store each value in related column of the database.

My controller

$this->vehicle_model->vehicle($new); 

我的应用程序

You should code like this
//view

< input type="text" name="vehicle_number[]">
< input type="text" name="make[]">
< input type="text" name="color[]">
< input type="text" name="model[]">

// Model

public function vehicle(){

$new = $this->input->post('vehicle_number');



for ($i=0; $i <count($new) ; $i++) { 

    $data[] = array(
        'vehicle_number' => $_POST['vehicle_number'][$i],
        'make'       => $_POST['make'][$i],
        'color'       => $_POST['color'][$i],
        'model'       =>$_POST['model'][$i],
        );

}

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

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