Setting cookie in codeigniter with one name and several values - php

I want to set a cookie in Codeigniter whenever the user clicks on "add to my favorites". But I'm confused. Because I have to add several items with one name at the same time. You know this is not possible and the CI overrides the previous values. Look at this:
$this->input->set_cookie(array("name"=>'fav', 'value'=>2500, 'expire'=>100000));
$this->input->set_cookie(array("name"=>'fav', 'value'=>3500, 'expire'=>100000));
$this->input->set_cookie(array("name"=>'fav', 'value'=>4500, 'expire'=>100000));
And when I try to get fav value using this function:
printer($this->input->cookie("fav"));
I get this result:
4500
How should I set a cookie for user when they ad an item to their favorite list so that in the moment of retrieving them I know what to retrieve. I cannot use database because this implementation is for the users who are not registered members.

You can use $this->session->set_userdata for storing values.
$fav = $this->session->userdata("my_favs");// get existing list
$fav[] = $new_fav; // append new items to list
$this->session->set_userdata(array("my_favs"=>$fav)); // update session with existing one.
To print all items
$fav = $this->session->userdata("my_favs")
foreach($fav as $fitems)
echo $fitems."<br/>";

I think you should use print_r() instead of printer().
more than that, you should use:
$this->input->get_cookie("fav");
Have a look here for more information: Cookie helper

Related

Typo3 8.7.x / Typoscript: Get page uid of the TEXT which was found

How can I get the uid of the found TEXT?
test = TEXT
test {
data = levelfield:-1, tx_myext_myfield, slide
required = 1
wrap = |
}
There are additional fields tx_myext_myfield2, tx_myext_myfield3,.. which I want to use, but I don't know how to get them with slide functionality.
To make use of the levelfield feature you have to add these fields to the so called "rootline fields" using
$GLOBALS['TYPO3_CONF_VARS']['FE']['addRootLineFields']
See https://docs.typo3.org/typo3cms/TyposcriptReference/singlehtml/Index.html#levelfield
Since you are getting just the value of a single field here, you can not access the other values though. If you want to access these values, you will need to fetch a whole record instead, which could be done using the cObjects
CONTENT
https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/Content/Index.html
or
RECORDS
https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/Records/Index.html

How to store search conditions with php and Codeigniter?

I have small problem.
I've coded a full website in php using CodeIgniter framework. One of my modules is search module, it contains text input with keyword and three select lists with filtering criterias.
That's ok, when I'm searching something - result's listing pagination is done via URL like that:
mysite.com/$keyword/$criteria1/$criteria2/$criteria3/$offset
works like a charm.
But when I'm entering into one of my images (it's image gallery) I want to have an option to go into NEXT and PREVIOUS image from my search results - the ones which I entered this image from.
I'm solving this case now in this way - I have session table called 'search_conditions' and I'm storing values of keyword and my three criterias there, but that's quite not comfortable, because why if someone opens second window and search something else there?
Then all of his searches in another windows or tabs are getting the same criteria - because with every new search, user overwrite the session value.
My next and previous functions:
public function next($count)
{
$search = $this->session->userdata('search_conditions'); //getting session table and overwriting it
$catid = isset($search['catid'])?$search['catid']:'0';
$brandid = isset($search['brandid'])?$search['brandid']:'0';
$prodid = isset($search['prodid'])?$search['prodid']:'0';
$keyword = isset($search['keyword'])?$search['keyword']:'';
$res = $this->search_model->main_search($keyword, $catid, $brandid, $prodid, $count, 1);
}
public function previous($count)
{
$search = $this->session->userdata('search_conditions');
$catid = isset($search['catid'])?$search['catid']:'0';
$brandid = isset($search['brandid'])?$search['brandid']:'0';
$prodid = isset($search['prodid'])?$search['prodid']:'0';
$keyword = isset($search['keyword'])?$search['keyword']:'';
$res = $this->search_model->main_search($keyword, $catid, $brandid, $prodid, $count-2, 1);
}
Can you recommend me some other, more comfortable solution, because this seems not to be good...
: )
Thank you!
Add an index to the $search_conditions variable:
$search_conditions[1]['catid']
$search_conditions[1]['brandid']
...
then refer to it with a controller's or config variable. This way you can allow one session to store multiple search conditions.
But I would recommend you drop storing the search condition in session. Instead, just pass it with the URI. Session data, in the case you describe, work as an intermediary; you don't need it. Use the Pagination Class and pass the search page number, not the direction (next or previous) to the URI.
Do not worry that the URI may look ugly - it only depends on what user searches for, and it's still friendly to share. Your only concern is if the GET string does not extend the limited length.
Pull the segments from the URI in your next() and previous() functions. Use the codeigniter URL helper. That should allow you to pass the different search criterion as variables to the next page, this would also remove your need to use the session.

Updating multiple page elements without refreshing the page using PHP & jQuery

