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
https://forums.phpfreaks.com/topic/211672-storing-last-6-ids-viewed-as-sessions/
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>";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.