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
Related
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[]">
Using php-mysql, I am fetching data, pushing it into an array. An array consists of 486 records. It's an associative array with 7 column for each record.
When there is a GET request, it works fine. Getting data, binding it to table, chart and dropdown. Everything works fine.
I need to populate dropdown based on selection of another dropdown. And in that case I am making a POST request. And searching in the same array of 486 records.
$temp = Array();
$teamSelectData = Array();
foreach ($allBookingsData as $key => $value) {
if($value['PDG'] == $passedPDG){
array_push($temp, $value["Team_Name"]);
}
}
$tempTeam = array_iunique($temp);
foreach ($tempTeam as $key => $value) {
array_push($teamSelectData, Array(
'name' => $value,
'value' => $key,
'title' => $value
));
}
$returnArray['TeamSelectData'] = $teamSelectData;
// get unique items from array
function array_iunique($array) {
$upper = array_map('strtolower', $array);
return array_intersect_key($array, array_unique($upper));
}
I couldn't able to figure out why is it taking too much time to execute. The comparison if($value['PDG'] == $passedPDG) is the issue or the function array_iunique. And the same way I am populating dropdown for PDG. Based on a selection of PDG, I need to fill dropdown of Team.
How to make this function efficient ?
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);
I'm using codeigniter.
I'm trying to compare some posted values from a form with entries from a database.
Put simply, i want to check to see if the entry is already in the database, if so ignore the posted data, but if there is no database entry then add to the database.
My thinking was that it shouldn't actually be that hard, but having some issues, and now im completely confused. Any pointers in the right direction would be appreciated.
I have an array coming from POST $assigned_ids
And i want to compare that with the data from $assign_data which is being output from the database.
I've been trying foreach loops, looping over the posted data, and then inside that looping through the database and comparing and adding if neccessary.
It works upto a point, but if there is data coming from the database, its adding multiple entires.
Heres my code, surely im over complicating things?
// posted values foreach - loop through all posted values
foreach($assigned_ids as $id) {
if(is_array($assign_data) && count($assign_data) > 0) {
// query all data in assignments table
foreach($assign_data as $key => $value) {
// is the user id from assignments table in posted id's
if(in_array($value->user_id, $id)){
// if it is, then do the course id's match as well? if so, do nothing, already an entry
if($value->course_id == $course_id) {
echo "match id and course, do nothing";
} else {
// else if there isnt an entry for this user for this course, add the entry
$add_data = array(
'user_id' => $value->user_id,
'course_id' => $course_id,
'org_id' => $org_id
);
$this->assignment_model->save_org_assignments($add_data);
}
} else {
// the user id was not in the array from the db, but was in the posted vars, so log in db
$add_data = array(
'user_id' => $id,
'course_id' => $course_id,
'org_id' => $org_id
);
$this->assignment_model->save_org_assignments($add_data);
}
}
} else {
$add_data = array(
'user_id' => $id,
'course_id' => $course_id,
'org_id' => $org_id
);
$this->assignment_model->save_org_assignments($add_data);
}
}
I think your main issue is your array is not properly structured that's why your having a hard time.
My opinion is to predefined your db result after fetching it.
function getAssignedData(){
// its better to get the only field you'll need than to fetch everything
$result = $this->db->select($field)->get();
if($result->num_rows()){
$existing_ids = [];
foreach($result->result() as $row){
$existing_ids[] = $row->$field;
}
return array_flip($existing_ids);
}
return FALSE;
}
And you can already compare the values like this
foreach($assigned_ids as $id)
{
if(!isset($existing_ids[$id])) {
// do something
}
}
Hope that helps.
Hello Every one i have a function in controller which is checkout where i am submitting a form.
But i want to run insert query in a loop on bellow $this->Session->read('Cart') value is 3 but my code insert one record in table if i alert something inside loop it alerts 3 time how can i run insert query 3 time please any one help me.
Thanks in advance.
public function checkout() {
$this->loadModel("Checkout");
Configure::read();
$aorderno = $this->Checkout->find('all', array('order' => array('Checkout.id' => 'DESC'),'limit' => 1));
$this->set('aorderno',$aorderno);
foreach($aorderno as $order)
{
$myorderno=$order['Checkout']['orderno']+1;
}
$firstname = $this->request->data['checkout']['firstname'];
$this->set('firstname',$firstname);
$lastname = $this->request->data['checkout']['lastname'];
$this->set('lastname',$lastname);
if(count($this->Session->read('Cart'))>0)
{
foreach($this->Session->read('Cart') as $value)
{
$sku=$value['Product']['sku'];
$this->Checkout->save(array('orderno' => $myorderno,'firstname' => $firstname,'lastname' => $lastname'sku' => $sku));
}
}
}
I supposed in your $this->Checkout->saveQuery function.
You are using something like $this->save();
Before $this->save(), call $this->create();
Hope it helps.
You can use saveMany
foreach(.......){
$arr[]= array('orderno' => $myorderno,'firstname' => $firstname,'lastname' => $lastname'sku' => $sku);
}
$this->Checkout->saveMany($arr, array('deep' => true));