...

View Full Version : Debug help much appreciated - Notice: Undefined index: page



SeattleMicah
01-09-2012, 07:20 AM
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


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

Spookster
01-09-2012, 07:43 AM
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.



if (isset($_GET['page'])) {

// your code here

}

SeattleMicah
01-09-2012, 07:56 AM
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?

Fou-Lu
01-09-2012, 05:36 PM
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:


$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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum