Welcome Guest, Not a member yet? Register   Sign In
Storing items in a session array as the page loads

Hi everyone.

I have a helper file which loads adverts on my site. It's used in several spots on the page and selects adverts to display from a pool of potential ones. Since the same advert can be booked into multiple positions on the site, I have to prevent the same ad from occupying more than one space on the site at a time.

On my old, pre-CI site, I did this using $_SESSION - every time I selected an ad for display, I added its filename to an array stored in the session. My query to get all the available ads included this code:

... "WHERE AdvertPath NOT IN('".implode("', '", $_SESSION['adsOnPage'])."')";

This meant that each time the query ran, it excluded any ads that had already appeared.

I'm trying to reproduce this in CodeIgniter but having problems with CI's session functions. Basically, when I assign the advert ID to flashdata/userdata, it only writes the first one, then all subsequent ones don't seem to be added - some sort of overwriting issue I guess?

Is there a logical way anyone can see for me to add items to a session array as the page is loaded?


I think logically speaking, something like this should work:
if ($this->session->userdata('ad_array')):
    $ar = $this->session->userdata('ad_array');

$Q1 = $this->db->get('adTable')->result(); // 1 or more result whatever is your logic

//You will have some logic to select ads to display and store them in array
$ar[] = Q1->myAdID;

$this->session->set_userdata('ad_array' , $ar);

Thanks for that, worked fine with a couple of changes. I had to unset the entire 'ad_array' userdata at the end of page execution (I added a function in my footer) otherwise it counts ads loaded across the entire session, not just a single page, meaning after a couple of page impressions, all the ads disappear. Thanks!

Theme © iAndrew 2016 - Forum software by © MyBB