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. Link to comment https://forums.phpfreaks.com/topic/81837-solved-problem-using-_getpage/ 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'; Link to comment https://forums.phpfreaks.com/topic/81837-solved-problem-using-_getpage/#findComment-415765 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! Link to comment https://forums.phpfreaks.com/topic/81837-solved-problem-using-_getpage/#findComment-415784 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'; } Link to comment https://forums.phpfreaks.com/topic/81837-solved-problem-using-_getpage/#findComment-415794 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? Link to comment https://forums.phpfreaks.com/topic/81837-solved-problem-using-_getpage/#findComment-415814 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. Link to comment https://forums.phpfreaks.com/topic/81837-solved-problem-using-_getpage/#findComment-415850 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: } } ?> Link to comment https://forums.phpfreaks.com/topic/81837-solved-problem-using-_getpage/#findComment-415874 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";} Link to comment https://forums.phpfreaks.com/topic/81837-solved-problem-using-_getpage/#findComment-415876 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.