Skip to main content

Do I need to write a custom session class for CodeIgniter?



I cannot seem to get CI's session library to function the way I want it to. Essentially, I am storing 2 different categories of data within the sessions. The data within the 2 categories may contain the same value. Right now my attempt to add a key => value pair to the session is failing, as it is only allowing 1 key => value pair to be associated with the array. It overrides itself each time I do a post.







$arr = array(

'favorite_products' => array(),

'viewed_products' => array()

);





$arr["favorite_products"][] = $fav_id;





$this->session->set_userdata($arr);







This is what the array looks when I print_r it:







Array ( [favorite_products] => Array ( [4f1066c2b7fff] => 1648406 ) [viewed_products] => Array ( ))







Am I doing something wrong, or is this just the way CI's session library works?


Comments

  1. Make sure you are destroying your session between attempts, but this code should work just fine...

    $arr = array(
    'favorite_products' => array(),
    'viewed_products' => array()
    );


    $arr["favorite_products"][] = $fav_id;
    $arr["favorite_products"][] = 033333; // another id


    $this->session->set_userdata($arr);


    should give you...

    Array (
    [favorite_products] => Array (
    [0] => 1648406,
    [1] => 033333
    ),
    [viewed_products] => Array ()
    )


    If you are trying to do this between requests...

    // if it doesn't already exist in the session, create an empty array.
    if( ! ($favorite_products = $this->session->get_userdata("favorite_products")))
    {
    $favorite_products = array();
    }

    $favorite_products[] = "new id or info";

    $this->session->set_userdata("favorite_products", $favorite_products);

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.