I have a PHP page that uses jQuery to let a user update a particular item without needing to refresh the page. It is an availability update where they can change their availability for an event to Yes, No, or Maybe. Each time they click on the link the appropriate jQuery function is called to send data to a separate PHP file (update_avail.php) and the appropriate data is returned.
Yes
Then when clicked the params are sent to a PHP file which returns back:
No
Then, if clicked again the PHP will return:
Maybe
It all works fine and I'm loving it.
BUT--
I also have a total count at the bottom of the page that is PHP code to count the total number of users that have selected Yes as their availability by simply using:
<?php count($event1_accepted); ?>
How can I make it so that if a user changes their availability it will also update the count without needing to refresh the page?
My thoughts so far are:
$var = 1;
while ($var > 0) {
count($day1_accepted);
$var = 0;
exit;
}
Then add a line to my 'update_avail.php' (which gets sent data from the jQuery function) to make $var = 1
Any help would be great. I would like to stress that my main strength is PHP, not jQuery, so a PHP solution would be preferred, but if necessary I can tackle some simple jQuery.
Thanks!
In the response from update_avail.php return a JSON object with both your replacement html and your new counter value.
Or to keep it simple, if they click "yes" incriment the counter, if they click No or maybe and their previous action wasn't No or Maybe decrease the counter.
Assuming your users are logged into the system I'd recommend having a status field in the user table, perhaps as an enum with "offline", "available", "busy", "unavailable" or something similar and use the query the number of available users whilst updating the users status.
If you were to do this you'd need to include in extend your methods containing session)start() and session_destroy() to change the availability of the user to available / offline respectively
The best way is the one suggested by Scuzzy with some improvements.
In your php, get the count from the database and return a JSON object like:
{ count: 123, html: 'Yes' }
In your page, in the ajax response you get the values and update the elements:
...
success: function(data) {
$("#linkPlaceholder").html(data.html);
$("#countPlaceholder").html(data.count);
}
...

How can I set a session for multiple links that I want to disable?

piece of file1.php:
Add this to DB
This takes the user to a page where the data gets inserted into the database.
file2.php :
$clicked = $_GET['addid'];
$_SESSION['clicked'] = $clicked;
// data gets inserted
header("Location: file1.php?id=$clicked");
But I have got multiple pages (like: file1.php?id=1 | file1.php?id=2 | file1.php?id=3 etc.).
Can the session variable handle multiple numbers? Is there any way to do this?
Any help appreciated.
(P.S.: Currently I am using the GET method to disable the links, but I think SESSION is more reliable.)
(P.P.S.: I need this for a voting script.)
To hold more data in one session variable, you need to create a multi-dimensional array which will hold multiple against $_SESSION['checked']. You can do this like:
$clicked = (int)$_GET['addid'];
$_SESSION['clicked'][$clicked] = true;
// data gets inserted
header("Location: file1.php?id=$clicked");
(also, you should be sanitizing $_GET['addid'].
Then to check if it is set, you can use array_key_exists:
if(array_key_exists($clicked,$_SESSION['clicked'])){
echo "this button should be disabled!";
}
I am not sure I understood right your question, but if it is how to send an array of data with the same id via an http request you can use this syntax for the url
file.php?arr[]=val1&arr[]=val2&arr[]=val3
from your php code you would access the values as a regular array
Can the session variable handle multiple numbers? Is there any way to do this?
The session variable can store an array
$_SESSION['clicked'] this session variable can store one value at a time.
If you want use 2 dimension array to handle multiple values.
$clicked = $_GET['addid'];
Ex: $_SESSION['clicked'][$clicked];
firstly concatinate all the ids with comma in a string as $var = 1,2,3,4 and then pass it using GET.
Then there on that page you can explode it from comma and can store it in array and then foreach loop for the array will do it for you. Hope it works.

how do i reset the value of a single session array index in codeigniter?

Using a user model which returns an array that looks like this:
$user_data['display_name'] = "John Doe";
$user_data['avatar'] = ./images/user144.jpg";
i create my session using $this->session->set_userdata('user_data',$user_data);
now if on another controller i let the user change his avatar,
how can i replace the session variable associated to that?
like $this->session->set_userdata('user_data["avatar"]',$new_avatar);
just wont work right?
hee thanks for the help...
From looking at an overview of your code, I'm guessing the best way to go about this is to unset the data and reset it.
Use $this->session->unset_userdata('thesessiontounset');
Then set it back up with the new information and old.
The session->set_userdata() function will only let you modify one key at a time. In your case the key refers to an array so what you're trying to do isn't possible in the way you're attempting to do it.
When I'm updating my session I run something like this.
//Create or setup the array of the fields you want to update.
$newFields = array('avatar' = > 'image01.png');
//Check to see if the session is currently populated.
if (!is_array($this->session->userdata('abc'))){
//...and if it's not - set it to a blank array
$this->session->set_userdata('abc',array());
}
//Retrieve the existing session data
$existing_session = $this->session->userdata('abc');
//Merge the existing data with the new data
$combined_data = array_merge($this->session->userdata('abc'), $newFields);
//update the session
$this->session->set_userdata('abc',$combined_data);
More details on array_merge can be found here
First controller
$user_data['display_name'] = "John Doe";
$user_data['avatar'] = "./images/user144.jpg";
$this->session->set_userdata('user_data',$user_data);
Second controller
$user_data = $this->session->userdata('user_data');
$user_data['avatar'] = $new_avatar;
$this->session->set_userdata('user_data', $new_avatar);
It is a bit late, but it might be useful to someone else, this seems to work:
$this->session->userdata['user_data']['avatar'] = $new_avatar;
$this->session->userdata['other_data']['other'] = $other;
$this->session->sess_write();
This allows you to edit values in array just like you do with $_SESION['user_data']['avatar'] = $avatar, with 'only' one extra line and only using CI library.
For Unset Session variable
$this->session->unset_userdata('avatar');
For Set Session variable
$this->session->set_userdata('avatar', '/images/user144.jpg"');
Use just like this
$this->session->set_userdata('session_var',"");

Categories