How to split two array and pass to ajax using Jquery? - php

I am getting an error while inserting into my database.
Its showing column ARRAY. How to fix it? Instead of that I want to pass all dates (showing in the screenshot only dates in the date column)
I want to know in code where I did mistake. Why the array is coming like this?
PHP MODAL
$data_jobschedule = array(
'jobschedule_id' => $jobschedule_id,
'Activity_area_id' => $Activity_area_id,
'Contract_id' => $this->input->post('getcontract_id'),
'job_freq_id' => $this->input->post('getcontractbranch_freq'),
'job_schedule_dates' => $this->input->post('getschedule'),
//'job_schedule_frequency' => $this->input->post('getjob_schedule_frequency'),
'created_at' =>$created_Dt
);
$insert_id = 0;
if ($this->db->insert("activity_area", $data)) { //AM INSERTING ANOTHER RECORD ALSO TO DIFFERENT TABLE
$this->db->insert('job_schedule', $data_jobschedule); //HERE IS THE TABLE I NEED TO ADD DATES AND FREQUENCY
$insert_id = $this->db->insert_id();
}

so your post data is an array, you need looping through it.
on your php model, try change this
$data_jobschedule = array(
'jobschedule_id' => $jobschedule_id,
'Activity_area_id' => $Activity_area_id,
'Contract_id' => $this->input->post('getcontract_id'),
'job_freq_id' => $this->input->post('getcontractbranch_freq'),
'job_schedule_dates' => $this->input->post('getschedule'),
//'job_schedule_frequency' => $this->input->post('getjob_schedule_frequency'),
'created_at' =>$created_Dt
);
$insert_id = 0;
if ($this->db->insert("activity_area", $data)) { //AM INSERTING ANOTHER RECORD ALSO TO DIFFERENT TABLE
$this->db->insert('job_schedule', $data_jobschedule); //HERE IS THE TABLE I NEED TO ADD DATES AND FREQUENCY
$insert_id = $this->db->insert_id();
}
to
$insert_id = 0;
if ($this->db->insert("activity_area", $data)) { //AM INSERTING ANOTHER RECORD ALSO TO DIFFERENT TABLE
if (is_array($this->input->post('getschedule'))) {
foreach($this->input->post('getschedule') as $value) {
$this->db->insert('job_schedule', array(
'jobschedule_id' => $jobschedule_id,
'Activity_area_id' => $Activity_area_id,
'Contract_id' => $this->input->post('getcontract_id'),
'job_freq_id' => $this->input->post('getcontractbranch_freq'),
'job_schedule_dates' => $value[0], //assume array form like your screenshot
'job_schedule_frequency' => $value[1],
'created_at' =>$created_Dt
));
}
$insert_id = $this->db->insert_id();
}
}

Related

Laravel UpdateOrCreate To Be Different values on Create on Update

Is there something like UpdateOrCreate I can use to have different values on create and on update. Meaning if it’s updating i don’t want it to change a certain column but i want it to insert the first time.
eg
$person = ['name'=> 'John', 'age' => 6, 'joined' => '2017-01-01'];
If John exists update age. If it doesn’t insert age and joined;
People::updateOrCreate(
['name' => $person['name']],
['age' => $person['age'], 'joined' => $person['joined']]
)
will update joined every time and i don't wanna change it.
You can use updateOrNew which doesn't persist it to the database and then check if it is not already in the database you can change the property you want to change.
$record = People::updateOrNew(
['name' => $person['name']],
['age' => $person['age']]
);
if(!$record->exists()) {
$record->joined = $person['joined'];
}
$record->save();
But if you are only trying to get the record if it exists and create it if it doesn't, use firstOrCreate() instead. That won't affect existing records, only return them, and it will create the record if it doesn't exist.
There is no any other option else you can try following:
$person = People::where('name', $name)->first();
if (!is_null($person)) {
$person->update([
'age' => $age
]);
}else{
$person = People::create([
'name' => $name,
'age' => $age,
'joined' => date('Y-m-d')
]);
}

How to alter next sequence number with last increment value using PHP?

