I'm trying to use Codeigniter to update my value once the user had submitted the form. But unfortunately, there was an error of undefined index for few of the variable which I store in an array.
Below is the Code that I use to pass from Controller to the Model
Controller Code :
$prep_data = array(
'full_name' => $this->input->post('full_name'),
'nric' => $this->input->post('nric'),
'dob' => $this->input->post('dob'),
'mobile_phone' => $this->input->post('mobile_phone'),
'email' => $this->input->post('email'),
'address_line_1' => $this->input->post('address_line_1'),
'address_line_2' => $this->input->post('address_line_2'),
'postcode' => $this->input->post('postcode'),
'state' => $this->input->post('state'),
'current_brand' => $this->input->post('current_brand'),
'pregnant' => $isPregnant, //Edit by Marcus
'breastfeeding' => $isBreastfeeding, //Added by Marcus
'child_full_name' => $this->input->post('child_full_name'),
'child_dob' => $this->input->post('child_dob'),
'child_current_brand' => $this->input->post('child_current_brand'),
'expected_give_birth_date' => $this->input->post('expected_give_birth_date'),
'image' => '',
'quantity_purchase' => $this->input->post('quantity_purchase'),
'foc_amount' => $this->input->post('foc_amount'),
'marketing_opt' => $this->input->post('marketing_opt'),
'centre_id' => get_cookie('abbott_centre', TRUE),
'user_id' => $this->session->id,
'sampling_day' => $centre_data['sampling_day'],
'created_at' => $datetime,
'updated_at' => $datetime
);
$this->entry_model->update_entry($prep_data);
Model Code :
function update_entry($data){
echo print_r($data);
if(!empty($data['full_name'])) $this->db->set('full_name', $data['full_name']);
if(!empty($data['nric'])) $this->db->set('nric', $data['nric']);
if(!empty($data['dob'])) $this->db->set('dob', $data['dob']);
if(!empty($data['mobile_phone'])) $this->db->set('mobile_phone', $data['mobile_phone']);
if(!empty($data['email'])) $this->db->set('email', $data['email']);
if(!empty($data['address_line_1'])) $this->db->set('address_line_1', $data['address_line_1']);
if(!empty($data['address_line_2'])) $this->db->set('address_line_2', $data['address_line_2']);
if(!empty($data['postcode'])) $this->db->set('postcode', $data['postcode']);
if(!empty($data['state'])) $this->db->set('state', $data['state']);
if(!empty($data['current_brand'])) $this->db->set('current_brand', $data['current_brand']);
if(!empty($data['pregnant'])){ //Fix by Marcus
$this->db->set('pregnant', $data['pregnant']);
}else {
$this->db->set('pregnant', 0);
}
if(!empty($data['breastfeeding'])){ //Fix by Marcus
$this->db->set('breastfeeding', $data['breastfeeding']);
}
else {
$this->db->set('breastfeeding', 0);
}
if(!empty($data['expected_give_birth_date'])) $this->db->set('expected_give_birth_date', $data['expected_give_birth_date']);//Fix by Marcus
if(!empty($data['child_full_name'])) $this->db->set('child_full_name', $data['child_full_name']);
if(!empty($data['child_dob'])) $this->db->set('child_dob', $data['child_dob']);
if(!empty($data['child_current_brand'])) $this->db->set('child_current_brand', $data['child_current_brand']);
if(!empty($data['image'])) $this->db->set('image', $data['image']);
if(isset($data['quantity_purchase'])) $this->db->set('quantity_purchase', $data['quantity_purchase']);
if(isset($data['foc_amount'])) $this->db->set('foc_amount', $data['foc_amount']);
if(!empty($data['marketing_opt'])) $this->db->set('marketing_opt', $data['marketing_opt']);
if(!empty($data['centre_id'])) $this->db->set('centre_id', $data['centre_id']);
if(!empty($data['user_id'])) $this->db->set('user_id', $data['user_id']);
if(!empty($data['created_at'])) $this->db->set('created_at', $data['created_at']);
if(!empty($data['updated_at'])) $this->db->set('updated_at', $data['updated_at']);
$this->db->where('id', $data['id']);
$this->db->update('entry');
}
Other $data[] value had no problem but only the 'pregnant' and 'breastfeeding' had the error on Undefined Index. I did try to print_r to show the $data value and all are seem to be alright.
Below are the output :
Array ( [full_name] => e2e [nric] => 123 [dob] => 2019-07-09 [mobile_phone] => 1231231231 [email] => [address_line_1] => [address_line_2] => [postcode] => [state] => [current_brand] => [pregnant] => 0 [breastfeeding] => 1 [child_full_name] => [child_dob] => 0000-00-00 [child_current_brand] => [expected_give_birth_date] => 2019-07-09 [image] => [quantity_purchase] => 123123 [foc_amount] => 123 [sampling_day] => [updated_at] => 2019-07-10 19:27:43 [id] => 8 ) 1Array ( [image] => 8_20190710_192743.jpg [id] => 8 ) 1
What will be the caused for this 'pregnant' and 'breastfeeding' array? As it keeps getting NULL as a return.
Thanks
You need to use isset() to check either index set or not like
$isPregnant = 0;
$isBreastfeeding = 0;
if(isset($_POST['pregnant']) && $_POST['pregnant'] == 1)
{
$isBreastfeeding = 0;
$isPregnant = 1; //Is breastfeeding selected
}
if(isset($_POST['breastfeeding']) && $_POST['breastfeeding'] == 1)
{
$isPregnant = 0;
$isBreastfeeding = 1; //Is breastfeeding selected
}
Related
i have 2 products in my cart session. and want me to combine in array data_order. but read only 1 product. actually this data I will send in an email so from that I make the format of the data like this.
my controller :
foreach ($this->cart->contents() as $item){
if($item['diskon'] == "0"){
$harga_before = $item['before'];
$harga_after = $item['price'];
}else{
$harga_before = $item['before'];
$harga_after = $item['price'];
}
foreach ($this->cart->product_options($item['rowid']) as $option_name => $option_value){
if(empty($option_value)){
$option_size = "";
$option_color = "";
}else{
$option = $item['options'];
$option_size = $option['Size'];
$option_color = $option['Warna'];
}
}
$data = array(
'inv' => $invoice,
'tglOrdercs' => date('d F Y H:i:s'),
'tglExp' => $date_maju,
'nmlkp' => $nmlkpi,
'almtkp' => $address,
'noTelp' => $notelp271,
'methode' => $method,
'bnk_option' => $banking_select,
'data_order' => array(
'nama_produk' => $item['name'],
'artikel' => $item['artikel'],
'diskon' => $item['diskon'],
'ukuran' => $option_size,
'warna' => $option_color,
'qty' => $item['qty'],
'harga_fix' => $harga_after,
'harga_before' => $harga_before,
),
);
}
my result :
Array(
[inv] => STJOHB9261JR
[tglOrdercs] => 28 September 2017 11:23:33
[tglExp] => 28 September 2017 21:23:33
[nmlkp] => danny setyawan
[almtkp] => Perum puspa sari H22 sidoarjo
[noTelp] => 089263423647
[methode] => Transfer
[bnk_option] => Bank BNI
[data_order] => Array
(
[nama_produk] => Sonic TL Brown
[artikel] => CH2002-831
[diskon] => 40
[ukuran] => 39
[warna] => Coklat
[qty] => 2
[harga_fix] => 125340
[harga_before] => 208900
)
)
ignore ">", this result from browser console.
I'm trying to figure out how to convert the array_map portion with the anonymous function to just a function while using the use function to be able to support php 5.2 but i keep getting an error. Here is my current code.
<?php
$collection = array();
$op_field = array(
'fname' => 'sarmen',
'lname' => 'b',
'age' => 33,
'gender' => 'male'
);
$nf_field = array(
'type' => 'human',
'age' => 30,
'gender' => 'male',
'ethnicity' => 'american'
);
array_map(function($op, $nf) use (&$collection)
{
$collection[] = array(
'op' => $op,
'nf' => $nf
);
}, $op_field, $nf_field);
print_r($collection);
I've tried
function mapping($op, $nf)
{
$collection[] = array(
'op' => $op,
'nf' => $nf
);
return $collection;
}
array_map(mapping($op, $nf), use ($&collection), $op_field, $nf_field);
But that just gives a parse error. Any idea on how it would be written? I really appreciate it.
There's no need to pass in a reference to $collection, instead just do the following:
function mapping($op, $nf)
{
return array(
'op' => $op,
'nf' => $nf
);
}
$collection = array_map('mapping', $op_field, $nf_field);
Yields:
Array
(
[0] => Array
(
[op] => sarmen
[nf] => human
)
[1] => Array
(
[op] => b
[nf] => 30
)
[2] => Array
(
[op] => 33
[nf] => male
)
[3] => Array
(
[op] => male
[nf] => american
)
)
Hope this helps :)
First off you have to pass the callback as string when you want to use a normal function. Second there is no use before PHP 5.3, so you would have to use global.
And if you put everything together you will get:
function callbackFunction($op, $nf){
global $collection;
$collection[] = array(
'op' => $op,
'nf' => $nf
);
}
array_map("callbackFunction", $op_field, $nf_field);
when I insert an array in the db, I get the error "unidentified index" in some of the variables even though they all have their corresponding values. Tried using print_r, and it returned an array with values, but when I try to insert the array, the values become null. I dont understand why and I can't see the error in my code.
here's my controller
public function test()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$user_id = $session_data['user_id'];
$table_id = $this->input->post('table');
$start = $this->input->post('start');
$end = $this->input->post('end');
$query = $this->process_resto_list->test($table_id);
if ($query->num_rows() > 0)
{
$row = $query->row();
$resto_id = $row->resto_id;
$table_number= $row->table_number;
$table_size = $row->table_size;
}
//$resto_id = $session_data['user_id'];
$values = array(
'resto_id' => $resto_id,
'diner_id' => $user_id,
'date' => $this->input->post('date'),
'start_time' => $start,
'end_time' => $end,
'table_number' => $table_number,
'number_of_people' => $table_size,
);
if($this->process_resto_list->insert_reservation($values))
redirect('reservation_test/reservation', 'location');
//print_r($values);
}
Here's my model
public function insert_reservation($values)
{
date_default_timezone_set("Asia/Manila");
$data = array(
'resto_id' => $values['resto_id'],
'diner_id' => $values['user_id'],
'date' => date('Y-m-d'),
'start_time' => $values['start'],
'end_time' => $values['end'],
'table_number' => $values['table_number'],
'number_of_people' => $values['table_size'],
);
return $this->db->insert('resto_reservations', $data);
}
When using print_r here's the result of the array
Array ( [resto_id] => 9 [diner_id] => 14 [date] => [start_time] => 11:00 [end_time] => 13:00 [table_number] => 6 [number_of_people] => 4 )
Please help me point out the error. Thank you
In your model. Change values as follows
$data = array(
'resto_id' => $values['resto_id'],
'diner_id' => $values['diner_id'],
'date' => date('Y-m-d'),
'start_time' => $values['start_time'],
'end_time' => $values['end_time'],
'table_number' => $values['table_number'],
'number_of_people' => $values['number_of_people'],
);
I just replaced my model with this. Thanks everyone!
$data = array(
'resto_id' => $values['resto_id'],
'diner_id' => $values['diner_id'],
'date' => date('Y-m-d'),
'start_time' => $values['start_time'],
'end_time' => $values['end_time'],
'table_number' => $values['table_number'],
'number_of_people' => $values['number_of_people'],
);
I want to insert data into my database. I am using codeigniter framework to build my app.
When I click on submit button,It don't give any error just reload the same page and data in not inserted in database.
Following my code to insert data into database -
public function addSale($saleDetails = array(), $items = array(), $warehouse_id)
{
foreach($items as $data){
$product_id = $data['product_id'];
$product_quantity = $data['quantity'];
$this->updateProductQuantity($product_id, $warehouse_id, $product_quantity);
}
// sale data
$saleData = array(
'reference_no' => $saleDetails['reference_no'],
'warehouse_id' => $warehouse_id,
'biller_id' => $saleDetails['biller_id'],
'biller_name' => $saleDetails['biller_name'],
'customer_id' => $saleDetails['customer_id'],
'customer_name' => $saleDetails['customer_name'],
'date' => $saleDetails['date'],
'note' => $saleDetails['note'],
'internal_note' => $saleDetails['internal_note'],
'inv_total' => $saleDetails['inv_total'],
'total_tax' => $saleDetails['total_tax'],
'total' => $saleDetails['total'],
'total_tax2' => $saleDetails['total_tax2'],
'tax_rate2_id' => $saleDetails['tax_rate2_id'],
'inv_discount' => $saleDetails['inv_discount'],
'discount_id' => $saleDetails['discount_id'],
'user' => $saleDetails['user'],
'shipping' => $saleDetails['shipping']
);
if($this->db->insert('sales', $saleData)) {
$sale_id = $this->db->insert_id();
$addOn = array('sale_id' => $sale_id);
end($addOn);
foreach ( $items as &$var ) {
$var = array_merge($addOn, $var);
}
if($this->db->insert_batch('sale_items', $items)) {
return true;
}
}
return false;
}
Based on https://ellislab.com/codeigniter/user-guide/database/examples.html , it seems to me, that you are missing a line -> $this->db->insert('sales', $saleData); . You do have it inside an if statement, but if statement will not execute it as a code, but check if it returns true.
$saleData = array(
'reference_no' => $saleDetails['reference_no'],
'warehouse_id' => $warehouse_id,
'biller_id' => $saleDetails['biller_id'],
'biller_name' => $saleDetails['biller_name'],
'customer_id' => $saleDetails['customer_id'],
'customer_name' => $saleDetails['customer_name'],
'date' => $saleDetails['date'],
'note' => $saleDetails['note'],
'internal_note' => $saleDetails['internal_note'],
'inv_total' => $saleDetails['inv_total'],
'total_tax' => $saleDetails['total_tax'],
'total' => $saleDetails['total'],
'total_tax2' => $saleDetails['total_tax2'],
'tax_rate2_id' => $saleDetails['tax_rate2_id'],
'inv_discount' => $saleDetails['inv_discount'],
'discount_id' => $saleDetails['discount_id'],
'user' => $saleDetails['user'],
'shipping' => $saleDetails['shipping']
);
$this->db->insert('sales', $saleData);
This should work. You can also check if it succeeds like this ->
return ($this->db->affected_rows() != 1) ? false : true;
I tried doing the following and surprisingly it only populates the first table (new_guest) while writing only zero values to the other (new_reservation). How can I properly configure this?
Controller:
function guest_checks_in() {
$data_guest = array (
'guest_title' => $this->input->post('guest_title'),
'guest_name' => $this->input->post('guest_name'),
'guest_gender' => $this->input->post('guest_gender'),
'guest_phone' => $this->input->post('guest_phone'),
'guest_email' => $this->input->post('guest_email'),
'guest_address' => $this->input->post('guest_address'),
'guest_province' => $this->input->post('guest_province'),
'guest_country' => $this->input->post('guest_country'),
'guest_postal_code' => $this->input->post('guest_postal_code'),
'guest_dob' => $this->input->post('guest_dob'),
'guest_nic_pp_dl' => $this->input->post('guest_nic_pp_dl')
);
$data_reservation = array (
'room_type' => $this->input->post('room_type'),
'meal_type' => $this->input->post('meal_type'),
'bed_type' => $this->input->post('bed_type'),
'ext_beds' => $this->input->post('ext_beds'),
'number_of_guests' => $this->input->post('number_of_guests'),
'start_date' => $this->input->post('start_date'),
'end_date' => $this->input->post('end_date'),
'reservation_duration' => $this->input->post('reservation_duration'),
'room_number' => $this->input->post('room_number'),
'total' => $this->input->post('total')
);
$this->reservations_model->add_guests($data_guest);
$this->reservations_model->add_reservations($data_reservation);
$this->confirm_check_in();
}
Model:
class Reservations_model extends CI_Model {
function add_guests($data_guest) {
$this->db->insert('new_guest', $data_guest);
return;
}
function add_reservations($data_reservation) {
$this->db->insert('new_reservation', $data_reservation);
return;
}
}
it will be good if you use transaction. if data in both tables must be entered.
function guest_checks_in() {
$data_guest = array (
'guest_title' => $this->input->post('guest_title'),
'guest_name' => $this->input->post('guest_name'),
'guest_gender' => $this->input->post('guest_gender'),
'guest_phone' => $this->input->post('guest_phone'),
'guest_email' => $this->input->post('guest_email'),
'guest_address' => $this->input->post('guest_address'),
'guest_province' => $this->input->post('guest_province'),
'guest_country' => $this->input->post('guest_country'),
'guest_postal_code' => $this->input->post('guest_postal_code'),
'guest_dob' => $this->input->post('guest_dob'),
'guest_nic_pp_dl' => $this->input->post('guest_nic_pp_dl')
);
$data_reservation = array (
'room_type' => $this->input->post('room_type'),
'meal_type' => $this->input->post('meal_type'),
'bed_type' => $this->input->post('bed_type'),
'ext_beds' => $this->input->post('ext_beds'),
'number_of_guests' => $this->input->post('number_of_guests'),
'start_date' => $this->input->post('start_date'),
'end_date' => $this->input->post('end_date'),
'reservation_duration' => $this->input->post('reservation_duration'),
'room_number' => $this->input->post('room_number'),
'total' => $this->input->post('total')
);
$this->reservations_model->add_all_guests($data_guest,$data_reservation);
$this->confirm_check_in();
}
in model
function add_all_guests($data_guest,$data_reservation) {
$this->db->trans_begin();
$this->db->insert('new_guest', $data_guest);
$this->db->insert('new_reservation', $data_reservation);
if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
return false;
}
else
{
$this->db->trans_commit();
return true;
}
}