Hi ... I have the following code embedded in the main page of my website, <?php $seenSnippets = $this->session->userdata('seenSnippets'); if(!is_array($seenSnippets)) $seenSnippets = array(); $lastSeenSnippet = $this->session->userdata('lastSeenSnippet'); if(!strlen($lastSeenSnippet)) $lastSeenSnippet = -3; $snippets = array(); LIST OF SNIPPETS $unseenSnippets = array_keys($snippets); if(count($seenSnippets)) { if(count($seenSnippets) == count($snippets)) $seenSnippets = array($lastSeenSnippet); $unseenSnippets = array_diff($unseenSnippets,$seenSnippets); } // Pick a random one that has not been seen $snippetIDX = array_rand($unseenSnippets); $seenSnippets[] = $snippetIDX; $lastSeenSnippet = $snippetIDX; $snippet = $snippets[$snippetIDX]; $this->session->set_userdata('seenSnippets', $seenSnippets); $this->session->set_userdata('lastSeenSnippet', $lastSeenSnippet); echo $snippet; ?> Whenever I try to remove a snippet or two from the list, the most recent version of FireFox displays the following error (IE and Chrome work fine), A PHP Error was encountered Severity: Notice Message: Undefined index: Filename: views/vmain.php Line Number: 890 Line 890 contains the following code, $snippet = $snippets[$snippetIDX]; Although the code is embedded and there's no specific database for the snippets, I suspect codeignitor caches the snippets somewhere, as this problem is somewhat intermittent. I can't figure out or imagine where the snippets cache might reside. All browsers are up to date, as of today. My desktop uses Windows 7. Any ideas, advice or help much appreciated. dgp
It's because the variable has not been set, just place at the top $snippet = ''; that will stop the error.
Hi ... thanks for your suggestion. Exactly where, in the code, do I place $snippet = '';? I tried several places, but the error message kept appearing. I cleared the cache before every page refresh. Thanks. dgp
Right at the top of the script, don't place it inside as that may overwrite whatever value is it getting set.
Hi ... I placed the code on the line following <?php, but the error continues. Here's the top of the code, <?php $snippet = ''; $seenSnippets = $this->session->userdata('seenSnippets'); if(!is_array($seenSnippets)) $seenSnippets = array(); $lastSeenSnippet = $this->session->userdata('lastSeenSnippet'); if(!strlen($lastSeenSnippet)) $lastSeenSnippet = -3; $snippets = array(); Thanks. dgp
replace this line: $snippet = $snippets[$snippetIDX]; PHP: to $snippet = (isset($snippets[$snippetIDX])) ? $snippets[$snippetIDX] : 0; PHP: Should stop the notice error.
The error is coming as the $snippetIDX is not getting a value from the array_rand() call. Just set this at the top of the script and remove the changes that I suggested. ini_set('display_errors', 'Off'); error_reporting(0); PHP: This isn't really a good way as we are just hiding the error, but without looking at the whole setup and the session data it's hard work.
Your'e welcome, sorry I couldn't get the actual error removed. I would of needed to apply var_dump and had a look at what the sessions values were.