Welcome Guest, Not a member yet? Register   Sign In
Session resetting itself each time i add an item to a cart via ajax droppables
#1

[eluser]haraldo[/eluser]
I'm new to CodeIgniter, so i may be missing stuff here.

The autoloader loads the session library.

I'm using scriptaculous to drop an item into a div.

Code:
Droppables.add('basket',{
    accept: 'thumbnails'
          , hoverclass:'basket-active'
          , onDrop:function(element) {
                         new Ajax.Updater( 'savedthumbs'
                                       , '/index.php/welcome/addToBasket/'+document.getElementById(thumb_id).getAttribute('encode64')
                                       , { onLoading:function( request ) {
                                                         Element.show( 'indicator' )
                                                     }
                                           , onComplete:function(request) {
                                                            Element.hide( 'indicator' )
                                                        }
                                           , evalScripts:true
                                           , asynchronous:true
                                           }
                                       )
           }
    }
)

When the item is dropped it runs addToBasket in the controller to add the item to a session. The following function is in my controller.

Code:
public function addToBasket( $sThumbDetails ) {

    $aThumbDetails = unserialize( base64_decode( $sThumbDetails ) );
        
    if( !empty( $aThumbDetails ) ) {
            
        $sEncodedURL = base64_encode( $aThumbDetails['DISPLAYURL'] );

        $newdata = array( 'basket'=> array( $sEncodedURL => $aThumbDetails ) );
        $this->session->set_userdata($newdata);

        /*if( !$_SESSION['visitor']->exist( $sEncodedURL ) ) {
            $_SESSION['visitor']->set( $sEncodedURL, $aThumbDetails );
        }*/
    }
        
    //$aBasket = $_SESSION['visitor']->getBasket();
    $aBasket = $this->session->userdata('basket');
    print_r($aBasket);
}

Example of the print_r follows:

Code:
Array ( [d3d3LmtpZHNwbGFuZXQub3JnL2ZhY3RzaGVldHMvbW91bnRhaW5fZ29yaWxsYS5odG1s] => Array ( [TITLE] => Mountain Gorilla -- Kids' Planet -- Defenders of Wildlife [DISPLAYURL] => www.kidsplanet.org/factsheets/mountain_gorilla.html [IMAGELOCATION_SMALL] => http://snoogle.local/system/cache/images/www.kidsplanet.orgfactsheetsmountain_gorilla.html_thumb.jpg ) )

Now if you look at the example output. If this was the first item dropped in that would look alright, but each time another item is dropped in it just replaces the session 'basket' with the new item. Repeating what you see above just with different data. The session never remembers the previous items dropped in. Only the current item. Even when i don't use the session library and use the standard session implementation its always resetting itself each time. Somewhere i read you can run chosen code before all the controllers run. I'm thinking of setting up the session like that. I'm losing my marbles over this, its not like i've never used sessions before!

Any ideas?

p.s this is a work in progress - its not meant to be perfect!
#2

[eluser]haraldo[/eluser]
OK,

For those who might be interested.

I added a pre_system hook that initialises a class where i do the following:

Code:
require_once 'system/application/libraries/Visitor.php';

class Session_initialise {

    function __construct() {
        session_start();
    }
    
    public function initiateVisitor() {
        if ( !isset( $_SESSION['visitor'] ) ) {
            $_SESSION['visitor'] = new Visitor();
        }
    }
}

This now allows the normal session implementation (the code in my first post that is commented out) to work.

For those of you who are seasoned codeignitors please let me know if i'm being a mong, and especially if there is a better way to do this.

Chears




Theme © iAndrew 2016 - Forum software by © MyBB