Including Content in a Master file

First off, I want to apologize for my ignorance. I am extremely new to PHP and so my terminology may be off. I am not exactly sure what I am asking for so I will explain it to the best of my ability.

Currently I have a template I created for my website. I want to be able to create multiple content pages for the site but only have 1 Master page that I have to edit when I add links, change site wide pictures etc etc. I am also using a news posting application (FusionNews) which is a PHP based news script.

What I’m looking to do -

  1. Create Master Page (Index.php) - This is the page that is displayed when visitors access my website
  2. By default I want it to call on my news script and display it in the designated content area.
  3. By manipulating the URL I want to be able to access different content pages and have them display with in the designated content area.
    Below is the code I thought I needed to use (may not be 100% correct) But essentially by putting the code in the designated content area it would allow me to call on the content.html pages I wanted and would display them in the content area of my website by using URL manipulation.

Example: http://site.com/ndex.php?page=content

[php]<?php include (“page.html”) ;>[/php]

Changing content with the appropriate name of the .html file I wanted to display in the content area would display the different pages appropriately. In addition I want the PHP include to call on my news.php automatically when visiting the index.php page so that users are not hit with an error code in the content area upon visiting.

Well, there are many different ways to do this. I would say the simplest is to just replace the name of the included file with a variable and load the variable with whatever you would like to display. In that way, you
can load the variable with data from the previous page using SESSION variables or pass it thru a POSTED
FORM field. Either will work.

So, let’s say you take the line you posted: <?php include (“page.html”) ;>
And change it to a variable format something like: <?php include ($newpage) ;>
What this does for you is allow you to vary the page that is loaded depending on the value in the $newpage!
Like:
$newpage=“index.html”; or $newpage=“somestrangepage.html”; or $newpage=“http://www.nfl.com”;

So, you can load this variable from a form using $_POST[‘somefieldname’], or from an HREF’s argument to
using $_GET[‘somearguementname’], or a session variable like $_SESSION[‘somesessionvariable’]…

If you are loading the new page target from a database, you can just assign the variable from the data you
pull from the table. This could be used in interesting ways. You could load all your pages into an array and
then randomly grab one for display. This would make a nice random selection of news pages or whatever.

Anyway, there you have a start… Ask any further questions if you get stuck… Good luck…

I’m not sure I follow, I have no formal training in PHP so would it possible to dumb this down a little for me?

Yes, I can. First, I need some idea of what you want to display and how you got the pages or page names.

So, if you want all of your web to have the same titles (called headers), same details at the bottom (called footers) and items on both the left and right side of the page (called sidebars) and a different content loaded into the middle section, you would have to start with all of those being created.

So, I will assume you have header, footer, sidebar-left and sidebar-right already created…

It’s just the middle section that we are discussing…

Now, how many of these “content” middle pages do you have? 2, 20, 130?? Some general idea is good.
How are these pages created? By you with an editor? From a database based on some keys?

If you can lay out a general idea of what you want and let us know, we can help you with it.
I was just showing you the way most programmers handle this and thought you already had it started.
You said:

Example: http://site.com/ndex.php?page=content and <?php include ("page.html") ;>
So, I assumed you knew what you wanted... Let me know and we can help...

Okay

First - The Website
http://gagreflexguild.com

This is a website I built for my World of Warcraft guild.

Instead of header.php/footer.php/menu.php I just have the index.php. The Index.php has all of the layout information Header/Footer/Menus/ etc. What I am trying to do is insert a PHP Include in the content area that will allow for me to insert basic .html files into the content portion of the page. Now, my understanding of web design is far out dated as the last time I created a website was roughly 8-10 years ago. At the time I was able to insert a short PHP include that did what I wanted.

The HTML files I am created are very basic unformated documents with just the information. These pages had no other code or information other than the content. In fact these could probably just be uploaded as .txt files and I could change the extension in my include script and it would work fine.

In the content portion of the index.php I was able to insert the following code

[php]<?php include ("$page.html");>[/php]

What this would do is allow me to go to say http://sitename.com/index.php?page=news

