Issue
I have values 3 seperated by comma like (AC,BC,MC).Some times the valuse will be AC only or AC,BC or AC,MC etc
I want to check it according to f_id from table like below
id f_id value
1 1 AC
2 1 BC
3 1 MC
4 2 AC
5 3 MC
If any value is not in the table then I need insert it according to f_id
How to do this?
Solution
basically, you want to explode the user input string and get all the results for the current user and compare them. if a value exists in the db that exists in the user input then we don't do anything, if it doesn't then we perform an insert.
$input = 'AC,BC';
$f_id = 1;
$items = explode(',', $input);
$res = $this->db->get_where('yourtablename', array('f_id', $f_id))->result_array();
$values = array_column($res, 'value');
foreach ($items as $index => $value) {
$value = trim($value); // remove whitespace; you might want to do more filtering/data manipulation than this
if (!in_array($value, $values)) {
$data = array('f_id' => $f_id, 'value' => $value);
$this->db->insert('yourtablename', $data);
}
}
Answered By - Alex
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.