-
stepgr
Junior Member
-
Posts: 16
Threads: 7
Joined: Feb 2020
Reputation:
0
Hi guys,
I have a simple table and one of the columns is SET type (mariadb) . I have found this
way to insert values when adding records to the table (with a default selected value):
In my View Add Form:
PHP Code: echo form_label('Select Department', 'Department'); $dept = array( 'DPT1' => '1', 'DPT2' => '2', 'DPT3' => '3', 'DPT4' => '4', 'DPT5' => '5', 'DPT6' => '6'); $sdept = array('1'); echo form_multiselect('Department[]', $dept, $sdept);
At first there was a bit of a struggle to understand that I needed to convert the array from _POST
data to a string to insert into mariadb with something like that in my model :
PHP Code: $dept = $this->input->post('Department');
$data = array( 'id' => $this->input->post('id'), 'date' => $this->input->post('date'), 'No' => $this->input->post('No'), 'Act' => $this->input->post('Act'), 'Category' => $this->input->post('Category'), 'Department' => implode(",",$dept), // 'Department' => $this->input->post('Department'), 'Desc' => $this->input->post('Desc'), 'Remarks' => $this->input->post('Remarks'), ); return $this->db->insert('Near', $data);
Now I'm struggling on how to do the same when editing a record , how can I get the current values from the database SET and present them as a form_multiselect on my edit form ? is that even a possibility ?
Thanks
-
stepgr
Junior Member
-
Posts: 16
Threads: 7
Joined: Feb 2020
Reputation:
0
(01-19-2021, 09:47 PM)iRedds Wrote: $sdept = explode(',', $fieldFromTable)
Thanks mate , I have already tried that but there was some additional steps in order for it to work and you neede to do the
explode function in the edit form.
So in case someone is interested or having the same problem:
1. In the update function of your controller you need to pass an additional array
from the row with the SET data (from db) to your view $data
PHP Code: $sdept = array();
$data['sdept']=$data['db_row']['Department'];
2. In your edit view you make your multiform item same as in create form
but you explode the data from the controller in the selected items:
PHP Code: echo form_label('Select Department', 'Department'); $dept = array( 'DPT1' => '1', 'DPT2' => '2', 'DPT3' => '3', 'DPT4' => '4', 'DPT5' => '5', 'DPT6' => '6'); $selected = explode(",",$sdept); echo form_multiselect('Department[]', $dept, $selected);
3. In your model you need again to implode the data again before updating
PHP Code: 'date' => $this->input->post('date'), 'No' => $this->input->post('No'), 'Act' => $this->input->post('Act'), 'Category' => $this->input->post('Category'), 'Department' => implode(",",$dept), // 'Department' => $this->input->post('Department'), 'Desc' => $this->input->post('Desc'), 'Remarks' => $this->input->post('Remarks'), ); $id = $this->input->post('id'); $this->db->where('id', $id); return $this->db->Update('Near', $data);
At least this is working for me so far . I don't know if there is a more elegant way to achieve
the same result.
|