Edward Posted December 15, 2007 Share Posted December 15, 2007 I am using a variable $_GET['page'] to determine what page should be displayed, by the following code: index.php: if ($_GET['page'] == '01_01_welcome') { $page = 'Welcome'; } else if ($_GET['page'] == '01_02_news') { $page = 'News'; } else if ($_GET['page'] == 'downloads') { $page = 'Downloads'; } else { $_GET['page'] = '01_01_welcome'; $page = 'Welcome'; } This way, if a user has not yet chosen a page from the menu, it will default to the Welcome page. This should work fine, but I'm getting an error I've never seen before. Here is the error message I get: Notice: Undefined index: page in e:\domains\i\my-domain.com\user\htdocs\test\index.php on line 18 Please can someone tell me how to get around this? Thank you. Quote Link to comment Share on other sites More sharing options...
corbin Posted December 15, 2007 Share Posted December 15, 2007 That's just PHP saying, "Hey! You're trying to use a non existant variable!" I bet when the url has page in it, that message doesn't show up. An easy way to fix that without having to recode a lot of it would be to add the following at the top: if(!isset($_GET['page'])) $_GET['page'] = '01_01_welcome'; Quote Link to comment Share on other sites More sharing options...
Edward Posted December 15, 2007 Author Share Posted December 15, 2007 That's great! Thank yo very much. What I previusly used at the start was this: if (!$_GET['page']) { $_GET['page'] = '01_01_welcome'; $page = 'Welcome'; } ...which I thought should work the same as your suggestion, but it didn't. Your suggestion solved it. Does this vary between PHP versions, as my alternative code worked fine on my local server and on another domain. Either way, thank you very much! Quote Link to comment Share on other sites More sharing options...
papaface Posted December 15, 2007 Share Posted December 15, 2007 You should be using switch for this anyway: switch ($_GET['page']) { case "01_01_welcome": $page = 'Welcome'; break; case "01_02_news": $page = 'News'; break; default: $page = 'Welcome'; } Quote Link to comment Share on other sites More sharing options...
Edward Posted December 15, 2007 Author Share Posted December 15, 2007 What is the advantage of using switch, other than I can see it's a slightly shorter code? Quote Link to comment Share on other sites More sharing options...
papaface Posted December 15, 2007 Share Posted December 15, 2007 What is the advantage of using switch, other than I can see it's a slightly shorter code? You said it. Its easier to read, and its actually more fit for purpose than using if's. Quote Link to comment Share on other sites More sharing options...
DyslexicDog Posted December 15, 2007 Share Posted December 15, 2007 You would want to check for the existence of $_GET['page'] before you used that switch though <?php if(!isset($_GET['page'])){ switch($_GET['page']){ case ... default: } } ?> Quote Link to comment Share on other sites More sharing options...
PC Nerd Posted December 16, 2007 Share Posted December 16, 2007 also helpful for testing if a variable is set is: if(empty($_GET['page'])) {echo "variable empty";} Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.