I was planing to save the search inputs as arrays in session.
if ($this->input->post()) {
$recent_search = array("Country" => $this->input->post('country'),
"State" => $this->input->post('state'),
"Building" => $this->input->post('building'),
);
I have saved the array in session as
$this->session->set_userdata('recentSearch',$recent_search);
The thing is after every form submission I'm taking in new array values. I want to store those input arrays into the session without erasing the old one. Is there any method that I could use?
I suggest you use localstorage or cookies rather than sessions.
Example:
$search_cache = [
'Country' => $this->input->post('country'),
'State' => $this->input->post('state'),
'Building' => $this->input->post('building')
];
setcookie( 'recentSearch', $search_cache , time() + 3600 , '/');
var_export($_COOKIE);
Related
//setting session data
$loginData = array(
'name' => 'Rajeev Singh',
'email' => 'rajeev#gmail.com',
'age' => '21',
);
$this->session->set_userdata('loginData',$loginData);
//accessing session data
$name = $this->session->userdata('loginData')['name'];
suppose if my users want to update his/her details then I need to change in session
but I'm unable to update session value
currently what I'm doing is creating a whole new session which changes my session ID
I wanted to update one value of my session without changing session ID
//accessing session ID
$sessionID = $this->session->session_id;
This will update your session variable 'loginData' using Codeigniter without destroying the session.
I'm using $data_update where your custom variable would go.
$this->session->set_userdata('loginData', $data_update);
Codeigniter Sessions page
[1] http://codeigniter.com/user_guide/libraries/sessions.html
Just overwrite the sessions
//initial set
loginData = array(
'name' => 'Rajeev Singh',
'email' => 'rajeev#gmail.com',
'age' => '21',
);
$this->session->set_userdata('loginData', $loginData);
//overwritting
$loginData = array(
'name' => 'new name',
'email' => 'new email',
'age' => 'new age',
);
$this->session->set_userdata('loginData', $loginData);
Take look at here.
print_r($_SESSION["logindata"]);
$_SESSION["logindata"]["name"]="updated name";
print_r($_SESSION["logindata"]);
Im working with an api which stores data into a JSON file. This data is gathered from a form that the users fill in my website. The way its inserted goes as follow:
$pers_payload = array(
'gender' => 'Unknown', //or Male / Female
'first_name' => $_POST['billing_first_name'],
'family_name' => $_POST ['billing_last_name'],
'email' => $_POST['billing_email'],
'linked_as_contact_to_organization' => array(
array(
'organization_id' => $organization_id, // add the person as a contact to the newly created organization
'work_email' => $_POST['billing_email'],
'work_phone' => $_POST['billing_phone']
)
),
'visiting_address' => array(
'country_code' => 'NL'
), // can be extented with other address data
'postal_address' => array(
'country_code' => $_POST['billing_country']
) // can be extented with other address data
);
And then:
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
Now instead of post i want to get the data. I tried getting data like this:
$SimplicateApi->makeApiCall('GET','/crm/organization?q[name]=*my name*');
I dont know if this is the right way, well it didn't work so obviously its not.
Anyways what im trying to achieve is with PHP i want to gather the name value of an existing person. this data is stored in /api/v2/crm/person.json
Api documentation (which i read but didn't understand to well) http://api.simplicate.nl/
It's been a while but i'm trying to answer all my open questions without an answer which i ended up solving on my own.
So for this.
You have to create a variable which makes the get request like this:
$test = $SimplicateApi->makeApiCall('GET','/crm/organization?q[name]=My name');
Now you can for example do a var_dump($test);
And as output you will get all the data inside
/crm/organization?q[name]=My name
I'm working with an API.
With an array I collect data like this:
$org_payload = array(
'name' => $_POST['billing_company'],
'phone' => $_POST['billing_phone'],
'email' => $_POST['billing_email'],
'note' => $_POST['order_comments'],
'relation_type' => array(
'id'=>'relationtype:c1ec3ae77036842d' //provide the relationtypeid, f.e. relationtype:796ce0d318a2f5db515efc18bba82b90
),
'visiting_address' => array(
'country_code' => 'NL',
'line_1' => $_POST['billing_address_1'],
'postal_code' => $_POST['billing_postcode'],
'locality' => $_POST['billing_city'],
'country' => $_POST['billing_country']
), // can be extented with other address data
'postal_address' => array(
'country_code' => 'NL'
) // can be extented with other address data
);
At one point i send this data to the program i'm working with. I achieve this with this code:
$organization = $SimplicateApi->makeApiCall('POST','/crm/organization',json_encode($org_payload));
I gather this data from a form on my website. This data gets posted in the program.
I am trying to achieve that when data gathered from my form matches existing data in the program then don't add it. I would like a hint in the right direction for this, been looking on the internet without any luck.
What I would suggest is to have one extra call to the API.
Like you said in the comments - the company name and the phone number is unique.
If there is some call to get a user by those values and check what you got from the form, would be enough.
If they are unique - send them,
if not - show to the user or whatever you want to do here.
No need to keep one more database on your system as well.
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 .