PHP Include - Including Other Pages and Linking Them

Hi PHP Help!

I’m hoping that after a long time searching I can finally get the answer I’ve been looking for. I’m not very good with code - I used to know it well, but after completely bombing Ruby on Rails, I hit a wall and it’s become difficult to learn/understand.

Anyways. I’ve been trying to build a content-based site now for the last few months. The only functionality I really need is to be able to change a global navigation menu and footer and have it reflect on all pages. After talking with some web people at work, they recommended I use PHP Include. After 2 months of doing Code Academy PHP courses and asking around, I feel like I’m ready to make an attempt at completing this site. However, I completely forgot about something and I’m glad I caught it before writing code up.

This is the general layout I’m trying to make: http://imgur.com/9Y349Kw

The way I currently have it structured is this: The header.php file contains the top black navigation menu, the title area where I’ll throw a graphic, and the green global navigation menu as well as the beginning of the HTML document with the main HTMlL/Body/Head tags. The white area right now is just set to a homepagecontent.php file - this is the part I’ll need to replace on other page. And then the bottom green bar and black footer area is in footer.php. My index.php contains the three files in order.

So I have 2 questions about this approach…1), how on earth do I include other pages? Obviously I can’t do an include for every single file because the homepagecontent.php will always be there unless I manually remove it, but then it affects the homepage. Plus, I haven’t named any future files yet (there could be several hundred pages on this site), so I don’t know how to set it up where it will accept future file names without having to constantly update the index.php page.

My second question is about linking them. I’m pretty sure I link them using this: index.php?p= - how does this work in this relationship with other pages? It kind of piles on from the first question.

Any help is appreciated. I would really appreciate any explanation if anyone posts code so I can really understand what’s happening rather than just copy+pasting since learning PHP will help me with my actual irl job (I do web and mobile usability :D). That’s one of the main reasons I want to make this site because learning some new code and refreshing myself on basic HTML/CSS will help.

Thanks so much everyone, can’t express how much I appreciate everything.

This is what I basically do, but first here’s a little directory structure that I use.

lib/includes/ --> This is where I put my header & footer HTML files in.

Here’s a basic structure of what a my pages will look like:

[php]<?php
/*

  • Create a configuratin file called config.php or utilities.inc.php that will do all the “household” chores. In this case it’s
  • called utilities.inc.php.
    */
    require_once ‘lib/includes/utilities.inc.php’;
    include_once ‘lib/includes/header.inc.php’;
    ?>
<?php include_once 'lib/includes/footer.inc.php'; [/php] My header file (header.inc.php): [php] Login || Register

Pepster's Registration and Login Tutorial

  • home
  • register
  • dashboard
  • [/php] and my footer file (footer.inc.php) [php]

    ©<?php echo $todays_date->format("Y"); ?> Pepster's Place

    [/php] This prevents me from having to retype it over and over again the same things. Hopefully this is what youwere talking about. John

Hi Strider,

This is pretty much what I have in my header and footer files, but I’m confused regarding how this works. I see you wrote “Main content goes here” and that would work for the homepage, but what about with other pages? How do I replace the centerwell content on other pages while retaining the header and footer?

Ideally, you use templates to keep code out of the html. But, since it is likely you don’t know what I am talking about, you include the page template you want to display. You can use a $_GET parameter and a switch statement to show the page you want.

When googling things before, I’ve seen $_GET before so I’m imaging that is close to the right track. Do you have any examples of this?

Something like this:
[php]
if (isset( $_GET [‘p’] )) {

switch ($_GET ['p']) {
	case 'page' :
		if (is_file( 'template/' . htmlspecialchars( $_GET ['p'] ) . '.html' )) {
			include 'template/' . htmlspecialchars( $_GET ['p'] ) . '.html';
		}
		break;
	default :
		include 'templates/home.html';
}

}[/php]

Wanted to respond to this. I launched my site today and can’t seem to get it working. The site URL is http://smash-ups.com and the index.php is right now at http://smash-ups.com/v1index.php. The main URL is just showing an index.html page.

I have a page at http://smash-ups.com/phptest.php which I want to include between the header and footer. However, if I go to http://smash-ups.com/v1index.php?p=phptest.php, it empties the white area and will not take that information.

This is the code I have in my index.php file to try and execute this inbetween the include header/footer:

<?php
    if(isset($_GET['p'])) {
        $file = 'page' . $_GET['p'] . '.php';
        include('$file');
    } else {
        include('Includes/homepagecontent.php');
    }
?>

The homepagecontent.php is loading as you can see on v1index.php but this code is not allowing me to check other pages. Any solutions on how to fix this?

You should be dropping the .php from the get request. You need the file name, not the file.

Thanks for responding so quickly. Does that mean this?

<?php
    if(isset($_GET['p'])) {
        $file = 'page' . $_GET['p'];
        include('$file');
    } else {
        include('Includes/homepagecontent.php');
    }
?>

I removed the . ‘php’; in it. I tried it (it’s at http://smash-ups.com/v1index5.php so it would be v1index5.php?p=phptest.php) but it did not work.

To be honest, I’m not sure exactly what that code means - it was just what was recommended to me by someone IRL.

I do appreciate all your help. I’m so anxious to figure this out!

What is being passed in the get array and what is the actual file name? You need consistency for this to work. You also need to remove the index.html page. index.php should be where the pages are run through.

Download my PDO Bumpstart Database in my signature. It does exactly what you want correctly.

Sponsor our Newsletter | Privacy Policy | Terms of Service