This would then load my index.php page and in the content area it would insert that news.html file. If I wanted to go to say http://sitename.com/index.php?page=ponies

It would load the index.php and in the content area it would insert amy ponies.html page.

The point of this is so that I will only have to make changes to the index.php page when I want to make layout changes.

For example: If I just do HTML lets say I have 2 pages - Index.html and Ponies.html and I want to add a new link on the site to a third page - dog.html.

I would have to open each indvidiual .html file and update the layout to reflect that change. With this PHP include I am hoping to only have 1 index.php that
houses all of my layout.

In addition, I want to set up the index.php to automatically pull my news.html page when a visitor comes to the website and a specific page has not been specfied.

Does that make any sense?

Yes it does. Clearly simple! I just have one more question for you. (Actually two!)

When you call the next page, such as http://sitename.com/index.php?page=ponies after the user views the default “news” page, how do you plan on getting to that “ponies” page? Will there be a link on the news version or are you using menu buttons? There are several ways of doing this and I have a few suggestions.

First, if you use a link that says: http://sitename.com/index.php?page=news (or ponies)
The user will see this and perhaps bookmark the “ponies” page instead of reading your news first.
I suggest a different format where the name of the page is NOT shown on the address bar.
It would always just read: http://sitename.com/index.php But, will show different pages from the “hidden”
variable. This is easy to do. Either with a hidden field if you are using forms or in a session variable if not.
(I like to use session variables as they are easy and very very secure!)

Would you like to know how to use the session variables to do this project? Let me know…

Yes this is exactly what I would like

and I’ll be using text links to get to the various pages

Okay, just one more question… Is you page going to be a log-in site so only members can get to it or is it a general site that everyone can get to?

general site that everyone can view

Okay, here is the basics for using session variables… Fairly easy!

You have an index.php page.

Make the first line BEFORE your HTML, at the top of the page <?PHP session_start(); ?>
In other words,

<?PHP session_start(); ?>

What this does is create a “session” in the server attached to your browser. This is locked in and everyone who is online will have their own session. (Unique and nobody can get to anyone else’s…)

Now you will have to have that line on EVERY page that is going to use any session variables.

Next, when you set a session variable, you do it this way…
$_SESSION[‘page’]=“news”; This is a session variable called “page” and I set it to “news”…
(Easy to understand…)

To read a session variable, you just do it almost the same way, like this:
$newpage = $_SESSION[‘page’]; Simple to understand…

Now, how to use that in your program is just one extra step…
To process this page switch, you, of course, have to have the page selected, let’s say with a link.
I suggest doing it this way: (Similar to the way you mentioned at the beginning of this post!)

or
Press here to view our news!
(anything between the anchor tags, will send them to the news page… )

Now, you see I didn’t use index.php, I used another page that I made up called “switchpage.php”.
In this file, the page that is selected is put into a session variable and then switches to the real index.php file.

Here is the entire switchpage.php file… (Small…)
[php]

<?PHP session_start(); $_SESSION['page']= $_GET['page']; header('Location: index.php'); ?>

[/php]
That’s it for that file… What it does it capture the new page and passes it on to the index.php file. Simple.
(Also, it hides the ?page=“blah” from the user. It switches so fast they do not see this on the address bar!)

Now, it the actual index.php file, you just have to add two lines to be able to use the new page name:

At the top of index.php, add these lines as we discussed:
[php]

<?PHP session_start(); $page = $_SESSION['page']; if(!$page) $page="news"; ?>

[/php]
That allows use of session variables anywhere in index.php and pulls the page (if empty, uses news!)…

Well, THAT’s IT! Simple, easy and hides your choices better than www.xyz.com/index.php?page=news

Hope that was easy to understand. I am sure you will get it! Good luck, fill me in on how it works for you!

I didn’t have any luck so I did some digging around. I found the exact include I used to use and it used to work just fine (possibly on an older version of PHP).

[php]<?php
if(!$page || $page == “”){$page = “news”;} ?>

<?php include("$page.html"); ?>[/php]

