sorting php sessions

Joined
12 Feb 2006
Posts
17,627
Location
Surrey
i'm trying to have a php script that records the latest 3 "threads" the user views by storing the id and name as session variables and if logged in then also in a database.

is there any way to have php order the session by time created?

atm i have it check if session1 is created then create session2, if 2 created then 3, if 3 then delete 1 and create 4, which is getting very ugly atm and is not the best way to have latest viewed first as the user could review the same as stored in session2 but session 3 will be shown as latest. again to sort this is becoming really ugly

thanks guys
 
Not sure if I'm quite understanding but here goes. Untested but I would do something like this - perhaps you need to start building up some kind of object but I'm thinking just store an array of them in the session.

As I say, untested so it wouldn't suprise me if this didn't work "copy & paste". The idea's here at least.

PHP:
function addThreadView ($threadid, $userid, $username) {
    if(is_array($_SESSION['thread_views'])){
        if(count($_SESSION['thread_views'])>3){ //shift first element if required
            array_shift($_SESSION['thread_views']);
        }
        //add to end of array
        $_SESSION['thread_views'][]=array('userid'=>$userid,'name'=>$username,'thread'=>$threadid);
    }else{ //make the array
        $_SESSION['thread_views']=array(array('userid'=>$userid,'name'=>$username,'thread'=>$threadid));
    }
    //check if logged in and dump into database here?
}

//on thread view
addThreadView(14,9,'furnace');

//show thread views, dump into database, or whatever
if(is_array($_SESSION['thread_views'])){
    foreach($_SESSION['thread_views'] as $viewdata){
        print('User '.$viewdata['userid'].' ('.$viewdata['name'].') viewed thread '.$viewdata['thread']);
    }
}

Presuming you want to show the 'latest threads' viewed by a user though, I would just log into the database the userid (or sessionid along with expiry time if it's a guest) and the threadid / view time, as opposed to storing this stuff in a session. I don't quite see any obvious benefit to having it in a session when you could just throw it straight into the database, but then I'm sure you have your reasons! ;)
 
thanks for that, i actually managed to get it working last night by having a break then it all became really clear. basically what i did was have is say if there is none store as session1, then if only 1 store as session2, if only 2 then store as session3, then else session3 = session2, session2 = session1, session1 = new thread :)

thanks for the help

I don't quite see any obvious benefit to having it in a session when you could just throw it straight into the database, but then I'm sure you have your reasons! ;)

my reason is because the user doesn't have to be logged in, so i store it as a session, but also in a database so that if they are logged in then it's still there when they come back whenever in the future
 
Back
Top Bottom