Jump to content

Archived

This topic is now archived and is closed to further replies.

Woodburn2006

Includes

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

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

×

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.