1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Snippet Code Help ...

Discussion in 'PHP' started by drgeorgep, Mar 29, 2013.

  1. #1
    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
     
    Solved! View solution.
    drgeorgep, Mar 29, 2013 IP
  2. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #2
    It's because the variable has not been set, just place at the top $snippet = ''; that will stop the error.
     
    HuggyStudios, Mar 29, 2013 IP
  3. drgeorgep

    drgeorgep Active Member

    Messages:
    854
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    58
    #3
    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
     
    drgeorgep, Mar 29, 2013 IP
  4. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #4
    Right at the top of the script, don't place it inside as that may overwrite whatever value is it getting set.
     
    HuggyStudios, Mar 29, 2013 IP
  5. drgeorgep

    drgeorgep Active Member

    Messages:
    854
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    58
    #5
    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
     
    drgeorgep, Mar 29, 2013 IP
  6. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #6
    replace this line:
    
    $snippet = $snippets[$snippetIDX];
    
    PHP:
    to

    
    $snippet = (isset($snippets[$snippetIDX])) ? $snippets[$snippetIDX] : 0;
    
    PHP:
    Should stop the notice error.
     
    HuggyStudios, Mar 29, 2013 IP
  7. drgeorgep

    drgeorgep Active Member

    Messages:
    854
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    58
    #7
    Hi ... replaced code; now returns 0 or any any number I replace the 0 with. Thanks. dgp
     
    drgeorgep, Mar 29, 2013 IP
  8. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #8
    Sorry I need to look at all of the script, can you post the code?
     
    HuggyStudios, Mar 29, 2013 IP
  9. drgeorgep

    drgeorgep Active Member

    Messages:
    854
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    58
    #9
    Hi ... code in original posting of this thread. Thanks. dgp
     
    drgeorgep, Mar 29, 2013 IP
  10. #10
    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.
     
    HuggyStudios, Mar 29, 2013 IP
  11. drgeorgep

    drgeorgep Active Member

    Messages:
    854
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    58
    #11
    Hi ... worked. Thanks so much. You don't know how much I appreciate your skill and your time. dgp
     
    drgeorgep, Mar 29, 2013 IP
  12. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #12

    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.
     
    HuggyStudios, Mar 30, 2013 IP