In my php code I am trying to collect some data each time a user clicks a particular button. However even though the data is avaiable everytime the button is executed only the latest data is added to the array.
How can I keep adding this new data while keeping the old data
$session_data['items'] = array('id' => $id,'price' => $price);
$this->session->set_userdata($session_data);
edit
Updated code with suggested answer however still no outcome
$session_data['items'][]= array('id' => $id,'price' => $price);
$this->session->set_userdata($session_data);
You can use an 2-dimensional array data type. This way, you can have a lot of data inserted and get the latest value too.
$session_data['items'][] = array('id' => $id, 'price' => $price);
$session_data['items'][] = array('id' => $id, 'price' => $price + 1);
If you keep adding it the above way, it pushes new data to the end. In the above code, both the values are saved.
Use CodeIgnighter's push() method to add to a session variable that contains an array.
$this->session->push('items', array('id' => $id, 'price' => $price));
Related
This question already has an answer here:
CI update_batch; Concatenate and increment variable and string as array value
(1 answer)
Closed 19 days ago.
I need to increase the value of a field in my CodeIgniter database.
I understand that CI allows not to escape values, setting as FALSE the third parameter in $this->db->set().
I am inserting multiple data from an array, and as I understand, it would be not much secure to escape them all (data is coming from form), since I only need increasing in one field.
How can I avoid inserting this value separately?
My current code:
$dataCliente = array(
'nombre' => $nombre,
'email' => $email,
'genero' => $genero,
'reserva_next' => $fecha,
'reserva_next_id' => $reserva_id,
'reserva_lastreq' => time(),
'reservas' => "reservas+1"
);
$this->db->where('telefono', $telefono);
$this->db->update('clientes', $dataCliente);
EDIT: I HAVE TESTED WITH SIMPLE AND DOUBLE QUOTES, IN BOTH CASES THE VALUE OF THE FIELD IS SAVED AS 0 (ZERO)
The previous code does not produce the expected result. But this works:
$dataCliente = array(
'nombre' => $nombre,
'email' => $email,
'genero' => $genero,
'reserva_next' => $fecha,
'reserva_next_id' => $reserva_id,
'reserva_lastreq' => time(),
//'reservas' => "reservas+1"
);
$this->db->where('telefono', $telefono);
$this->db->update('clientes', $dataCliente);
$this->db->set('reservas', 'reservas+1', FALSE);
$this->db->where('telefono', $telefono);
$this->db->update('clientes', $dataCliente);
My intention is to use only one statement to insert data.
Is it possible not to escape a single element of the array, using the first code?
Thank you very much!
The simple answer is No. It's a very simple array.
You can see that the SET statement has 3 parameters. How are you planning on adding that into your data array.
The other reasons probably are...
It's already covered by using set with false.
It's a royal pain in the butt to add more fields in the array and have to parse them to determine which one requires the false flag.
Using Set as you have is perfectly acceptable and when you think about it in your case...
Each time you perform an update to an entry you will always increment the "reservas" value. This isn't and definitely should not be a part of your "Data" you wish to update from your Form. It is a function of the update itself.
So that kind of leads into... lets make a function. This isn't really part of the answer to your question but I have added it as simply something to ponder over.
You can put this where ever you like but to keep it simple I'll assume you have a client controller and I'll put it all in there for this example...
So set up your $telefono and $dataCliente as only you know where that comes from... Then create a simple single function.
private function update_client_details($data, $telefono) {
$this->db->set('reservas', 'reservas+1', FALSE);
$this->db->where('telefono', $telefono);
$this->db->update('clientes', $data);
}
And call the above method to perform the update. It's become One Statement.
It's a good idea to make this private if its in the controller. In a Model it would need to be public.
use a single quote not double quote
$dataCliente = array(
'nombre' => $nombre,
'email' => $email,
'genero' => $genero,
'reserva_next' => $fecha,
'reserva_next_id' => $reserva_id,
'reserva_lastreq' => time(),
'reservas' => 'reservas+1'
);
see this link for further info
I have a quite complicated form, which has a lot of fields - everything works fine, but I also need to store a slug and path to thumbnail. The problem is, that when I am using store method and create a post, I need to dynamically create that slug and path based on the post ID (which is not created yet and therefore I don't have an ID).
This is the chunk of the code I use to store the main post data:
Post::forceCreate([
'title' => $title,
'slug' => create_url_slug($title, $id),
'thumbnail' => thumbPath($id),
'description' => request('description'),
'password' => bcrypt(request('password')),
'user_id' => get_user_id()
]);
Here I passed two functions -> create_url_slug and thumbPath. If I put these functions above this chunk of code, the error will be thrown because the ID does not exist yet. On the other hand, if I put these functions under this code, the error will also appear, because those functions would be undefined. Can I somehow use closures or divide this method to two parts?
Thanks anybody in advance.
A way to do this is to create the model and right after assigning the values for example
$post = Post::forceCreate([
'title' => $title,
'description' => request('description'),
'password' => bcrypt(request('password')),
'user_id' => get_user_id()
]);
$post->slug = create_url_slug($title, $post->id);
$post->thumbnail = thumbPath($post->id);
$post->save();
I do not recall if make creates an id for the Model else you could use Post::make and save a call to the database. Worth a try.
I was wondering if someone could help with this. I'm trying to do a php query to update post meta values on Wordpress. Pb : the string has to be very long and full of unorthodox characters like double quotes.
The result is somewhat strange as it gets updated but adds another serial around the value, breaking it, like this :
s:5086:"a:28:{i:0;a:7:.....
Here's the initial query, if someone could figure out how to make it compliant that'd be amazing :
update_post_meta( $product_id, '_wc_booking_pricing', 'a:28:{i:0;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:5:"0.027";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"1";s:2:"to";s:1:"3";}i:1;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.02914814815";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"4";s:2:"to";s:1:"4";}i:2;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:12:"0.0312962963";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"5";s:2:"to";s:1:"5";}i:3;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.03344444444";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"6";s:2:"to";s:1:"6";}i:4;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.03559259259";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"7";s:2:"to";s:1:"7";}i:5;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.03774074074";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"8";s:2:"to";s:1:"8";}i:6;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.03988888889";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"9";s:2:"to";s:1:"9";}i:7;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.04203703704";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"10";s:2:"to";s:2:"10";}i:8;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.04418518519";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"11";s:2:"to";s:2:"11";}i:9;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.04633333333";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"12";s:2:"to";s:2:"12";}i:10;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.04848148148";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"13";s:2:"to";s:2:"13";}i:11;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05062962963";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"14";s:2:"to";s:2:"14";}i:12;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05277777778";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"15";s:2:"to";s:2:"15";}i:13;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05492592593";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"16";s:2:"to";s:2:"16";}i:14;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05707407407";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"17";s:2:"to";s:2:"17";}i:15;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05922222222";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"18";s:2:"to";s:2:"18";}i:16;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06137037037";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"19";s:2:"to";s:2:"19";}i:17;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06351851852";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"20";s:2:"to";s:2:"20";}i:18;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06566666667";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"21";s:2:"to";s:2:"21";}i:19;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06781481481";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"22";s:2:"to";s:2:"22";}i:20;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06996296296";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"23";s:2:"to";s:2:"23";}i:21;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.07211111111";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"24";s:2:"to";s:2:"24";}i:22;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.07425925926";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"25";s:2:"to";s:2:"25";}i:23;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.07640740741";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"26";s:2:"to";s:2:"26";}i:24;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.07855555556";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"27";s:2:"to";s:2:"27";}i:25;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:12:"0.0807037037";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"28";s:2:"to";s:2:"28";}i:26;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.08285185185";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"29";s:2:"to";s:2:"29";}i:27;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:5:"0.085";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"30";s:2:"to";s:2:"30";}}');
First, it's worth noting that you are not doing a PHP query, but rather you are using WordPress functions (update_post_meta) to save this data. That is probably where the confusion lies.
The problem you are having is that you are passing in serialized data. In WordPress, even though it stores the data in the DB as serialized data, you do not have to serialize / unserialize yourself.
WordPress stores the data serialized "automatically". That means that when you call update_post_meta, you should simply pass in the array you are trying to save.
This is a minimal example based on your code above:
$data = array(
array(
'type' => 'blocks',
'cost' => NULL,
'modifier' => NULL,
'base_cost' => 0.027,
'base_modifier] => 'times',
'from' => 1,
'to' => 3
),
array(
'type' => 'blocks',
'cost' => NULL,
'modifier' => NULL,
'base_cost' => 0.02914814815,
'base_modifier] => 'times',
'from] => 4,
'to] => 4
)
);
update_post_meta( $product_id, '_wc_booking_pricing', $data );
Then, when you retreive the data using get_post_meta, it will return it as an array, exactly as you saved it.
The string that you are trying to pass through update_post_meta() is serialized data and needs first to be unserialized.
Two possible cases:
1) If you have serialize that data, just don't do it and pass the data array directly in update_post_meta().
Note that a "passed array will be serialized into a string" when using update_post_meta(), so in your case to avoid a kind of double serialization that will make your data unreadable.
2) If you have not serialize that data yourself, you might need to unserialize this data first. For that you can use the WordPress function maybe_unserialize() before using update_post_meta(), this way:
// Unserializing the data
$data = maybe_unserialize( 'a:28:{i:0;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:5:"0.027";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"1";s:2:"to";s:1:"3";}i:1;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.02914814815";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"4";s:2:"to";s:1:"4";}i:2;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:12:"0.0312962963";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"5";s:2:"to";s:1:"5";}i:3;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.03344444444";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"6";s:2:"to";s:1:"6";}i:4;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.03559259259";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"7";s:2:"to";s:1:"7";}i:5;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.03774074074";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"8";s:2:"to";s:1:"8";}i:6;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.03988888889";s:13:"base_modifier";s:5:"times";s:4:"from";s:1:"9";s:2:"to";s:1:"9";}i:7;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.04203703704";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"10";s:2:"to";s:2:"10";}i:8;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.04418518519";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"11";s:2:"to";s:2:"11";}i:9;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.04633333333";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"12";s:2:"to";s:2:"12";}i:10;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.04848148148";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"13";s:2:"to";s:2:"13";}i:11;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05062962963";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"14";s:2:"to";s:2:"14";}i:12;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05277777778";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"15";s:2:"to";s:2:"15";}i:13;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05492592593";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"16";s:2:"to";s:2:"16";}i:14;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05707407407";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"17";s:2:"to";s:2:"17";}i:15;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.05922222222";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"18";s:2:"to";s:2:"18";}i:16;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06137037037";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"19";s:2:"to";s:2:"19";}i:17;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06351851852";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"20";s:2:"to";s:2:"20";}i:18;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06566666667";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"21";s:2:"to";s:2:"21";}i:19;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06781481481";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"22";s:2:"to";s:2:"22";}i:20;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.06996296296";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"23";s:2:"to";s:2:"23";}i:21;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.07211111111";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"24";s:2:"to";s:2:"24";}i:22;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.07425925926";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"25";s:2:"to";s:2:"25";}i:23;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.07640740741";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"26";s:2:"to";s:2:"26";}i:24;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.07855555556";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"27";s:2:"to";s:2:"27";}i:25;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:12:"0.0807037037";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"28";s:2:"to";s:2:"28";}i:26;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:13:"0.08285185185";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"29";s:2:"to";s:2:"29";}i:27;a:7:{s:4:"type";s:6:"blocks";s:4:"cost";s:0:"";s:8:"modifier";s:0:"";s:9:"base_cost";s:5:"0.085";s:13:"base_modifier";s:5:"times";s:4:"from";s:2:"30";s:2:"to";s:2:"30";}}' );
// Saving the data
update_post_meta( $product_id, '_wc_booking_pricing', $data );
Now your data is correctly saved in database and can be read and/or processed.
I am using codeigniter to built a type of online shop.
I want to create a process order function in order to verify the details of the order the clients puts in.
I am stuck though because on the last page i have the data to submit and when i click i go to main/process_order where i insert the data in the table and then use curl to comunicate with another server.
My question is: when i hit submit and then stop on the process_order page if i reload it 1000 times, the table will be filled with the same 1000 lines, so this can be a security issue. Also if i make a function to add the data to db and then redirect to process_order it will be another issue because i still need my data that was posted.
What's the best way to solve this. I hope i made it as clear as i can.
Code:
$data=array(
'userid' => $userid,
'email' => $email_data,
'phone' => $this->input->post('phone'),
'discount' => $this->input->post('discount'),
'price' => $this->input->post('price'),
'final_price' => $this->input->post('final_price'),
'client_data' => $this->input->post('client_info'),
'client_ip' => $this->input->post('client_ip'),
'time' => $date
);
$this->db->insert('orders_temp', $data);
Maybe something like this can help. Do a check on the userid and the time before you insert the data. You would need make sure to do a search on database fields that are unique (unless the time field is the full time)
$data=array(
'userid' => $userid,
'email' => $email_data,
'phone' => $this->input->post('phone'),
'discount' => $this->input->post('discount'),
'price' => $this->input->post('price'),
'final_price' => $this->input->post('final_price'),
'client_data' => $this->input->post('client_info'),
'client_ip' => $this->input->post('client_ip'),
'time' => $date
);
if (<!--CONDITIONAL STATEMENT-->)
{
$this->db->insert('orders_temp', $data);
}
So I have this array:
$data = array(
'item_1' => $this->input->post('item_1'),
'item_2' => $this->input->post('item_2'),
'item_3' => $this->input->post('item_3')
);
$this->session->set_userdata( 'items', $data );
And I want to add a new item to that array, so the updated array of userdata will be like this:
$data = array(
'item_1' => $this->input->post('item_1'),
'item_2' => $this->input->post('item_2'),
'item_3' => $this->input->post('item_3'),
'item_4' => $this->input->post('item_4')
);
$this->session->set_userdata( 'items', $data );
How to do that, Thank you
Follow these steps to add data to the current session:
$data = $this->session->userdata('items');
$data['item_4'] = $this->input->post('item_4');
$this->session->set_userdata('items', $data);
Here, we first take out the current session items in an array, add another item to the array, you can do a array_push() but I prefer the above. Now, set the session back with the updated data.
Got this from ellislab:
Adding Custom Session Data
A useful aspect of the session array is that you can add your own data to it and it will be stored in the user's cookie. Why would you want to do this? Here's one example:
Let's say a particular user logs into your site. Once authenticated, you could add their username and email address to the session cookie, making that data globally available to you without having to run a database query when you need it.
To add your data to the session array involves passing an array containing your new data to this function:
$this->session->set_userdata($array);
Where $array is an associative array containing your new data. Here's an example:
$newdata = array(
'username' => 'johndoe',
'email' => 'johndoe#some-site.com',
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
If you want to add userdata one value at a time, set_userdata() also supports this syntax.
$this->session->set_userdata('some_name', 'some_value');
So in essence you should be able to create the item you want to add and then add it to the session by using $this->session->set_userdata("item_4", "item_4"); or $this->session->set_userdata($newdata) if you want to add many items .