I used to be able to just insert this into the table that houses my content in my layout. Then when going to the site it would load the index.php (layout page) and automatic grab my news.html file and put it in the content table. If I were to then go to http://siteabc.com/index.php?page=test It would then put the contents of that test.html file into the table instead of the news.html file. Is there anything as simple as that these days?

Well, either the way I posted would work or your way would work too. Either will!
But, your old way is fine. You will have to POST the page somehow, like from a form and submit.
Then, read it in in the index.php page. That is easy to do. If you are using an to post the page
such as your first poste with index.php?page=“news” … Then, you would get the page with $_GET[‘page’]

I guess I am mixed up on what you want. Either way would work, show some code and we can help you…

Probelm is the script is not working.

Website: http://gagreflexguild.com/

Index.php (Please excuse my really unorganized coding)

[code]

Gag Reflex - Dark Iron Alliance a:link { COLOR: #B4B4B4; } a:visited { COLOR: #B4B4B4; } a:hover {text-decoration:underline; COLOR: #B4B4B4; } a:active { COLOR: #B4B4B4; } .auto-style1 { background-image: url('http://gagreflexguild.com/layoutimages//layout_09.png'); } .auto-style2 { background-image: url('http://gagreflexguild.com/layoutimages//layout_03.png'); text-align: center; font-family: Tahoma; color: #84BDF8; } .auto-style3 { background-image: url('http://gagreflexguild.com/layoutimages//layout_19.png'); text-align: center; } .auto-style4 { background-image: url('http://gagreflexguild.com/layoutimages//layout_22.png'); text-align: center; } .auto-style5 { background-image: url('http://gagreflexguild.com/layoutimages//layout_25.png'); text-align: center; } .auto-style7 { text-align: left; } .auto-style8 { color: #B4B4B4; font-family: Tahoma; text-decoration: underline; } .auto-style14 { text-align: left; font-family: Tahoma; color: #00CEB6; font-size: small; } .auto-style10 { text-align: left; font-family: Tahoma; color: #B4AE4A; font-size: small; } .auto-style11 { text-align: left; font-family: Tahoma; color: #0023FF; font-size: small; } .auto-style13 { text-align: left; font-family: Tahoma; color: #A682CA; font-size: small; } .auto-style12 { text-align: left; font-family: Tahoma; color: #CE9C6E; font-size: small; } .auto-style25 { color: #F58CD0; font-family: Tahoma; text-align: left; font-size: small; } .auto-style26 { text-align: left; font-family: Tahoma; color: #69CCF0; font-size: small; } .auto-style27 { text-align: left; font-family: Tahoma; color: #ABD473; font-size: small; } .auto-style28 { text-align: left; font-family: Tahoma; color: #C41E3A; font-size: small; } .auto-style29 { text-align: left; font-family: Tahoma; color: #FF7D0A; font-size: small; } .auto-style31 { text-align: right; font-family: Tahoma; text-decoration: underline; color: #008000; font-size: small; } .auto-style32 { text-align: right; font-family: Tahoma; color: #FF0000; text-decoration: underline; font-size: small; } .auto-style33 { text-align: center; font-family: Tahoma; color: #808080; text-decoration: underline; } .auto-style34 { text-align: left; font-family: Tahoma; color: #FFFFFF; font-size: small; } .auto-style35 { color: #B4B4B4; font-family: Ebrima; text-decoration: underline; } .auto-style37 { color: #008080; } .auto-style39 { color: #B4B4B4; } .auto-style40 { background-image: url('http://gagreflexguild.com/layoutimages//layout_30.png'); } .auto-style41 { background-image: url('http://gagreflexguild.com/layoutimages//layout_28.png'); } .auto-style42 { color: #B4B4B4; font-family: Tahoma; text-decoration: underline; background-image: url('http://gagreflexguild.com/layoutimages/layout_29.png'); }
HOME - FORUMS - APPLY TO GAG REFLEX - GUILD VIDEOS - PROGRESSION
</tr>
GUILD RANK

RANK GOES HERE
RECRUITMENT NEEDS
<tr>
	<td style="width: 1303px" class="auto-style34"><strong>PRIEST</strong></td>
	<td style="width: 952px" class="auto-style32"><strong>CLOSED</strong></td>
</tr>

<tr>
	<td style="width: 1303px" class="auto-style10"><strong>ROGUE</strong></td>
	<td style="width: 952px" class="auto-style32"><strong>CLOSED</strong></td>
</tr>

<tr>
	<td style="width: 1303px; height: 2px;" class="auto-style11"><strong>SHAMAN</strong></td>
	<td style="width: 952px; height: 2px;" class="auto-style32"><strong>CLOSED</strong></td>
</tr>
	<tr>
	<td style="width: 1303px" class="auto-style13"><strong>WARLOCK</strong></td>
	<td style="width: 952px" class="auto-style32"><strong>CLOSED</strong></td>
</tr>

<tr>
	<td style="width: 1303px" class="auto-style12"><strong>WARRIOR</strong></td>
	<td style="width: 952px" class="auto-style32"><strong>CLOSED</strong></td>
</tr>
DEATH KNIGHT CLOSED
DRUID CLOSED
HUNTER CLOSED
MAGE CLOSED
MONK OPEN
PALADIN CLOSED
WORLD OF LOGS
WOL HERE
  <?php if(!$page || $page == ""){$page = "news";} ?> <?php include("$page.html"); ?>  
[/code]

The PHP include can be found at the very bottom.

What this used to do for me is load the index.php and in the blankbottomost table it would insert my news.html file.

Here is my news.html file

[code]

News [/code]

this include when working automaticcally pulls the news from a PHP program I have called Fusion News.

Lastly here is a test page that I created for this test.

Test.html

[code]

Hello

Hello

[/code]

Now when I go to httP;//gagreflex.com/index.php?page=test

It shoudl load that test.html docdument in the content table but it doesn’t. That is my issue.

Updated Code to reflect changes I’ve made - the news.php page is now displaying properly. Now when I change the URL to say http://gagreflexguild.com/index.php?page=test, it should load my test.php page in the content area INSTEAD of the news.php page. It is NOT doing that. What should I do?

[code]

Gag Reflex - Dark Iron Alliance a:link { COLOR: #B4B4B4; } a:visited { COLOR: #B4B4B4; } a:hover {text-decoration:underline; COLOR: #B4B4B4; } a:active { COLOR: #B4B4B4; } .auto-style1 { background-image: url('http://gagreflexguild.com/layoutimages//layout_09.png'); } .auto-style2 { background-image: url('http://gagreflexguild.com/layoutimages//layout_03.png'); text-align: center; font-family: Tahoma; color: #84BDF8; } .auto-style3 { background-image: url('http://gagreflexguild.com/layoutimages//layout_19.png'); text-align: center; } .auto-style4 { background-image: url('http://gagreflexguild.com/layoutimages//layout_22.png'); text-align: center; } .auto-style5 { background-image: url('http://gagreflexguild.com/layoutimages//layout_25.png'); text-align: center; } .auto-style7 { text-align: left; } .auto-style8 { color: #B4B4B4; font-family: Tahoma; text-decoration: underline; } .auto-style14 { text-align: left; font-family: Tahoma; color: #00CEB6; font-size: small; } .auto-style10 { text-align: left; font-family: Tahoma; color: #B4AE4A; font-size: small; } .auto-style11 { text-align: left; font-family: Tahoma; color: #0023FF; font-size: small; } .auto-style13 { text-align: left; font-family: Tahoma; color: #A682CA; font-size: small; } .auto-style12 { text-align: left; font-family: Tahoma; color: #CE9C6E; font-size: small; } .auto-style25 { color: #F58CD0; font-family: Tahoma; text-align: left; font-size: small; } .auto-style26 { text-align: left; font-family: Tahoma; color: #69CCF0; font-size: small; } .auto-style27 { text-align: left; font-family: Tahoma; color: #ABD473; font-size: small; } .auto-style28 { text-align: left; font-family: Tahoma; color: #C41E3A; font-size: small; } .auto-style29 { text-align: left; font-family: Tahoma; color: #FF7D0A; font-size: small; } .auto-style31 { text-align: right; font-family: Tahoma; text-decoration: underline; color: #008000; font-size: small; } .auto-style32 { text-align: right; font-family: Tahoma; color: #FF0000; text-decoration: underline; font-size: small; } .auto-style33 { text-align: center; font-family: Tahoma; color: #808080; text-decoration: underline; } .auto-style34 { text-align: left; font-family: Tahoma; color: #FFFFFF; font-size: small; } .auto-style35 { color: #B4B4B4; font-family: Ebrima; text-decoration: underline; } .auto-style37 { color: #008080; } .auto-style39 { color: #B4B4B4; } .auto-style40 { background-image: url('http://gagreflexguild.com/layoutimages//layout_30.png'); } .auto-style41 { background-image: url('http://gagreflexguild.com/layoutimages//layout_28.png'); } .auto-style42 { color: #B4B4B4; font-family: Tahoma; text-decoration: underline; background-image: url('http://gagreflexguild.com/layoutimages/layout_29.png'); }
HOME - FORUMS - APPLY TO GAG REFLEX - GUILD VIDEOS - PROGRESSION
</tr>
GUILD RANK

RANK GOES HERE
RECRUITMENT NEEDS
<tr>
	<td style="width: 1303px" class="auto-style34"><strong>PRIEST</strong></td>
	<td style="width: 952px" class="auto-style32"><strong>CLOSED</strong></td>
</tr>

<tr>
	<td style="width: 1303px" class="auto-style10"><strong>ROGUE</strong></td>
	<td style="width: 952px" class="auto-style32"><strong>CLOSED</strong></td>
</tr>

<tr>
	<td style="width: 1303px; height: 2px;" class="auto-style11"><strong>SHAMAN</strong></td>
	<td style="width: 952px; height: 2px;" class="auto-style32"><strong>CLOSED</strong></td>
</tr>
	<tr>
	<td style="width: 1303px" class="auto-style13"><strong>WARLOCK</strong></td>
	<td style="width: 952px" class="auto-style32"><strong>CLOSED</strong></td>
</tr>

<tr>
	<td style="width: 1303px" class="auto-style12"><strong>WARRIOR</strong></td>
	<td style="width: 952px" class="auto-style32"><strong>CLOSED</strong></td>
</tr>
DEATH KNIGHT CLOSED
DRUID CLOSED
HUNTER CLOSED
MAGE CLOSED
MONK OPEN
PALADIN CLOSED
WORLD OF LOGS
WOL HERE
  <?php if(!$page || $page == ""){$page = "news";} ?> <?php include("$page.php"); ?>  
[/code]

Because of the changes I made to the PHP Include in the index.php page I believe all of my content pages have to have a .php extension in order for it to work. I have updated my test.html page to be called test.php.

http://gagreflexguild.com/test.php

This is just a basic .html document with 1 word.

When I change the link to

http://gagreflexguild.com/index.php?page=test

It should load the index.php page first and then it should pull the test.php page and include it straight into my content area. Instead, no matter what I do I just get the news.php page. Is there a problem with my code?

Fixed and Working

Final Code Used:

[code]

Gag Reflex - Dark Iron Alliance a:link { COLOR: #B4B4B4; } a:visited { COLOR: #B4B4B4; } a:hover {text-decoration:underline; COLOR: #B4B4B4; } a:active { COLOR: #B4B4B4; } .auto-style1 { background-image: url('http://gagreflexguild.com/layoutimages//layout_09.png'); } .auto-style2 { background-image: url('http://gagreflexguild.com/layoutimages//layout_03.png'); text-align: center; font-family: Tahoma; color: #84BDF8; } .auto-style3 { background-image: url('http://gagreflexguild.com/layoutimages//layout_19.png'); text-align: center; } .auto-style4 { background-image: url('http://gagreflexguild.com/layoutimages//layout_22.png'); text-align: center; } .auto-style5 { background-image: url('http://gagreflexguild.com/layoutimages//layout_25.png'); text-align: center; } .auto-style7 { text-align: left; } .auto-style8 { color: #B4B4B4; font-family: Tahoma; text-decoration: underline; } .auto-style14 { text-align: left; font-family: Tahoma; color: #00CEB6; font-size: small; } .auto-style10 { text-align: left; font-family: Tahoma; color: #B4AE4A; font-size: small; } .auto-style11 { text-align: left; font-family: Tahoma; color: #0023FF; font-size: small; } .auto-style13 { text-align: left; font-family: Tahoma; color: #A682CA; font-size: small; } .auto-style12 { text-align: left; font-family: Tahoma; color: #CE9C6E; font-size: small; } .auto-style25 { color: #F58CD0; font-family: Tahoma; text-align: left; font-size: small; } .auto-style26 { text-align: left; font-family: Tahoma; color: #69CCF0; font-size: small; } .auto-style27 { text-align: left; font-family: Tahoma; color: #ABD473; font-size: small; } .auto-style28 { text-align: left; font-family: Tahoma; color: #C41E3A; font-size: small; } .auto-style29 { text-align: left; font-family: Tahoma; color: #FF7D0A; font-size: small; } .auto-style31 { text-align: right; font-family: Tahoma; text-decoration: underline; color: #008000; font-size: small; } .auto-style32 { text-align: right; font-family: Tahoma; color: #FF0000; text-decoration: underline; font-size: small; } .auto-style33 { text-align: center; font-family: Tahoma; color: #808080; text-decoration: underline; } .auto-style34 { text-align: left; font-family: Tahoma; color: #FFFFFF; font-size: small; } .auto-style35 { color: #B4B4B4; font-family: Tahoma; text-decoration: underline; } .auto-style37 { color: #008080; } .auto-style39 { color: #B4B4B4; } .auto-style40 { background-image: url('http://gagreflexguild.com/layoutimages//layout_30.png'); } .auto-style41 { background-image: url('http://gagreflexguild.com/layoutimages//layout_28.png'); } .auto-style42 { color: #B4B4B4; font-family: Tahoma; text-decoration: underline; background-image: url('http://gagreflexguild.com/layoutimages/layout_29.png'); }
HOME - FORUMS - APPLY TO GAG REFLEX - ABOUT US - ROSTER - GUILD VIDEOS
</tr>
GUILD RANK

WoW Guild Rankings
RECRUITMENT NEEDS
<tr>
	<td style="width: 1303px" class="auto-style34"><strong>PRIEST</strong></td>
	<td style="width: 952px" class="auto-style32"><strong>CLOSED</strong></td>
</tr>

<tr>
	<td style="width: 1303px" class="auto-style10"><strong>ROGUE</strong></td>
	<td style="width: 952px" class="auto-style32"><strong>CLOSED</strong></td>
</tr>

<tr>
	<td style="width: 1303px; height: 2px;" class="auto-style11"><strong>SHAMAN</strong></td>
	<td style="width: 952px; height: 2px;" class="auto-style32"><strong>CLOSED</strong></td>
</tr>
	<tr>
	<td style="width: 1303px" class="auto-style13"><strong>WARLOCK</strong></td>
	<td style="width: 952px" class="auto-style32"><strong>CLOSED</strong></td>
</tr>

<tr>
	<td style="width: 1303px" class="auto-style12"><strong>WARRIOR</strong></td>
	<td style="width: 952px" class="auto-style32"><strong>CLOSED</strong></td>
</tr>
DEATH KNIGHT CLOSED
DRUID CLOSED
HUNTER CLOSED
MAGE CLOSED
MONK OPEN
PALADIN CLOSED
WORLD OF LOGS
  <?php //If is defined URL variable 'roster' if(isset($_GET['roster'])){ // include page roster include('roster.php'); //else if is defined URL variable 'videos' }else if(isset($_GET['videos'])){ // include page videos include('videos.php'); //else if is defined URL variable 'about' }else if(isset($_GET['about'])){ // include page about include('about.php'); // in all other cases include the home page } else { include('news.php'); } ?>  
[/code]

Thanks so much for the help.

Glad I could help. Sorry it took so long, but, we got it there! See you in your next post… Good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service