Jump to content


Photo

Includes


  • Please log in to reply
6 replies to this topic

#1 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 11 July 2006 - 11:18 PM

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

#2 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 11 July 2006 - 11:23 PM

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 (?page=desc).
If so, then put this code before all the if/elses.:
$page = ((isset($_GET['page']))?($_GET['page']):(''));

<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#3 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 11 July 2006 - 11:27 PM

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

#4 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 11 July 2006 - 11:29 PM

No, sorry.

What this does is take the value of page from the browser address and put it into $page, the modified code is:
<?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";}
?>

<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#5 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 11 July 2006 - 11:31 PM

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



#6 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 11 July 2006 - 11:35 PM

Sure, this method is called a ternary, because there are three parts:
The test: (isset($_GET['page']))
True value: ($_GET['page'])
False value: ('')

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.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 July 2006 - 11:35 PM

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:
<?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';
}?>

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users