Home > Programming > PHP Code to Include Content in Layout

PHP Code to Include Content in Layout

When I didn’t know PHP I spent a lot of time trying to find the coveted script that would allow me to include content in a page by including a page so that I could change the layout and it would remain the same for all pages, only the content would change. I of course didn’t want to use an iframe because these scroll and look hideous. The solution I found, but did not understand for several months later. When a bit later I lost this code I realized it was time for me to learn PHP. The code I wished to know was the code that allows you to include pages with content like http://example.com/index.php?page=home.php. The layout would be in the file index.php but the content for the homepage would be at home.php. The code to do this would be:

<?php if($_GET['page'] == null) { include “default.php”; } else { include $_GET['page']; } ?>

The explaination of this is:

$_GET['page']
is the PHP way to get a parameter from a url.  In PHP all the page parameters are stored after the ? in the url.  For example in this page index.php?page=anything.php the only variable is page and in this case page is equal to “anything.php”.  The $_GET['page'] gets the value of page.  If you wish to add more variables to the url you simply add an & in between the variables.  For example “index.php?page=anything.php&loggedin=false” Thus $_GET['loggedin'] would result in false.   Next time you do a go0ogle search check the url and you can see these variables.

include “default.php”
The first include will be shown if the page variable is null.  In this case it will show the default page you assign it.

include $_GET['page']
This will show the page of whatever follows the page variable in the url.

For an example of this visit my homepage http://rkania.com/index.php?view=home.  My site has customized code and mod rewrite so it won’t work exactly the same.

Categories: Programming Tags: ,
  1. December 4th, 2011 at 20:57 | #1

    It?? difficult to find knowledgeable people about this topic, and you seem like you know what you??e discussing! Thanks

  2. December 5th, 2011 at 09:19 | #2

    Nice weblog right here! Additionally any website quite a bit ” up ” very fast! The things web host lots of people are the use of? May well I get any affiliate hyperlink on your host or hostess? Document desire great website full ” up ” like quickly like your site lol.

  3. December 5th, 2011 at 10:16 | #3

    I’ve learn some good stuff here. Definitely worth bookmarking for revisiting. I wonder how a lot attempt you put to create this sort of excellent informative web site.

  4. December 5th, 2011 at 14:54 | #4

    Hello, i think that i saw you visited my weblog so i came to “return the favor”.I’m attempting to find things to improve my site!I suppose its ok to use some of your ideas!!

  5. January 11th, 2012 at 07:41 | #5

    I think this is one of the so much important info for me. And i’m happy studying your article. But should commentary on some general things, The website style is perfect, the articles is truly great : D. Good task, cheers

  6. January 11th, 2012 at 08:52 | #6

    Heya i’m for the first time here. I came across this board and I find It really helpful & it helped me out a lot. I am hoping to provide one thing again and aid others such as you aided me.

Comment pages
1 4 5 6 402
  1. December 1st, 2011 at 10:47 | #1
  2. December 1st, 2011 at 16:13 | #2
  3. December 2nd, 2011 at 06:19 | #3
  4. December 3rd, 2011 at 06:13 | #4
  5. December 3rd, 2011 at 13:03 | #5
  6. December 3rd, 2011 at 16:34 | #6
  7. December 3rd, 2011 at 20:06 | #7
  8. December 4th, 2011 at 15:07 | #8
  9. December 5th, 2011 at 08:01 | #9
  10. December 5th, 2011 at 11:01 | #10
  11. December 5th, 2011 at 11:28 | #11