use $_SESSION[$variable] instead of $_SESSION['whatever'] ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sharonDonnelly
    New Member
    • Mar 2008
    • 2

    use $_SESSION[$variable] instead of $_SESSION['whatever'] ?

    Hi

    Really dumb problem that's got me beat. Can someone help.

    The prolem: I'm trying to count the number of times an item has been clicked. There are many items. I want to create a session variable for each item as its clicked, and increment the value held by the session variable by 1 each time that item is clicked. My plan was to allow an item to be selected on one page - when a user selects an item, a numeric value will be passed to a second page by means of the querystring .... ?item=5.

    Here is the second page where I try to deal with it...

    [PHP]<?php

    session_start() ;

    $item = $_GET['item'];

    if (isset($_SESSIO N[$item]))
    {
    //increment the number to times this item has been selected by 1.
    $_SESSION[$item] = $_SESSION[$item] + 1;
    }
    else
    {
    //This is the first time this item has been selected. Set value to 1.
    $_SESSION[$item] = 1;
    }

    echo "Item ".$item." has been selected ".$_SESSION[$item]." times";
    //e.g Item 5 has been selected 14 times
    ?>[/PHP]


    Can anyone see what I'm doing wrong. The session doesn't set, so item 5 (or any other item) always comes back as being clicked 1 time.

    Thanks
    Shaz


    P.S. I also tried using a session array(?), but no joy there either:

    [PHP]<?php

    session_start() ;

    $_SESSION['test'] = array();
    $item = $_GET['item'];

    if (isset($_SESSIO N['test'][$item]))
    {
    //increment the number to times this page has been selected by 1.
    $_SESSION['test'][$item] = $_SESSION['test'][$item] + 1;
    }
    else
    {
    //This is the first time this page has been selected before. Set value to 1.
    $_SESSION['test'][$item] = 1;
    }

    echo "Item ".$item." has been selected ".$_SESSION['test'][$item]." times";
    //e.g Item 3 has been selected 14 times


    ?>[/PHP]
  • sharonDonnelly
    New Member
    • Mar 2008
    • 2

    #2
    Scrub that. I got it sussed all by myself :)

    I was almost there second time round. I moved the line that sets the session up as an array to its own page - e.g. page 0. Now it all work.

    Thanks!

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      Good you found it yourself. See you next time.

      Ronald

      Comment

      Working...