I want to alter next sequence number after for each loop.How can i get the last incremented number using PHP?
$incjobid = $job_Id++;//1,2,3,4 and i need to alter the next sequence number with 5.
At a time am inserting 4 records with id 1,2,3,4 and it should be next sequence number should be 5.How to set this?
$this->db->select('NEXT VALUE FOR job_seq as jobid');
$queryjobid = $this->db->get();
foreach ($queryjobid->result_array() as $row) {
$job_Id = $row['jobid'];
}
if (is_array($this->input->post('getschedule'))) {
foreach($this->input->post('getschedule') as $value) {
//Auto generate JOB SCHEDULE HERE
$this->db->insert('job_schedule', array(
'jobschedule_id' => $jobschedule_id,
'Activity_area_id' => $Activity_area_id,
'Contract_id' => $this->input->post('getcontract_id'),
'job_freq_id' => $this->input->post('getcontractbranch_freq'),
'job_schedule_dates' => $value[0], //assume array form like your screenshot
'job_schedule_frequency' => $value[1],
'created_at' =>$created_Dt
));
//Auto generate JOB HERE
$incjobid = $job_Id++;
$this->db->insert('job', array(
'Job_id' => $incjobid,
'Job_no' => $incjobid,
'Contract_id' => $this->input->post('getcontract_id'),
'Job_Date' => $value[0],
'Start_time' => '08:30',
'Activity_Area_id' => $Activity_area_id,
'created_Dt' => $created_Dt,
'status' => 'open',
'sr_available' => '0'
//'end_time' => $this->input->post('getEnd_Time') ?: null,
//'team_id' => $this->input->post('getassignedtoJob'),
//'type_of_services_id' => $this->input->post('gettypeofserviceJob'),
//'type_of_visit_id' => $this->input->post('gettypeofvisitjob')
));
}
$insert_id = $this->db->insert_id();
//$this->db->ALTER('SEQUENCE job_seq RESTART WITH 547');
//$queryjobalter = $this->db->get();
//$queryjobalter->result();
}

How to merge exists data with new data?

Method 1 : Here I wrote the code for insert booking seat data into the database
Problem : When I book new seat it will creating new row so I'm getting duplicate rows so I tried method 2
Method 1 code :
$booking = new Bookings();
$booking->users_id = 4;
$booking->schedules_id = $schedules_id;
$booking->buses_id = $buses_id;
$booking->routes_id = $routes_id;
$booking->seat = implode(',', $seat);
$booking->price = $request->price;
$booking->profile = 'pending';
Method 2 : Here checking schedules_id equal to exists schedules_id then update seat and other data's
Problem : Insert new data updating old data
Method 2 code :
$booking = Bookings::updateOrCreate(
['schedules_id' => $schedules_id], // match the row based on this array
[ // update this columns
'buses_id' => $buses_id,
'routes_id' => $routes_id,
'seat' => json_encode($seat),
'price' => $request->price,
'profile' => 'pending',
]
);
// I don't know this is logically correct or wrong
My idea : Here I'm retrieving old data and storing into one variable then merging old data and new data into one column
Problem : Getting error.
My idea code :
$extSeat = DB::table('bookings')->select('seat')->get();
$booking = Bookings::updateOrCreate(
['schedules_id' => $schedules_id],
[ // update this columns
'buses_id' => $buses_id,
'routes_id' => $routes_id,
'seat' => implode(",", array_merge($seat,$extSeat)),
'price' => $request->price,
'profile' => 'pending',
]);
what i actually need ? : i need merge exists data with new data without updating.
Old data look like A1,B1
when insert new data like C1
i need data like this A1,B1,C1
I hope I explain clear enough. Any help is appreciated, thank you.
I don't know this is a correct logic or not but works for me, any other suggestions are welcome.
$extSeat = DB::table('bookings')->select('seat')->first();
$extSeat = explode(",", $extSeat->seat);
$booking = Bookings::updateOrCreate(
['schedules_id' => $schedules_id],
[ // update this columns
'buses_id' => $buses_id,
'routes_id' => $routes_id,
'seat' => implode(",", array_merge($seat,$extSeat )),
'price' => $request->price,
'profile' => 'pending',
]);

Insert jQuery repeater row in PHP database

