Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Dec 2011
    Posts
    201
    Thanks
    12
    Thanked 26 Times in 26 Posts

    Debug help much appreciated - Notice: Undefined index: page

    Hello this is my first website using php so bare with me I am still trying to get use to syntax and basics!

    I have my index.php set up with my various includes (is this a sloppy way of dynamically linking the static assets? please advise).

    PHP Code:
    <?php


    include('includes/header.html');
    include(
    'includes/nav.html');

    if (
    $_GET['page'] == "social")
    {
    include(
    'includes/social.html');
    }
    else if(
    $_GET['page'] == "technological")
    {
    include(
    'includes/technological.html');
    }
    else if(
    $_GET['page'] == "geographic")
    {
    include(
    'includes/geographic.html');
    }
    else if(
    $_GET['page'] == "login")
    {
    include(
    'includes/login.html');
    }
    else if(
    $_GET['page'] == "register")
    {
    include(
    'includes/register.html');
    }
    else if(
    $_GET['page'] == "gotcode")
    {
    include(
    'includes/gotcode.html');
    }
    else
    {
    include(
    'includes/home.html');
    }
    include(
    'includes/footer.html');
    ?>
    I am receiving these errors when viewing index.php, looks like the variable for page? is undefined?

    ( ! ) Notice: Undefined index: page in C:\wamp\www\index.php on line 7
    ( ! ) Notice: Undefined index: page in C:\wamp\www\index.php on line 11
    ( ! ) Notice: Undefined index: page in C:\wamp\www\index.php on line 15
    ( ! ) Notice: Undefined index: page in C:\wamp\www\index.php on line 19
    ( ! ) Notice: Undefined index: page in C:\wamp\www\index.php on line
    ( ! ) Notice: Undefined index: page in C:\wamp\www\index.php on line 27

    Thank you

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    You will want to wrap all of those inside another if condition to check if the page parameter has been set otherwise you will get those errors saying that $_GET[index] is undefined where index in your case is 'page'. $_GET is an array and 'page' is the index of that array you are looking for.

    PHP Code:
    if (isset($_GET['page'])) {

    // your code here


    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • Users who have thanked Spookster for this post:

    SeattleMicah (01-09-2012)

  • #3
    Regular Coder
    Join Date
    Dec 2011
    Posts
    201
    Thanks
    12
    Thanked 26 Times in 26 Posts
    Ah ok makes sense, your the man! thank you...

    The error is gone, but just to be sure I wrapped up before the last else statement, is that correct?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by SeattleMicah View Post
    Ah ok makes sense, your the man! thank you...

    The error is gone, but just to be sure I wrapped up before the last else statement, is that correct?
    No, it needs to be any access. The else should apply to the whole block.
    This said, its easier to use an in_array check. Since the name to page combinations are identical, you can keep it very flat:
    PHP Code:
    $sInclude 'includes/home.html';
    $aValid = array('social''technological''geographic''login''register''gotcode');
    if (isset(
    $_GET['page']) && in_array($_GET['page'], $aValid))
    {
        
    $sInclude sprintf('includes/%s.html'$_GET['page']);
    }
    include_once 
    $sInclude
    If the matches get more complicated, you can use a key => value relationship instead where the value is the path and the key is the provided $page from the client.


  •  

    Posting Permissions

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