Unknown error!

Discussion in 'PHP' started by le007, Apr 29, 2009.

  1. #1
    Any ideas what is going on here? I've tried various things - designing a simple switch statement but it ain't 100% yet.

    Notice: Undefined index: locate in C:\wamp\www\pro\index.php on line 3

    the above comes up for my switch file?
    This is at the top of index.php
    <?php
    include("functions.php");
    $locate=$_REQUEST['locate'];
    ?>
    Code (markup):

    This is where the switch is meant to take place (and it does work) but the error appears when I load the page initally?
    <div id="middle">
    <?php
    if (empty($locate)) { $locate='pageone';
    } changelocation($locate);
    ?>
    </div>
    Code (markup):
    Functions.php:
    <?php
    
    /****************************************************
    functions.php:
    This file contains main stock of functions.
    ****************************************************/
    
    /******************************************************\
     * Function Name : changelocation($locate)
     *
    * this function is for including the middle part of the page, which is the main
    * part of the site really, this function was originally in index.php, now
    * index.php just calls the function
    *
     \******************************************************/
    
    function changelocation($locate)
    {
       
        switch ($locate)
        {
    
    // load an error page if $locate is set incorrectly by the user or the site (debugging)
        default: include ("404.php");
            break;
    
    
    // these are all the legit classes, you can add as many as you want
    
        case 'pageone': include ('pageone.php');
            break;
        case 'pagetwo': include ('pagetwo.php');
            break;
        }
    
    }
    
    
    
    
    
    ?>
    Code (markup):

     
    le007, Apr 29, 2009 IP
  2. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hey,

    Undefined index means you are using/referencing an array entry that hasn't been set of doesn't exist.

    You should check to see if the array entry exists before trying to use it.

    Try this,

    
    <?php
    include("functions.php");
    
    if(isset($_REQUEST['locate'])) {
        $locate=$_REQUEST['locate'];
    } else {
        $locate = 'pageone';
    }
    ?>
    
    PHP:
    Regards,

    Steve
     
    Steve136, Apr 29, 2009 IP
  3. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #3
    Hi Steve, 50% there - thanks for the reply!
    I now get (where the pageone.php should be)

    I don't have a 404.php yet (because pageone.php does exist) I just need it for when the site gets bigger.
    Any ideas please?
     
    le007, Apr 29, 2009 IP
  4. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi,

    That just means the file doesn't exist, it's trying to include 404.php - Just make a blank PHP file for now if you wish to add to it later.

    404.php
    
    <?php
          echo "You have encountered a 404 Error. Please go back and try again";
    ?>
    
    PHP:
    That should do the trick until you've sorted it out properly ;)

    Regards,

    Steve
     
    Steve136, Apr 29, 2009 IP
  5. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #5
    Topman! Thanks for the help - it seems to all be working now EXCEPT, its showing showing the 404 error page now!?
     
    le007, Apr 29, 2009 IP
  6. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Can't see any reason why it should be, however it is late here :)

    Try doing this:

    Shouldn't need testing for empty, as that's what we do with the isset statement.
    
    <div id="middle">
    <?php
    changelocation($locate);
    ?>
    </div>
    
    PHP:
    Next for the switch, it's only including the 404.php if it detects that $locate is blank so there is some simple methods to check what's wrong.

    First, how are you setting locate? - I see you're using $_REQUEST so I assume it's index.php?locate=pageone which should work, $_GET should also work instead of $_REQUEST if you are typing it into the browser.

    If that doesn't work, try echo'ing the page to the screen in various places so you can see where it's not working. An example:

    
    <div id="middle">
    <?php
    echo $locate . " is the current page.";
    changelocation($locate);
    ?>
    </div>
    
    PHP:
    It should show what the $_REQUEST['locate'] is set to.
     
    Steve136, Apr 29, 2009 IP
  7. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #7
    Hi Steve,
    yeah its showing "pageone.php" is the current page <--- which is correct BUT the undefined error is still at the top... I just don't get it :(
     
    le007, Apr 29, 2009 IP
  8. bluebenz

    bluebenz Well-Known Member

    Messages:
    876
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    138
    #8
    As Steve said,
    Undefined index means you are using/referencing an array entry that hasn't been set of doesn't exist.

    So now the problem is in pageone.php.
    check your pageone.php, if you don't change :

    $locate=$_REQUEST['locate'];

    or other variable similar with this (use the variable without $_REQUEST)
     
    bluebenz, Apr 29, 2009 IP
  9. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #9
    I found out through google - I had to include this at the top:
    error_reporting (E_ALL ^ E_NOTICE);

    Thanks for your help fellas
     
    le007, Apr 30, 2009 IP
  10. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Just a warning, changing the way error reporting works doesn't mean that the notices will go away - Just hidden.

    Regards,

    Steve
     
    Steve136, Apr 30, 2009 IP
  11. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #11
    Yeah you're right but I'm happy enough. Thanks.
    Just looked at your website, nice work - I hope you're enjoying the snooker :D
     
    le007, Apr 30, 2009 IP