0
votes

I've an existing form which is passing the input data to the model in an array format. $postdata has all the data from the view and sending to model.

Controller:

$inquiry_id = $this->input->post('inquiry_id');
$postdata = $this->input->post();
$this->load->model('Design_model');
$this->Design_model->insertdata($postdata,$inquiry_id);

Model:

function insertdata($data = array(), $inquiry_id){
        $sql = $this->db->query("select * from design where inquiry_id='".$inquiry_id."'");
        if($sql->num_rows() == 0){
                $sql_query = $this->db->insert('design', $data);
        }
        else{
            $this->db->where('inquiry_id', $inquiry_id);
            $this->db->update('design', $data);
        }          
    }

Above is working fine. Now, I'd like to add few fields in the view and save in a different database table. Need to exclude the new field values from $postdata array getting saved. Need to find the best approach to do this. I can start with some name for all the new fields, so that we can add any filter if available to exclude from the $postdata.

2

2 Answers

0
votes

You can use elements() function from Array helper.

$array = array(
        'id' => 101,
        'title' => 'example',
        'desc' => 'something',
        'unwanted' => 'bla bla'
);

$filtered_array = elements(array('id','title','desc'),$array); //you can use this directly to the post data

$this->Design_model->insertdata($filtered_array,$inquiry_id);

You can use array_merge() or array_push() functions to add new fields to the array.

0
votes

Let's say you have following data

$postdata = array("name"=>"xyz",
"email"=>"[email protected]",
"age"=>"40",
"gender"=>"Male",
"occupation"=>"Engineer"
);

Of which first 3 records are from old fields and last 2 are from new fields as you saying. You need to find last index of first set i.e. '3' Now you can do this.

$firstDb = array_splice($postdata,0,3);    //here 3 is index we are using to get first 3 records from $postdata
$secondDb = array_slice($postdata,0,3);    //here 3 is index we are using to get records from position 3 from $postdata

Output:

$firstDb = array("name"=>"xyz","email"=>"[email protected]","age"=>"40");
$secondDb = array("gender"=>"Male","occupation"=>"Engineer");

Now you can insert you records as you wish to. Happy coding