Err Posted February 25, 2010 Share Posted February 25, 2010 $len = 9; // whatever numberfor ($i = 0; $i < $len; $i++) { if ($thisPage == $_SESSION[$session_name][$i]) { // do whatever }} Link to comment Share on other sites More sharing options...
Hooch Posted February 25, 2010 Author Share Posted February 25, 2010 Thanks Rah, but on a fresh array with nothing in it, it will add9 of the same entries.if all 9 were already set, it might work. Link to comment Share on other sites More sharing options...
Err Posted February 25, 2010 Share Posted February 25, 2010 How does your code look like now? Link to comment Share on other sites More sharing options...
Hooch Posted February 25, 2010 Author Share Posted February 25, 2010 <?PHPsession_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...
justsomeguy Posted February 25, 2010 Share Posted February 25, 2010 You could do something like this: $found = false;for ($i = 0; $i < count($_SESSION[$session_name]); $i++) { if ($thisPage == $_SESSION[$session_name][$i]) { $found = true; break; }} After that $found will be true if the item was in the array. You could also just do this:$found = in_array($thisPage, $_SESSION[$session_name]); Link to comment Share on other sites More sharing options...
Hooch Posted February 25, 2010 Author Share Posted February 25, 2010 Thanks Guy.I'm trying to follow your ever so kind suggestion above.Say I have 10 spots in the array I am wanting to keep track of (last 10 items viewed).The loop will go through 10 times. From that 10 times, if there is one match I don't want to append anything.So I need to create another array from the 10 $found results.Then check that array for 1 true. If all are false I want to append the info.Am I following correctly? Link to comment Share on other sites More sharing options...
justsomeguy Posted February 25, 2010 Share Posted February 25, 2010 I'm not sure I understand, the code I showed will let you determine if a certain item exists in the array. So there's not going to be more than one match, there's going to be 0 or 1 matches (assuming the array doesn't have duplicate values). The first loop stops after it finds the first match, and the second piece of code just uses the buit-in in_array function to do the same thing. The loop is basically what in_array does internally. Link to comment Share on other sites More sharing options...
Hooch Posted February 25, 2010 Author Share Posted February 25, 2010 Ding goes the bell. $found = false;for ($i = 0; $i < count($_SESSION[$session_name]); $i++) { if ($thisPage == $_SESSION[$session_name][$i]) { $found = true; break; }}if($found == false){ 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)} Here's the final code <?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 duplicates$found = false;$count = count($_SESSION[$session_name]);$start = $count - $showEntriesNum;for ($i = $start; $i <= $count; $i++) { if ($thisPage == $_SESSION[$session_name][$i]) { $found = true; break; }}//If there are no dupliacte entries, add the value to the arrayif($found == false){ 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 />'; }}?> Thanks Guys!!!**Edit I had to edit your code a bit Guy. It was getting the 1st entries from the array. I need to check the last entries.again thank you very much for your time.The code above works flawless now. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.