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);
Related
I am using the simple form and array input like this (not dynamic)
<input type="text" name="pre_ref_position[]" id="pre_ref_position">
<input type="text" name="pre_ref_position[]" id="pre_ref_position">
Form post with ajax and return data like this;
[
0:{pre_ref_position:'example1'}
1:{pre_ref_position:'example2'}
]
So I want to save this data with eloquent but how can I handle this array and save each row in the database?
Assuming you receive your array in a variable called $refValin your controller
Then use the following code:
$refArr = [];
foreach($refVal as $key => $val)
{
$now = Carbon::now();
//ref_pos in the array below refers to the field name in your database.
$refArr[] = [ 'ref_pos' => $val['pre_ref_position'] ,'created_at' => $now, 'updated_at' => $now ];
}
//assuming model name is `Position`
Position::insert($refArr);
I am using append multi select box with some input text box also. i want save all data into table using for loop one by one. I m finished this but problem is i couldn't implode multi select box value inside of for loop. i want store each loop individual in table.It always save first record only in team(multiselectbox value) Please help me.
My View Screen
My controller :
public function saveprogress()
{
$project_id=$this->input->post('project_id');
$prog_date=$this->input->post('date');
$task=$this->input->post('task');
$team = $this->input->post('team');
$report=$this->input->post('report');
$numFields = count($prog_date);
$teamFields = count($team);
for ($i = 0; $i < $numFields; $i++) {
// Pack the field up in an array for ease-of-use.
$field = array(
'prog_date' => $prog_date[$i],
'task' => $task[$i],
'team' => $team[$i],
'report' => $report[$i],
'project_id' => $project_id
);
$result=$this->db->insert('progress', $field);
}
}
My table after save (i selected 2 employee but store one in team column) :
In view add [ ] square brackets after input name hope useful..
<input type="text" name="date[]">
I fill my html table with these values in two rows. The row can be added dynamically.
https://i.stack.imgur.com/tc5OA.jpg
All the fields are working fine (they go to the proper place inside the database) except for the checkboxes. I checked 2 checkboxes for each row but the values all stored in the first row like this while the second row doesn't store the expected value.
https://i.stack.imgur.com/BB1QM.jpg
I named each field with array for example <input name="category[]">
this is the controller
foreach ($basic_category as $id => $key) {
$basic_data = array(
'id_number' => $id_number,
'basic_category' => $basic_category[$id],
'basic_workscope' => $basic_workscope[$id],
'basic_i' => $basic_i[$id],
'basic_e' => $basic_e[$id],
'basic_pi' => $basic_pi[$id],
'basic_pa' => $basic_pa[$id],
'basic_ata_chapter' => $basic_ata_chapter[$id]
);
$this->mod->insert_t_basic($basic_data);
}
this is the model
public function insert_t_basic($data) {
$this->db->insert('t_basic', $data);
}
Please tell me if this is not clear enough or you want more details :)
Please change all the field names in the below format
<input name="data[category][]">
<input name="data[basic_pi][]">
In the controller
$data = array();
foreach ($this->input->post('data') as $k => $v) {
foreach ($v as $data_k => $data_v) {
$data[$data_k][$k] = $data_v;
}
}
if(count($data) > 0) {
$this->mod->insert_t_basic($data);
}
In the model
$this->db->insert_batch('t_basic', $data);
Please try this Hope this helps. Thanks
i want to insert two different values into my database.
The field names are same but i want two different values to be saved there.
For Now i have a form which creates Organization. in that i have two fields sales and tech. So i insert Name,last_name,email for sales and tech as well. Now whatever values i get from there i save it to users table and save their id to my organization table.
First i want insert sales person information and then tech person
information and get their id and save it in organization
This is my code:
$this->data['company'] = $this->company_m->get_new();
$this->data['user'] = $this->secure_m->get_new();
$rules = $this->company_m->rules_admin;
$this->form_validation->set_rules($rules);
if ($this->form_validation->run() == TRUE)
{
$data =$this->secure_m->array_from_post(array('first_name','last_name','email'));
$sales_id = $this->secure_m->save($data,$id);
$data =$this->secure_m->array_from_post(array('first_name','last_name','email'));
$tech_id = $this->secure_m->save($data,$id);
$data = $this->company_m->array_from_post(array('org_name','dba','addr1','addr2','city','state','country','pin','sales_id','tech_id','tax_number','comment','url'));
$data['sales_id']= $sales_id;
$data['tech_id']= $tech_id;
$this->company_m->save($data, $id);
redirect('admin/company');
}
// Load the view
$this->data['subview'] = 'admin/company/add';
$this->load->view('admin/_layout_main', $this->
Array from POST code
public function array_from_post($fields){
$data = array();
foreach ($fields as $field) {
$data[$field] = $this->input->post($field);
}
return $data;
}
You're trying to get values from wrong keys. I.e. you have first_name_sales and first_name_tech, but always read from first_name.
Try
$data_tech = $this->secure_m->array_from_post(array('first_name_tech','last_name_tech','email_tech','tech'));
// ...
$data_sales = $this->secure_m->array_from_post(array('first_name_sales','last_name_sales','email_sales','sales'));
// ...
Also you should set_value from corresponding objects instead of same $user. Pass like $user_tech and $user_sales to view. Smth like
$this->load->view('admin/_layout_main', ['user_tech' => $data_tech, 'user_sales' => $data_sales]);
$values = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email' => $this->input->post('email'),
'user_type' => $this->input->post('user_type'),
);
$data = $this->model->inset('table_name', $values);
And also use this array to many tables
This is the first time i create my own webservice (someone always did it for me before), so please bear with me.
I post this array :
$data = array(
'user_id' => $this->post('user_id'),
'group_id' => $this->post('group_id'),
'child_id' => $this->post('child_id'), //will be nested array
'custom' => $this->post('custom'),
'time' => $this->post('time'),
'date' => $this->post('date')
);
I tried to create a nested array with this : $this->post('child_id'), because user can post multiple child_id at once.
Then i tried to iterate through the child_id, because i need to insert them to the mysql :
for($i = 0; $i < sizeof($data['child_id']); $i++)
{
$result2 = $this->schedule_m->add_trans('transaction_schedule', $data, $result_id[0]['id']);
}
What should i do, so i can have an array of child_id in my $data array? (nested array)
And how to iterate through it?
UPDATE :
I have updated the codes above.
I use advanced rest client for testing, and i tried to post something like this in the form content type :
child_id=1&user_id=1&group_id=1&custom=&time=17%3A17%3A00&date=&child_id=2
Notice that theres two child_id (left most and right most), but only the last one (right most) is inserted.
And this is the add_trans in the model :
function add_trans($table, $data, $schedule_id) {
$query = $this->db->insert($table, array('child_id' => $data['child_id'], 'schedule_id' => $schedule_id));
return $query;
}
Thanks a lot for your time.
Even thought you set the name attribute as child[] on the markup,
You still need to call it as:
'child_id' => $this->post('child_id')
It will still return an array.
for($i = 0; $i < sizeof($data['child_id']); $i++) {
$result2 = $this->schedule_m->add_trans('transaction_schedule', $data, $result_id[0]['id']);
}
EDIT:
Looking upon you query string, that seems to be the culprit:
child_id=1&user_id=1&group_id=1&custom=&time=17%3A17%3A00&date=&child_id=2
^ same index , same index, same index, it will overwrite and you will get only `2`
If you want to get them all into an array format, you need to set them like this
child_id[]=1&user_id=1&group_id=1&custom=&time=17%3A17%3A00&date=&child_id[]=2
^ it needs to be set like this
UPDATE:
And in your model, if you want each id per row, well you can also loop in this case:
function add_trans($table, $data, $schedule_id) {
foreach($data['child_id'] as $child_id) {
$query = $this->db->insert($table, array('child_id' => $child_id, 'schedule_id' => $schedule_id));
}
// return $this->db->insert_id();
return $query;
}
ofcourse that won't work, it has to be
for($i = 0; $i < sizeof($data['child_id']); $i++)
{
$result2 = $this->schedule_m->add_trans('transaction_schedule', $data['child_id'][$i], $result_id[0]['id']);
}
because you've not set $data['child_id[]'] so it doesn't exist, the key is just a string or number, it does not validate or parse anything
you don't need to give child[] in post method. just give only child, it will get complete array what are you sending from views
replace
'child_id' => $this->post('child_id[]')
with
'child_id' => $this->post('child_id')