smonkcaptain Posted April 5, 2011 Share Posted April 5, 2011 Hey all, http://www.adamrowden.co.uk/photo-searchr.asp If you visit this link, you'll see on the left side you have a few photos. If you click a photo from that page and then go back to the link i've provided you'll see that it shows you your 'recently viewed photos'. I'm wanting to do exactly that and need some pointers on how i would!? I'd be very grateful for any help Thanks, Jimmy m Quote Link to comment https://forums.phpfreaks.com/topic/232715-cookies/ Share on other sites More sharing options...
Zurev Posted April 5, 2011 Share Posted April 5, 2011 That site specifically stores a session named AR that stored this data for me after viewing planes 1744, and 1745: rvp4=&rvp3=&rvp2=&rvp1=1745&rvp0=1744 So it simply uses a script that tacks on the ID to the session, and probably limits it I would assume. Quote Link to comment https://forums.phpfreaks.com/topic/232715-cookies/#findComment-1196958 Share on other sites More sharing options...
smonkcaptain Posted April 5, 2011 Author Share Posted April 5, 2011 Thanks for the repsonse! Would you have any idea how you would achieve this then? $id = //id of photo clicked on session_start(); $_SESSION['id'] = $id.. <img src="images/$id.jpg"></img> But that'd only be for one!? Quote Link to comment https://forums.phpfreaks.com/topic/232715-cookies/#findComment-1196960 Share on other sites More sharing options...
Zurev Posted April 5, 2011 Share Posted April 5, 2011 Logically, without me coding the whole thing out. You'd want to check if the session is set, if not you can set it equal to $id& If it is set, you would check how many values are in there, I would explode the array using & as the delimeter, if it were over 5 (or however many max you want), use array pop or something similar to get rid of the last element, or maybe even push if the first element is the latest image they've viewed. If it's set and there's less than your max values, you would just append $id& again. Then on the page you explode again and run through a foreach loop, and echo the img tag with $id.jpg. Quote Link to comment https://forums.phpfreaks.com/topic/232715-cookies/#findComment-1196961 Share on other sites More sharing options...
smonkcaptain Posted April 5, 2011 Author Share Posted April 5, 2011 Logically, without me coding the whole thing out. You'd want to check if the session is set, if not you can set it equal to $id& If it is set, you would check how many values are in there, I would explode the array using & as the delimeter, if it were over 5 (or however many max you want), use array pop or something similar to get rid of the last element, or maybe even push if the first element is the latest image they've viewed. If it's set and there's less than your max values, you would just append $id& again. Then on the page you explode again and run through a foreach loop, and echo the img tag with $id.jpg. Thank's mate. I'm a newbie but will give it a go! First of all, this code i've started to write doesnt even show up in page info/cookies/sessions? Also i get an error 'Parse error: syntax error, unexpected ';' in /home/fhlinux009/a/aviation-photographs.net/user/htdocs/photo.php on line 25' having the & sign after $id. if(!isset($_SESSION['recent'])){ $_SESSION['recent'] = $id&; } Quote Link to comment https://forums.phpfreaks.com/topic/232715-cookies/#findComment-1196964 Share on other sites More sharing options...
smonkcaptain Posted April 5, 2011 Author Share Posted April 5, 2011 Logically, without me coding the whole thing out. You'd want to check if the session is set, if not you can set it equal to $id& If it is set, you would check how many values are in there, I would explode the array using & as the delimeter, if it were over 5 (or however many max you want), use array pop or something similar to get rid of the last element, or maybe even push if the first element is the latest image they've viewed. If it's set and there's less than your max values, you would just append $id& again. Then on the page you explode again and run through a foreach loop, and echo the img tag with $id.jpg. Thank's mate. I'm a newbie but will give it a go! First of all, this code i've started to write doesnt even show up in page info/cookies/sessions? Also i get an error 'Parse error: syntax error, unexpected ';' in /home/fhlinux009/a/aviation-photographs.net/user/htdocs/photo.php on line 25' having the & sign after $id. if(!isset($_SESSION['recent'])){ $_SESSION['recent'] = $id&; } Manage to sort the error... just me being silly. This is what i have so far: <?php if(!isset($_SESSION['recent'])){ $_SESSION['recent'] = $id.'&'; } else if (isset($_SESSION['recent'])){ $session = $_SESSION['recent']; $recent = explode("&", $session); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/232715-cookies/#findComment-1196965 Share on other sites More sharing options...
gizmola Posted April 5, 2011 Share Posted April 5, 2011 Session variables can store arrays. For something like this I would probably use array_unshift() and array_pop() to create a lifo queue. Something like this would work: function addTolastViewed($id) { $maxcount = 10; array_unshift($_SESSION['lastViewed'], $id); if (count($_SESSION['lastViewed']) > $maxcount) array_pop($_SESSION['lastViewed']); } Now whenever someone views a picture, you simply have to call that function and pass it the picture id, and you have a session variable with up to 10 pictures in it. Quote Link to comment https://forums.phpfreaks.com/topic/232715-cookies/#findComment-1196966 Share on other sites More sharing options...
smonkcaptain Posted April 5, 2011 Author Share Posted April 5, 2011 How can I make the session have mutliple values then, because whenever i visit an image it's just overwrites the session with the new $id? Quote Link to comment https://forums.phpfreaks.com/topic/232715-cookies/#findComment-1196968 Share on other sites More sharing options...
gizmola Posted April 5, 2011 Share Posted April 5, 2011 Here's a test program to show you: function addTolastViewed($id) { $maxcount = 10; $r = array(); if (is_array($_SESSION['lastViewed'])) $r = $_SESSION['lastViewed']; array_unshift($r, $id); if (count($r) > $maxcount) array_pop($r); $_SESSION['lastViewed'] = $r; } session_start(); $avalue = rand(1, 1000); addTolastViewed($avalue); echo "This id: $avalue "; echo ''; var_dump($_SESSION['lastViewed']); echo ''; Quote Link to comment https://forums.phpfreaks.com/topic/232715-cookies/#findComment-1196978 Share on other sites More sharing options...
smonkcaptain Posted April 5, 2011 Author Share Posted April 5, 2011 Here's a test program to show you: <?php function addTolastViewed($id) { $maxcount = 10; $r = array(); if (is_array($_SESSION['lastViewed'])) $r = $_SESSION['lastViewed']; array_unshift($r, $id); if (count($r) > $maxcount) array_pop($r); $_SESSION['lastViewed'] = $r; } session_start(); $avalue = rand(1, 1000); addTolastViewed($avalue); echo "This id: $avalue </br>"; echo '<pre>'; var_dump($_SESSION['lastViewed']); echo '</pre>'; That's brill thanks! However, i'm using the function() on one page, and echoing the array on another to dsiplay the recent viewed photos, and getting this error: Fatal error: Call to undefined function addtolastviewed() in /home/fhlinux009/a/aviation-photographs.net/user/htdocs/results.php on line 241 Quote Link to comment https://forums.phpfreaks.com/topic/232715-cookies/#findComment-1196986 Share on other sites More sharing options...
smonkcaptain Posted April 5, 2011 Author Share Posted April 5, 2011 Ok, ignore my previous post, I've sorted it now. Here is the code i have: <?php function addTolastViewed($id) { if(isset($_GET['id'])){ $maxcount = 6; $lastv = array(); if (is_array($_SESSION['lastViewed'])) $lastv = $_SESSION['lastViewed']; array_unshift($lastv, $id); if (count($lastv) > $maxcount) array_pop($lastv); $_SESSION['lastViewed'] = $lastv; } } session_start(); addTolastViewed($id); echo "This id: $id </br>"; echo '<pre>'; var_dump($_SESSION['lastViewed']); echo '</pre>'; ?> How can i now display the images using the variables as the id from the array? Thanks all! Quote Link to comment https://forums.phpfreaks.com/topic/232715-cookies/#findComment-1196989 Share on other sites More sharing options...
gizmola Posted April 5, 2011 Share Posted April 5, 2011 You need the function to be available in the script where you call it. So if you have it in a functions.php file you'd need to include or require it, or just have it defined somewhere in the script. If you copied and pasted my code into your own, your error could reflect the fact that php is case sensitive. addtolastviewed() Would need to be addTolastViewed($id) Notice the "camel" casing I used. Quote Link to comment https://forums.phpfreaks.com/topic/232715-cookies/#findComment-1196991 Share on other sites More sharing options...
gizmola Posted April 5, 2011 Share Posted April 5, 2011 I don't know what these id#'s refer to, but in general , you can foreach through an array() foreach($_SESSION['lastViewed'] as $id) { // use $id to lookup image info and display it? } Quote Link to comment https://forums.phpfreaks.com/topic/232715-cookies/#findComment-1196995 Share on other sites More sharing options...
smonkcaptain Posted April 5, 2011 Author Share Posted April 5, 2011 I don't know what these id#'s refer to, but in general , you can foreach through an array() foreach($_SESSION['lastViewed'] as $id) { // use $id to lookup image info and display it? } Works perfectly, thankyou very much! One last thing, If i click on an image twice, then the image $id get's posted into the session array twice, how can i stop this from happening and as a result stop duplicate copies of 'recently viewed photos'? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/232715-cookies/#findComment-1197000 Share on other sites More sharing options...
gizmola Posted April 5, 2011 Share Posted April 5, 2011 That's a good point. Add some code to the addToLastViewed() function that will strip out prior occurrences of the same id in the queue. This can be easily done using array_diff. function addTolastViewed($id) { $maxcount = 10; $r = array(); if (is_array($_SESSION['lastViewed'])) $r = $_SESSION['lastViewed']; // Check if we've recently viewed this same picture if (count($r) > 0) { $r = array_diff($r, array($id)); } array_unshift($r, $id); if (count($r) > $maxcount) array_pop($r); $_SESSION['lastViewed'] = $r; } Quote Link to comment https://forums.phpfreaks.com/topic/232715-cookies/#findComment-1197044 Share on other sites More sharing options...
smonkcaptain Posted April 5, 2011 Author Share Posted April 5, 2011 That's a good point. Add some code to the addToLastViewed() function that will strip out prior occurrences of the same id in the queue. This can be easily done using array_diff. function addTolastViewed($id) { $maxcount = 10; $r = array(); if (is_array($_SESSION['lastViewed'])) $r = $_SESSION['lastViewed']; // Check if we've recently viewed this same picture if (count($r) > 0) { $r = array_diff($r, array($id)); } array_unshift($r, $id); if (count($r) > $maxcount) array_pop($r); $_SESSION['lastViewed'] = $r; } That's fantastic! Thanks for all your help guys, much appriciated. Quote Link to comment https://forums.phpfreaks.com/topic/232715-cookies/#findComment-1197205 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.