SitePoint Sponsor

User Tag List

Results 1 to 23 of 23
  1. #1
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    520
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    "undefined index" error -- can anyone explain?

    I am running into the following error with a php array:

    Code:
    Notice: Undefined index: /Index/ in /home2/sites/westeros.org/www/html/Citadel/Citadel-Menu-Array.html on line 58
    The array (with some extra stuff trimmed out, but it has all necessary stuff I think) in question looks like this ($MainMenuItem = $WhichMainMenuItem[$CurrentSection]; being line 58):

    Code:
    <?php 
    
    $WhichMainMenuItem = Array ( 
    '/FAQ/' => 'VolumeI',
    '/SSM/' => 'VolumeI',
    '/Concordance/' => 'VolumeII',
    '/Encyclopaedia/' => 'VolumeII',
    '/Heraldry/' => 'VolumeII',
    '/History/' => 'VolumeII',
    '/Characters/' => 'VolumeIII',
    '/Prophecies/' => 'VolumeIII',
    '/Artwork/' => 'VolumeIV',
    '/Books/' => 'VolumeIV',
    '/Images/' => 'VolumeIV'
    ); 
    
    $URL = $_SERVER['PHP_SELF'];
    
    $Parts = explode("/",$URL);
    
    if ($Parts[2] == "index.php")
    {
      $Parts[2] = $Parts[3];
    }
    
    $CurrentSection = "/".$Parts[2]."/"; 
    
    $MainMenuItem = $WhichMainMenuItem[$CurrentSection];
    
    ?>
    I've tried to find some information about "undefined index", and I found one page that said that it meant that 'The index you were trying to get information from in this array on line X does not exist.' However, I am afraid I don't follow what that means. I didn't put together the array myself, I am just trying to troubleshoot it now that I've run into some problems.

  2. #2
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    An index in an array is like a variable name. With a variable name, you'd have like $bla = 'bla'; and with an array you'd have $array['thenameoftheindex'] = 'bla';. Here, "thenameoftheindex" is the index and bla is its value. So "/Index/" just doesn't exist in your $WhichMainMenuItem array. That's all .

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this code:
    PHP Code:
    $URL "/myDir/index/index.php";
    $Parts explode('/'$URL);
    echo 
    "<pre>";
    print_r$Parts);
    echo 
    "</pre>";

    echo 
    $Parts[2]; 

  4. #4
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    520
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Icheb,

    Can I tell the array somehow to just ignore it when it comes across a value that doesn't exist in the array? That's basically supposed to be the 'default' state for the menu that this is used for.

    Oddly enough, btw, when the value is /index.html/ it works, even though that isn't defined in the array.

    7stud,

    I don't think that '$URL = "/myDir/index/index.php";' would work, because that isn't alwaus true.

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

  6. #6
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    520
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How would I use that in this particular situation?

    I am afraid I don't really know PHP, I am just muddling my way to existing code to try to fix a problem that cropped up.

  7. #7
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Something like this (untested):
    PHP Code:
    $menuItems = array(
        
    "FAQ" => "VolumeI",
        
    "SSM" => "VolumeI",
        
    "Concordance" => "VolumeII",
        
    "Encyclopaedia" => "VolumeII",
        
    "Heraldry" => "VolumeII",
        
    "History" => "VolumeII",
        
    "Characters" => "VolumeIII",
        
    "Prophecies" => "VolumeIII",
        
    "Artwork" => "VolumeIV",
        
    "Books" => "VolumeIV",
        
    "Images" => "VolumeIV"
    );

    $url $_SERVER['PHP_SELF'];
    $parts explode("/"$url);
    if (
    array_key_exists($menuItems$parts[2])) {
        
    $menuItem $menuItems[$parts[2]];
    } else {
        
    $menuItem "Default Value Here";


  8. #8
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've tried to find some information about "undefined index", and I found one page that said that it meant that 'The index you were trying to get information from in this array on line X does not exist.' However, I am afraid I don't follow what that means. I didn't put together the array myself, I am just trying to troubleshoot it now that I've run into some problems.
    uhmmm...if you don't understand the very basics of how an array works, how do you expect to troubleshoot code that accesses an array?

    Here is a simple array:
    PHP Code:
    $arr = array(102030); 
    The value 10 is stored at index position 0 in the array, and it can be accessed using the name $arr[0] e.g.
    PHP Code:
    echo $arr[0];  //10 
    You can access the value 30 using $arr[2], e.g.:
    PHP Code:
    echo $arr[2]; //30 
    The index numbers 0, 1, 2 in the array are also known as 'keys'. php automatically assigns those numeric keys to the values 10, 20, 30 when you don't specify the keys when you declare the array, as above.

    Instead of numeric values, you can also use strings for the keys, like this:
    PHP Code:
    $arr = array('first'=>10'second'=>20'third'=>30); 
    Then you would access the values 10, 20, 30 using their string keys:
    PHP Code:
    echo $arr['second']; //20 
    Now, what would happen if you did this:
    PHP Code:
    echo $arr['eight'];  //?? 

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oddly enough, btw, when the value is /index.html/ it works, even though that isn't defined in the array.
    It does? Try this:
    PHP Code:
    $WhichMainMenuItem = Array ( 
    '/FAQ/' => 'VolumeI',
    '/SSM/' => 'VolumeI',
    '/Concordance/' => 'VolumeII',
    '/Encyclopaedia/' => 'VolumeII',
    '/Heraldry/' => 'VolumeII',
    '/History/' => 'VolumeII',
    '/Characters/' => 'VolumeIII',
    '/Prophecies/' => 'VolumeIII',
    '/Artwork/' => 'VolumeIV',
    '/Books/' => 'VolumeIV',
    '/Images/' => 'VolumeIV'
    ); 

    echo 
    $WhichMainMenuItem['/index.html/']; 

  10. #10
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    520
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, I don't think that would work, because sometimes there isn't a parts[3]. I would need it to default to nothing if it doesn't match anything within the array.

    Alternatively, could one construct the array with a default case? Something like this?

    Code:
    $WhichMainMenuItem = Array ( 
    '/FAQ/' => 'VolumeI',
    '/SSM/' => 'VolumeI',
    '/<a catch-all for anything that isn't specified above>/' => '<blank>'
    Another option ... couldn't one kill the script under the following circumstances?

    a) there is no $parts[3]

    b) $parts[2] doesn't exist in the array

    To do some further testing, I added a test page that echoes what happens using this:

    Code:
    $URL = $_SERVER['PHP_SELF'];
    
    $Parts = explode("/",$URL);
    
    echo "<pre>";
    print_r($Parts);
    echo "</pre>";  
    
    if ($Parts[2] == "index.php")
    {
      $Parts[2] = $Parts[3];
    }
    
    $CurrentSection = "/".$Parts[2]."/"; 
    
    echo "Currentsection = $CurrentSection";
    
    $MainMenuItem = $WhichMainMenuItem[$CurrentSection];
    
    $MainMenuItemID = $WhichMainMenuItemID[$CurrentSection];
    
    $SubMenu = $WhichSubMenu[$CurrentSection];
    
    $SubMenuItem = $WhichSubMenuItem[$CurrentSection];
    
    echo "MainMenuItem = $MainMenuItem";
    
    echo "MainMenuItemID = $MainMenuItemID";
    
    echo "SubMenu = $SubMenu";
    
    echo "SubMenuItem = $SubMenuItem";
    You can see the errors it generates here: http://www.westeros.org/Citadel/About/

    As you can see, it gives the undefined index errors.

    What is really weird is that this url doesn't:

    http://westeros.org/Citadel/About/

    I have no idea why the 'www' would make a difference, though.

  11. #11
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    520
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    7stud,

    I understand the basic concept of the array, but I don't really know php, so actually modifying it beyond adding or removing sections starts to get tricky.

    I tested 'echo $WhichMainMenuItem['/index.html/'];', and that comes out blank.

  12. #12
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    See my example. It looks for the page in the array and assigns a default value if nothing is found.

  13. #13
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tested 'echo $WhichMainMenuItem['/index.html/'];', and that comes out blank.
    You should get:
    Notice: Undefined index: /index.html/
    What happens when you try:

    echo $WhichMainMenuItem['/Index/'];

  14. #14
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Error reporting may be set to a level where notices are not displayed.

  15. #15
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Error reporting may be set to a level where notices are not displayed.
    What about the first line of the original post? This whole thread is cuckoo.

  16. #16
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    520
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    7stud,

    You're right, I had a mistake in the first test. That does return the error you mention. I am not sure what's going on, actually. I do know I don't get the errors at http://www.westeros.org/Citadel/, where no parts[3] exists and where parts[2] is index.html.

    I'll see if I can try that modification to the array, though.

  17. #17
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    520
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looking at the example, I am thinking it won't quite work.

    Now, I don't quite know what can and can't be done in php, but this is what I'd like to try to accomplish:

    Code:
    $URL = $_SERVER['PHP_SELF'];
    
    $Parts = explode("/",$URL);
    
    if ($Parts[2] == "index.php") --> Add a check here to make sure that if $Parts[2] is index.php and either of the following is true, $Parts[3] doesn't exist OR $Parts[3] is not within the array, then terminate the script or set $CurrentSection to no value at all. I think that if that's possible, that is better than setting a default value for $CurrentSection.
    {
      $Parts[2] = $Parts[3];
    }
    
    $CurrentSection = "/".$Parts[2]."/";

  18. #18
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    520
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After staring at some examples of php code, I've cobbled together the following:

    Stage 1: If part 2 is NOT index.php or index.html, go ahead and define current section. Not sure how I would get it to not to go onto stage 2 and 3 if stage 1 is as it should be, though.

    if ($Parts[2] != "index.php|index.html")
    {
    $CurrentSection = "/".$Parts[2]."/";
    {

    Stage 2: If stage 1 wasn't true, check to see if part 2 IS index.html. If so, terminate (the default menu should be displayed, so no values need to be set). Again, if this is true, it shouldn't go on to stage 3.

    if ($Parts[2] == "index.html")
    {
    die;
    {

    Stage 3: If part 2 is index.php and part 3 exists and part 3 is within the array, set current section. Otherwise, terminate.

    if ($Parts[2] == "index.php") && ($Parts[3] != "NULL") && (array_key_exists($WhichMainMenuItem,$Parts[3]))
    {
    $CurrentSection = "/".$Parts[3]."/";
    }
    else
    {
    die;
    }

    Currently, this does not seem to work, but is it anywhere in the vicinity of being on the correct track?

  19. #19
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    520
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After struggling with this a little more, I think I've almost got it, using a default value in the array and these conditionals:

    Code:
    $URL = $_SERVER['PHP_SELF'];
    
    $Parts = explode("/",$URL);
    
    if ($Parts[2] != "index.php" && $Parts[2] != "index.html")
    {
    $CurrentSection = $Parts[2];
    }
    
    elseif ($Parts[2] == "index.html")
    {
    $CurrentSection = 'Default';
    }
    
    elseif ($Parts[2] == "index.php" && $Parts[3] != "NULL" && array_key_exists($Parts[3],$WhichMainMenuItem))
    {
    $CurrentSection = $Parts[3];
    }
    
    else
    {
    $CurrentSection = 'Default';
    }
    
    $MainMenuItem = $WhichMainMenuItem[$CurrentSection];
    $MainMenuItemID = $WhichMainMenuItemID[$CurrentSection];
    $SubMenu = $WhichSubMenu[$CurrentSection];
    $SubMenuItem = $WhichSubMenuItem[$CurrentSection];
    The only remaining problem is when there isn't a $Parts[3]. In those cases, I get this error:

    Notice: Undefined offset: 3 in <filename>.

    I imagine the problem lies with this part:

    Code:
    elseif ($Parts[2] == "index.php" && $Parts[3] != "NULL" && array_key_exists($Parts[3],$WhichMainMenuItem))
    {
    $CurrentSection = $Parts[3];
    }
    I am not sure what the problem might be, however. It looks to me as if it should work.

  20. #20
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You can try replacing

    PHP Code:
    $Parts[3] != "NULL" 
    with

    PHP Code:
    isset($Parts[3]) 

  21. #21
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Part of your conditional says this:

    if(......&& $Parts[3] != "NULL" &&.....)

    In that conditional you are telling php: "Please get the value of $Parts[3] and compare it to the string on the right." So, php goes looking for the value of $Parts[3] and when it can't find anything, php issues an error message.

  22. #22
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    520
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh, I see. I thought that you could use $Parts[3] != "NULL" (or something like it, anyway), to check whether $Parts[3] exists or not. Guess I was confused by how the tags within the CMS I use works.

    In any case, it looks like isset($Parts[3]) has done the trick. I'll need to do some more 'live' testing, but that may have solved it.

    Thank you so much, both of you.

  23. #23
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    520
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tested it all over now, and it works just fine. I ended up being able to simplify it a bit further, too, and ended up with this:

    Code:
    if (array_key_exists($Parts[2],$WhichMainMenuItem))
    {
    $CurrentSection = $Parts[2];
    }
    
    elseif ($Parts[2] == "index.php" && isset($Parts[3]) && array_key_exists($Parts[3],$WhichMainMenuItem))
    {
    $CurrentSection = $Parts[3];
    }
    
    else
    {
    $CurrentSection = 'Default';
    }
    I did some more reading up on arrays, though, and saw a suggestion that it was better to use isset() than array_key_exists().

    Would it be possible to simplify it even further, using one and the same isset() to check if $Parts[3] exists and is within the array:

    Code:
    if (isset($WhichMainMenuItem[$Parts[2]]))
    {
    $CurrentSection = $Parts[2];
    }
    
    elseif ($Parts[2] == "index.php" && isset($WhichMainMenuItem[$Parts[3]]))
    {
    $CurrentSection = $Parts[3];
    }
    
    else
    {
    $CurrentSection = 'Default';
    }
    I am not sure if that is correct, though. In particular, I am wondering if isset($WhichMainMenuItem[$Parts[2]]) or isset($WhichMainMenuItem['$Parts[2]']) is the correct format?

    Additionally, I am wondering if there is a 'built in' way to specify a default for an array? I am using this

    'Default' => ''

    as the last part of the array now, but perhaps there's a better way?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •