Jump to content

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/14338-includes/
Share on other sites

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]
Link to comment
https://forums.phpfreaks.com/topic/14338-includes/#findComment-56499
Share on other sites

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]
Link to comment
https://forums.phpfreaks.com/topic/14338-includes/#findComment-56502
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/14338-includes/#findComment-56510
Share on other sites

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
Link to comment
https://forums.phpfreaks.com/topic/14338-includes/#findComment-56511
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.