Jump to content

storing last 6 ID's viewed as SESSIONS


johnrb87

Recommended Posts

Hello everyone

 

I wonder if you could help me.

 

I want to create a function called

 

<?php
function mythings($what)

 

Which would allow me to pass an ID number to, such as

 

<?php
mythings(5419);

 

What I want to do with that function is store the ID numbers of the past 6 items I have viewed, but I want to store each item as a SESSION once it has been passed.

 

So essentially I would end up with the following 6 SESSIONS

 

<?php
$_SESSION['item1'];
$_SESSION['item2'];
$_SESSION['item3'];
$_SESSION['item4'];
$_SESSION['item5'];
$_SESSION['item6'];

 

So the "item1" SESSION would be the latest item ID passed through the function

 

But then if I then passed a new ID through the function, that new ID passed would then become

 

$_SESSION['item1'];

 

and the current SESSION

 

$_SESSION['item1'];

 

would become

 

$_SESSION['item2'];

 

and

 

$_SESSION['item2'];

 

would become

 

$_SESSION['item3'];

 

and so on.

 

This means I can do a list such as

 

<?php
print "Item 1".$_SESSION['item1']."<br>";
print "Item 2".$_SESSION['item2']."<br>";
print "Item 3".$_SESSION['item3']."<br>";
print "Item 4".$_SESSION['item4']."<br>";
print "Item 5".$_SESSION['item5']."<br>";
print "Item 6".$_SESSION['item6']."<br>";
?>

 

Does that make much sense? Can anyone help.

 

Thanks very much all

 

John

Link to comment
Share on other sites

It would be much easier to use an array and do it like this:

 

function mythings($id) {
if(!isset($_SESSION['items']) {
	$_SESSION['items'] = array($id);
} else {
	array_unshift($_SESSION['items'], $id);
	if(sizeof($_SESSION['items'] > 6)) {
		$_SESSION['items'] = array_slice($_SESSION['items'], 0, 6);
	}
}
}

 

And then to print it out:

 

foreach($_SESSION['items'] as $key => $item) {
echo "Item " . ($key + 1) . ": " . $item . "<br>";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.