Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/276303-php-snippet-help-for-an-idiot-me/
Share on other sites

There is no viable reason why you would receive different results on different browsers. PHP is server side, and sends the same output to all browsers.  It doesn't even know what browser you have, (unless the browser specifically sends that info).

Lets look at the logic.

 

1. Get seen snippets.

2. Set snippets to an empty array.

3. Get the array keys of the empty snippets array (unSeenSnippets).

4. If there are indexes in the seenSnippets array, then get the difference between the seenSnippets array, and the empty unSeenSnippets array.
5. Now get a random index from the unSeenSnippets array, which will be empty unless you have something in the seenSnippets array.

6. Now you ask for the random index, which may be empty,  to find the snippet.

I'm not sure if the indexes are numerical or associative.  But, here is a stab.

 

 
<?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);
$snippetIDX = (!empty($snippetIDX)) ? $snippetIDX : 0; //set snippet IDX to a default value of 0, if it is empty.

$seenSnippets[] = $snippetIDX;

$lastSeenSnippet = $snippetIDX;
$snippet = $snippets[$snippetIDX];



$this->session->set_userdata('seenSnippets', $seenSnippets);



$this->session->set_userdata('lastSeenSnippet', $lastSeenSnippet);



echo $snippet;



?>

Hi JC bones ... the code you wrote works, but the snippets don't rotate; only the snippet equal to the number, in the string, shows up. If, for example, I change the 0 to 3, snippet three shows up after every refresh. I tried this with several numerical values. I cleared the cache before each page refresh.

 

$snippetIDX = (!empty($snippetIDX)) ? $snippetIDX : 0; //set snippet IDX to a default value of 0, if it is empty.
 

What do you suggest?

 

Thanks.

 

dgp

How is the snippets array set?  Because those symptoms tell us one of three things is happening.

 

1. The seenSnippets array is empty.

2. The unSeenSnippet array is empty.

3. Both.

 

So right under:

 

// Pick a random one that has not been seen

 

Add this:

 

echo '<pre>SeenSnippets: ' . print_r($seenSnippets,1) . "\nUnSeenSnippets: " . print_r($unseenSnippets,1) . '</pre>';

 

copy/paste the results.

Hi JCbones ... when I copy the follow line of code where you indicate,

 

echo '<pre>SeenSnippets: ' . print_r($seenSnippets,1) . "\nUnSeenSnippets: " . print_r($unseenSnippets,1) . '</pre>';
 

I get the long list of numbers, see below. What do you think is up? Thanks. dgp

SeenSnippets: Array(    [0] => 4    [1] => 3    [2] => 12    [3] => 9    [4] => 7    [5] => 13    [6] => 1    [7] => 0    [8] => 5    [9] => 11    [10] => 6    [11] => 8    [12] => 2    [13] =>     [14] =>     [15] => 10    [16] =>     [17] =>     [18] => 14    [19] =>     [20] =>     [21] =>     [22] => 15    [23] =>     [24] => 16    [25] =>     [26] =>     [27] => 17    [28] =>     [29] =>     [30] =>     [31] => 25    [32] => 22    [33] => 24    [34] => 20    [35] => 27    [36] => 29    [37] => 21    [38] => 23    [39] => 19    [40] => 28    [41] => 26    [42] => 18    [43] =>     [44] =>     [45] =>     [46] =>     [47] =>     [48] =>     [49] =>     [50] =>     [51] =>     [52] =>     [53] =>     [54] =>     [55] =>     [56] =>     [57] =>     [58] =>     [59] =>     [60] =>     [61] =>     [62] =>     [63] =>     [64] =>     [65] =>     [66] =>     [67] =>     [68] =>     [69] =>     [70] =>     [71] =>     [72] =>     [73] =>     [74] =>     [75] =>     [76] =>     [77] =>     [78] =>     [79] =>     [80] =>     [81] =>     [82] => 0    [83] => 0    [84] => 0    [85] => 4    [86] => 4    [87] => -2    [88] => 99    [89] => 3    [90] => 3    [91] => 3    [92] => 3    [93] => 0    [94] => 0    [95] => 0    [96] => 0    [97] => 0    [98] => 0    [99] => 0    [100] => 0)UnSeenSnippets: Array()

Hi ... the code, echo '<pre>' . print_r($snippets,1) . '</pre>';, displays all the snippet content, at the same one, one on top of the other. This is most bizarree. Just a reminder, I honestly and sincerely appreciate your help in this matter. Thanks. dgp

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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