Woodburn2006 Posted July 11, 2006 Share Posted July 11, 2006 i am trying to use file includes as pages for my website, this is the code that i have used:<?php if ($page == "desc"){include("pages/description.php");} elseif ($page == "groundfloor"){include("pages/floors.php");} elseif ($page == "firstfloor"){include("pages/floors.php");} elseif ($page == "photos"){include("pages/photos.php");} elseif ($page == "vp"){include("pages/pic_display.php");} elseif ($page == "contact"){include("pages/contact.php");} else{echo "arse";}?>when i use it on locally on a testing server, it works fine, but when i use it on an actual server it only ever displays what is in the 'else' command and never what i tell it to display with the $page variable.has anybody got any ideas why this may be happening?thanks Quote Link to comment https://forums.phpfreaks.com/topic/14338-includes/ Share on other sites More sharing options...
ShogunWarrior Posted July 11, 2006 Share Posted July 11, 2006 Heh heh, funny. I too use the first word (usually curse) that comes to my mind for debugging.Are you setting the page variable before this code or is this supposed to get it from the query string ([b]?page=desc[/b]).If so, then put this code before all the if/elses.:[code]$page = ((isset($_GET['page']))?($_GET['page']):(''));[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14338-includes/#findComment-56499 Share on other sites More sharing options...
Woodburn2006 Posted July 11, 2006 Author Share Posted July 11, 2006 so if i was going to call up the description page, would i use this:$page = ((isset($_GET['desc']))?($_GET['desc']):('pages/description.php'));??thanks Quote Link to comment https://forums.phpfreaks.com/topic/14338-includes/#findComment-56500 Share on other sites More sharing options...
ShogunWarrior Posted July 11, 2006 Share Posted July 11, 2006 No, sorry.What this does is take the value of page from the browser address and put it into $page, the modified code is:[code]<?php $page = ((isset($_GET['page']))?($_GET['page']):('')); if ($page == "desc"){include("pages/description.php");} elseif ($page == "groundfloor"){include("pages/floors.php");} elseif ($page == "firstfloor"){include("pages/floors.php");} elseif ($page == "photos"){include("pages/photos.php");} elseif ($page == "vp"){include("pages/pic_display.php");} elseif ($page == "contact"){include("pages/contact.php");} else{echo "arse";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14338-includes/#findComment-56502 Share on other sites More sharing options...
Woodburn2006 Posted July 11, 2006 Author Share Posted July 11, 2006 brilliant thanks, dont mean to be a pain but could you please explain what each part does so that i know for future reference,thanks alot for the help Quote Link to comment https://forums.phpfreaks.com/topic/14338-includes/#findComment-56505 Share on other sites More sharing options...
ShogunWarrior Posted July 11, 2006 Share Posted July 11, 2006 Sure, this method is called a ternary, because there are three parts:The test: [b](isset($_GET['page']))[/b]True value: [b]($_GET['page'])[/b]False value: [b]('')[/b]We wrap this in a parentheses and now we can assign the variable a value based on a test:$page = ((isset($_GET['page']))?($_GET['page']): (''));You can use this for many things, to test if a variable is not null, and then assign it to something etc.. hope it helps. Quote Link to comment https://forums.phpfreaks.com/topic/14338-includes/#findComment-56510 Share on other sites More sharing options...
kenrbnsn Posted July 11, 2006 Share Posted July 11, 2006 This is the classic sign that the setting register_globals is enabled on you local server (not good) and disabled on the live server (good). Please see http://www.php.net/register_globals for more information.Once you have that information digested, I suggest that you change the if statement to a switch statement. It is much clearer and less prone to errors.I will assume that the variable $page is coming from the URL for this example:[code]<?php$inc_file = '';if (isset($_GET['page'])) switch($_GET['page']) { case 'desc': $inc_file = 'description'; break; case 'groundfloor': case 'firstfloor': $inc_file = 'floors'; break; case 'photos': case 'contact': $inc_file = $_GET['page']; break; case 'vp': $inc_file = 'pic_display'; break; } if ($inc_file != '') include('pages/' . $inc_file . '.php'); else echo 'arse';}?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/14338-includes/#findComment-56511 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.