I have 1 row having 5 form fields. User can add/remove rows. Its repeatable row.
Now i want to store these fields into database with PDO php.
For normal values i am using this code but i am confused for repeater field.
$data = array(
'bill_no' => trim($_REQUEST['bill_no']),
'from_name' => trim($_REQUEST['from_name']),
'to_name' => trim($_REQUEST['to_name']),
'date' => trim($_REQUEST['date_bill']),
'mr_or_ms' => trim($_REQUEST['mr_or_ms']),
);
if($crud->InsertData("bill",$data)) {
header("Location: add-bill.php");
}
Insert Function:
public function InsertData($table,$fields) {
$field = array_keys($fields);
$single_field = implode(",", $field);
$val = implode("','", $fields);
try {
$query = $this->db->prepare("INSERT INTO ".$table."(".$single_field.") VALUES('".$val."')");
$query->execute();
return true;
} catch(PDOException $e) {
echo "unable to insert data";
}
}
Please help me to insert fields. Thanks
Change the names of your form fields, add [] to the end to get PHP arrays. For example change bill_no to bill_no[]. Something like this:
foreach($_REQUEST['bill_no'] as $row_number => $row_content){
$data = array(
'bill_no' => trim($_REQUEST['bill_no'][$row_number]),
'from_name' => trim($_REQUEST['from_name'][$row_number]),
'to_name' => trim($_REQUEST['to_name'][$row_number]),
'date' => trim($_REQUEST['date_bill'][$row_number]),
'mr_or_ms' => trim($_REQUEST['mr_or_ms'][$row_number]),
);
$crud->InsertData("bill",$data);
}
This assumes the browser is not mixing up the order of the fields, so maybe it's better to add unique names to the form fields when adding rows.
Also, there's no input data validation at all, please ensure you are escaping all data properly.
I did it with this method.
$total=count($_POST['description']);
for($i=0; $i<$total; $i++){
$data1 = array(
'bill_no' => trim($_POST['bill_no']),
'description' => trim($_POST['description'][$i]),
'nos' => trim($_POST['nos'][$i]),
'nos_day' => trim($_POST['nos_day'][$i]),
'pay' => trim($_POST['pay'][$i]),
'weekly_off' => trim($_POST['weekly'][$i]),
'hra' => trim($_POST['hra'][$i]),
'rs' => trim($_POST['rs'][$i]),
'ps' => trim($_POST['ps'][$i]),
);
$crud->InsertData("bill_details",$data1);
}

Error in getting the last inserted ID of a query using batch insert in CodeIgniter

How can I get the last inserted ID of a query using the batch insert in CodeIgniter. I used the code $this->db->insert_id() but it returns the ID of my first inserted array. I can't get the last insert.
Here's what I did:
for ($x = 0; $x < sizeof($filtername); $x++) {
$orders[] = array(
'poid' => null,
'order_id' => $poid,
'item_desc' => $filtername[$x],
'item_qty' => $filterquantity[$x],
'item_price' => $filterprice[$x],
'total' => $filtertotal[$x],
'cash_on_delivery' => $val_delivery,
'is_check' => $val_check,
'bank_transfer' => $val_transfer,
'transaction_date' => $dateorder
);
}
$this->db->insert_batch('po_order', $orders);
echo $this->db->insert_id(); //will return the first insert array
I can't spot where's my error. My last option is to get it using a query. I also did mysql_insert_id() but always returns to 0.
I think the best way would be to use the batch insert instead of individual inserts in a loop for performance , but to get the last insert id, ADD the First Insert ID & the Affected Rows.
$this->db->insert_batch('po_order', $orders);
$total_affected_rows = $this->db->affected_rows();
$first_insert_id = $this->db->insert_id();
$last_id = ($first_insert_id + $total_affected_rows - 1);
You will need to do something like this,
$insertIds = array();
for ($x = 0; $x < sizeof($filtername); $x++) {
$orders = array(
'poid' => null,
'order_id' => $poid,
'item_desc' => $filtername[$x],
'item_qty' => $filterquantity[$x],
'item_price' => $filterprice[$x],
'total' => $filtertotal[$x],
'cash_on_delivery' => $val_delivery,
'is_check' => $val_check,
'bank_transfer' => $val_transfer,
'transaction_date' => $dateorder
);
$this->db->insert('po_order', $orders);
$insertIds[$x] = $this->db->insert_id(); //will return the first insert array
}
print_r($insertIds); //print all insert ids

Categories