CodeIgniter Forums
Storing items in a session array as the page loads - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Storing items in a session array as the page loads (/thread-21592.html)



Storing items in a session array as the page loads - El Forum - 08-14-2009

[eluser]mattpointblank[/eluser]
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:

Code:
<?php
... "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?

Thanks
Matt


Storing items in a session array as the page loads - El Forum - 08-14-2009

[eluser]verynewtothis[/eluser]
I think logically speaking, something like this should work:
Code:
if ($this->session->userdata('ad_array')):
    $ar = $this->session->userdata('ad_array');
endif;

$this->db->where_not_in($ar);
$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);



Storing items in a session array as the page loads - El Forum - 08-17-2009

[eluser]mattpointblank[/eluser]
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!