Hooch Posted January 31, 2010 Share Posted January 31, 2010 Hey all. I have googled with no luck. I'm curious on how to show the last 5 products viewed. It needs to be dynamic enough that if only 1 product wasviewed, then it shows only 1. Then from there keep rolling the last five. (or less) Each item will have it's own unique id saved in the DB already. I get so far then hit a wall. Thank you for your time. Link to comment Share on other sites More sharing options...
Ingolme Posted January 31, 2010 Share Posted January 31, 2010 I don't know how your system is structured, but I'd store a comma-separated list of the product IDs that the person viewed. Then I would use explode() to separate the list into pieces and loop through them. Link to comment Share on other sites More sharing options...
Hooch Posted January 31, 2010 Author Share Posted January 31, 2010 Store in the database?I was hoping to skip the database and go sessions only. Link to comment Share on other sites More sharing options...
Ingolme Posted January 31, 2010 Share Posted January 31, 2010 You can store the string in a session too. It doesn't matter where it's stored. $_SESSION['viewed_products'] = "6,8,14,9,10"; Link to comment Share on other sites More sharing options...
Hooch Posted January 31, 2010 Author Share Posted January 31, 2010 I must be missing the simple solution, but how do I enter those id's in that single session?I have only entered 1 value to a session. Never multiple. Link to comment Share on other sites More sharing options...
jeffman Posted January 31, 2010 Share Posted January 31, 2010 A $_SESSION array can hold an unlimited number of elements. Is that the question? Link to comment Share on other sites More sharing options...
Hooch Posted January 31, 2010 Author Share Posted January 31, 2010 Sure...this would work I guess. Then I just draw the last 5 off and echo the results. Seems simple enough. Time to google session arraysThanks guys Link to comment Share on other sites More sharing options...
Hooch Posted February 23, 2010 Author Share Posted February 23, 2010 I finally had some time to research this.I'm having a hard time adding values to the array.I have 3 pages set up with very similar code.The variable "$thisPage" changes for each page. (1,2,3) <?PHP session_start(); //Last 5 properties viewed $thisPage = 1; if(!isset($_SESSION[$session_name])) { $session_name = session_id(); $_SESSION[$session_name] = array(); } array_push($_SESSION[$session_name],$thisPage); echo '1: ' . $_SESSION[$session_name][0]; echo '<br>'; echo '2: ' . $_SESSION[$session_name][1]; echo '<br>'; echo '3: ' . $_SESSION[$session_name][2]; echo '<br>'; echo '4: ' . $_SESSION[$session_name][3]; echo '<br>'; echo '5: ' . $_SESSION[$session_name][4]; ?> If I go to page 1, it will print out...1: 12: 3:4:5: If I go to page 2, it will print out...1: 22: 3:4:5: and if I go to page 3...1: 32: 3:4:5: Any help would be great thank you. Link to comment Share on other sites More sharing options...
thescientist Posted February 23, 2010 Share Posted February 23, 2010 Maybe something where everytime someone views something, you save it to the session variable for that person, which will continue to grow as they view things within that session. Use a loop to only display the last 5 elements of that variable, (so that you are doing LIFO (last in/first out)). To handle it so that it can display less than 5 if they haven't viewed that many yet, set a conditional (within the loop) that checks each iteration to see if there is data, and if there isn't, display "" or something. Link to comment Share on other sites More sharing options...
Hooch Posted February 23, 2010 Author Share Posted February 23, 2010 Hi scientist.I was going for that solution in the code you see above.A new session is created when a visitor clicks on one of the pages.Then it is suppose to add everytime a page is viewed.But it's not adding on to the array. Link to comment Share on other sites More sharing options...
justsomeguy Posted February 23, 2010 Share Posted February 23, 2010 What do you get if you use print_r on $_SESSION? Link to comment Share on other sites More sharing options...
Hooch Posted February 24, 2010 Author Share Posted February 24, 2010 1: 12:3:4:5:print_r:Array ( [0] => 1 ) Link to comment Share on other sites More sharing options...
justsomeguy Posted February 24, 2010 Share Posted February 24, 2010 Look at the order you have these lines in:if(!isset($_SESSION[$session_name])){ $session_name = session_id(); Link to comment Share on other sites More sharing options...
Hooch Posted February 24, 2010 Author Share Posted February 24, 2010 I see no problem.If the session is not set yet, we need to set it.Then once it's set, we do not need to set it again. :)I just tried echoing the following echo $_SESSION[$session_name] . '<br />'; echo $session_name . '<br />'; The actual session echoed nothing. Link to comment Share on other sites More sharing options...
Hooch Posted February 24, 2010 Author Share Posted February 24, 2010 I just had a break through. <?PHP session_start(); //Last 5 properties viewed $thisPage = 1; if(!isset($_SESSION[session_id()])) { //$session_name = session_id(); $_SESSION[session_id()] = array(); } array_push($_SESSION[session_id()],$thisPage); echo '1: ' . $_SESSION[session_id()][0]; echo '<br>'; echo '2: ' . $_SESSION[session_id()][1]; echo '<br>'; echo '3: ' . $_SESSION[session_id()][2]; echo '<br>'; echo '4: ' . $_SESSION[session_id()][3]; echo '<br>'; echo '5: ' . $_SESSION[session_id()][4]; ?> It's adding the values to the array:) Link to comment Share on other sites More sharing options...
ShadowMage Posted February 24, 2010 Share Posted February 24, 2010 You could have also followed jsg's advice... Look at the order you have these lines in:if(!isset($_SESSION[$session_name])){ $session_name = session_id();and moved this line...$session_name = session_id();...outside the if statement, like so:$session_name = session_id();if(!isset($_SESSION[$session_name])){ $_SESSION[$session_name] = array();}That would have saved the trouble of renaming all your variables Link to comment Share on other sites More sharing options...
Hooch Posted February 24, 2010 Author Share Posted February 24, 2010 Thanks jkloth, I wasn't following JSG. I now have a header issue.Here's my code.. <?PHP session_start(); //Last 5 properties viewed $thisPage = 3; $session_name = session_id(); if(!isset($_SESSION[$session_name])) { $_SESSION[$session_name] = array(); } echo '<a href="test1.php">1</a>Â Â Â <a href="test2.php">2</a>Â Â Â <a href="test3.php">3</a>Â Â Â <a href="test4.php">4</a>'; echo '<br />'; array_push($_SESSION[$session_name],$thisPage); echo '1: ' . $_SESSION[$session_name][0]; echo '<br>'; echo '2: ' . $_SESSION[$session_name][1]; echo '<br>'; echo '3: ' . $_SESSION[$session_name][2]; echo '<br>'; echo '4: ' . $_SESSION[$session_name][3]; echo '<br>'; echo '5: ' . $_SESSION[$session_name][4]; echo '<br />print_r:<br />'; print_r ($_SESSION[$session_name]); echo '<br />'; $count = count($_SESSION[$session_name]); echo $count; ?> This produces...Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /test3.php:1) in /home/public_html/test3.php on line 3 :) Link to comment Share on other sites More sharing options...
justsomeguy Posted February 24, 2010 Share Posted February 24, 2010 I didn't really give any advice, just pointed out where the problem was. I wanted you to see that you were trying to use $session_name before you defined it. It looks like you got that worked out. For the sake of efficiency, it will be more efficient if you do define the variable once and use the variable instead of using the session_id function each time. It just takes a little overhead to run the function, so might as well run it once and save the value instead of running it every time you need the value.The session start error means that you have output before you tried to send a header. There's a description about how to read that error message and find the source of the problem in this thread:http://w3schools.invisionzone.com/index.php?showtopic=12509 Link to comment Share on other sites More sharing options...
Hooch Posted February 24, 2010 Author Share Posted February 24, 2010 Here is the total code...what would be considered a header? <?PHP session_start(); $thisPage = 3; $session_name = session_id(); if(!isset($_SESSION[$session_name])) { $_SESSION[$session_name] = array(); } echo '<a href="test1.php">1</a>Â Â Â <a href="test2.php">2</a>Â Â Â <a href="test3.php">3</a>Â Â Â <a href="test4.php">4</a>'; echo '<br />'; array_push($_SESSION[$session_name],$thisPage); $count = count($_SESSION[$session_name]); $last5 = $count - 5; $output = array_slice($_SESSION[$session_name], $last5); for ($i = 4; $i >= 0; $i--) { echo $output[$i]; echo '<br />'; } ?> oh..btw I have a new error popping upWarning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/connie/public_html/test3.php:1) in /home/connie/public_html/test3.php on line 3Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/connie/public_html/test3.php:1) in /home/connie/public_html/test3.php on line 3 Link to comment Share on other sites More sharing options...
justsomeguy Posted February 24, 2010 Share Posted February 24, 2010 Headers include cookies, which you can set with either setcookie or session_start (which sends a session cookie), or you can explicitly send any header using the header function. The error occurs when you try to send a header after you've already sent output, because the first time you send any output PHP will send all of the headers first. So, if you try to send a header after you've already sent output you'll get the warning you're seeing.Check the link to the other thread, it describes the situation and explains how to determine from the error message exactly what the cause is. Your error messages are indicating that output was sent on line 1. That most likely means that you have a space or newline character before your opening PHP tag. Link to comment Share on other sites More sharing options...
Hooch Posted February 24, 2010 Author Share Posted February 24, 2010 Thanks Guy.It was the space before the PHP tag.Ah...W3 comes through again.Does it get any better than this? Link to comment Share on other sites More sharing options...
justsomeguy Posted February 24, 2010 Share Posted February 24, 2010 Does it get any better than this?There's no beer here, so yeah, it does. Link to comment Share on other sites More sharing options...
Hooch Posted February 24, 2010 Author Share Posted February 24, 2010 For anyone following here is my final code that will show the last 5 from the array fromthe most recent down.Next I will need to put a check in to stop duplicate entries.Below would be test1.php <?PHP session_start();$thisPage = 1;//What we want to keep track of$showEntriesNum = 10;//Set to the number of entries to keep track of $session_name = session_id();//Set a unique session name//Only set the session onceif(!isset($_SESSION[$session_name])){ $_SESSION[$session_name] = array();} echo '<a href="test1.php">1</a>Â Â Â <a href="test2.php">2</a>Â Â Â <a href="test3.php">3</a>Â Â Â <a href="test4.php">4</a>';echo '<br />';//Do not echo out duplicatesif( ($thisPage == $_SESSION[$session_name][9]) || ($thisPage == $_SESSION[$session_name][8]) || ($thisPage == $_SESSION[$session_name][7]) || ($thisPage == $_SESSION[$session_name][6]) || ($thisPage == $_SESSION[$session_name][5]) || ($thisPage == $_SESSION[$session_name][4]) || ($thisPage == $_SESSION[$session_name][3]) || ($thisPage == $_SESSION[$session_name][2]) || ($thisPage == $_SESSION[$session_name][1]) || ($thisPage == $_SESSION[$session_name][0]) ){ //Do Nothing} else{ array_push($_SESSION[$session_name],$thisPage);//This will append the new entry if it is new with in the last 10 (or whatever $showEntriesNum is)}//If the array count is less than or equal to $showEntriesNum we need to only show what is in the array.$count = count($_SESSION[$session_name]); if($count <= $showEntriesNum){ for ($i = ($showEntriesNum - 1); $i >= 0; $i--) { if(isset($_SESSION[$session_name][$i])) { echo $_SESSION[$session_name][$i]; echo '<br />'; } } } else//There is alteast 1 more entry to the array then $showEntriesNum{ $showEntries = $count - $showEntriesNum; $output = array_slice($_SESSION[$session_name], $showEntries);//Show everything past the value of $showEntries for ($i = ($showEntriesNum - 1); $i >= 0; $i--) { echo $output[$i]; echo '<br />'; }}?> Link to comment Share on other sites More sharing options...
Hooch Posted February 24, 2010 Author Share Posted February 24, 2010 There's no beer here, so yeah, it does.It's only an arms length away! Link to comment Share on other sites More sharing options...
Hooch Posted February 24, 2010 Author Share Posted February 24, 2010 Hey all...How would I use a "for" loop to shorten this code...plus it could be dynamic too. if( ($thisPage == $_SESSION[$session_name][9]) || ($thisPage == $_SESSION[$session_name][8]) || ($thisPage == $_SESSION[$session_name][7]) || ($thisPage == $_SESSION[$session_name][6]) || ($thisPage == $_SESSION[$session_name][5]) || ($thisPage == $_SESSION[$session_name][4]) || ($thisPage == $_SESSION[$session_name][3]) || ($thisPage == $_SESSION[$session_name][2]) || ($thisPage == $_SESSION[$session_name][1]) || ($thisPage == $_SESSION[$session_name][0]) ) Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.