PHP Include Navigation and Footer on Main Page

Please help… this should be simple but it is not working for me! I am a total newbie but I’ve been reading about this for over a week and no luck :frowning:

I have a navigation.php file that has the following code (I’ve also tried it very simple without the div class but it didn’t make a difference):

[code]

>Navigation
</HEAD>[/code]

Then my main page has lots of code but I’ll only paste the relevant part:

[code]

<link rel="stylesheet" href="http://s3.amazonaws.com/codecademy-content/courses/ltp/css/bootstrap.css">
<link rel="stylesheet" href="main.css">
<?php require("navigation.php"); ?> [/code] (The home page file is attached too).

I’ve tried require and I’ve tried include.

Then I put it on our FTP and view them both … the navigation is not being pulled into the main page… WHY???

Please help! Thanks, K


new home.pdf (14.5 KB)

Don’t know what you are doing, but with me it works fine.

Can you show me the stylesheet?

That will not pass W3C Markup Validation: http://validator.w3.org/ and I would clean up the code before trying to solve the problem. Heck, there is a possibility that it will solve it.

Here’s an example (This is what I use):
[php]

<?php echo (isset($metaDesc)) ? $metaDesc : NULL; ?> <?php echo $pageTitle; ?>

J.R. Pepp | A Website Design & Development Company

  • Pepster's Place Facebook Page
  • Pepster's Twitter Profile
  • John Pepp's LinkedIn Profile
  • John Pepp's Flickr Profile
&Congruent;
  • Home
  • About
  • Forums
  • Contact
  • Gallery
  • The List
  • <?php if ($pageTitle == "HTML5, CSS3, jQuery and PHP Forums" || preg_match("/ Forum/i", $pageTitle)) { if ($user) { echo '
  • Logout
  • '; } else { echo '
  • Login
  • '; } } ?> <?php echo (!($user) && ($pageTitle == "HTML5, CSS3, jQuery and PHP Forums" || preg_match("/ Forum/i", $pageTitle))) ? '
  • Register
  • ' : NULL; ?>
	<div class="mobileHeader">
		<div class="headerStyle">
			<div class="mobileLogo">
				<h2 class="mLogo">J.R.Pepp - Web Development</h2>
			</div>
		</div>
	</div>
	<!-- END OF MOBILE HEADER --> 
	<?php include 'lib/views/login.html'; ?>
</div>
<!-- END OF NAVIGATION STYLING -->
Contact Form Name (required)
Subject (required)
E-Mail (required)
Message (required)
×
[/php]

All your HTML tags should go between the tags and what I call housekeeping chores (CSS for example) goes between the Tags.

As a side note, I normally call this file, header.inc.php and for every php page I just do this:
[php]include ‘lib/includes/header.inc.php’;
?>
[/php]
before the main portion of my HTML page, I don’t have to put the top portion of the HTML code and above (plus my navigation and header tags are in that file, thus saving me from having to write repetitive code) for it is in that file.

Hi aaad,
How are you looking at it? I’m opening it in Google Chrome and it doesn’t work ???

I’ve attached the style sheet per your request.


main css.pdf (6.9 KB)

Hi Strider64,
I’ll take a look at the code you posted but I did a tutorial and basically build the code via the dicrection at codecademy.com so … I would think it’s good. As I’m really a beginner at this I just have to trust what I’m learning ;); but, as I said, I’ll take a look at your code and compare it to mine. BTW I never heard of the validator so I’ll check that out too! Thanks!

I would be surprised and shocked :o if code academy told you to make to two tags, looking over you code again (I did a quick glance over the first time), I’m taking a guess that it should be (Lose the caps it’s bad coding practicing) than . HTML5 doesn’t care if you use lower or uppercase characters, though good coding practice is to use lowercase characters. I’m sure codeacademy is good, without trying to sound rude you have to pay attention to detail (my old boss use to say this to me all the time. ;D). Validating your HTML and even your CSS is a good way to correct those errors. I would check into firebug for Firefox and I think chrome has it’s own built in debugger (I use chrome but can’t remember if I installed an add-on or not) that will also help in the dubbing stage.

Hi Strider64,
I was able to get my navigation.inc.php file to look ok in Chrome (it was just showing me the code and by using your example I now see the web page); I’ve still got formatting issues but I’ll work on those.

BUT… the include is still not working … when I pull up my webpage that I put:

[code]

<?php include 'navigation.inc.php'; ?>[/code]

I still do not see the navigation. Any more ideas?

thanks,
K

Well, one major issue with this code is the headers you are sending to the browser.
There can be only one ! ( Yes, a Highlander quote! )

So, headers are sent out to the browser after your PHP code is completed.
If you send out two headers, you will receive odd errors which can be hard to see at first.

Now, remember, you can NOT see your PHP code browser-side, but, you can see what is being sent out to the browser. Just view the finished page and RIGHT-CLICK on it and select VIEW-SOURCE. Then, review your code “AS THE BROWSER SEES IT”, not as you think it was sent out. You can debug where it is missing your content and do further testing.

Just remember that only one page header may be sent out to the browser. If your main page has a inside it, you can not use inside your include file.

Hope that helps… Oh, and yes, pass the output of your page thru the W3 Validator which may show up other errors we all miss… Good luck

Ok everyone … this is quite frustrating… this should really be simple but it’s just not working for me.

I created very simple files based on info from: http://www.tizag.com/phpT/include.php and http://www.tutorialspoint.com/php/php_file_inclusion.htm which are almost identical and the include still does not work … FRUSTRATING :’(

menu.php file:

[code]

Home - About Us - Links - Contact Us
[/code]

index.php file:

[code]<?php include("menu.php"); ?>

This is my home page that uses a common menu to save me time when I add new pages to my website!

[/code]

result SHOULD BE:

Home - About Us - Links - Contact Us This is my home page that uses a common menu to save me time when I add new pages to my website!

But I’m seeing:

This is my home page that uses a common menu to save me time when I add new pages to my website!

WHY? Anything at all to help with this seemingly simple (NOT) problem would be greatly appreciated!!!

BTW ErnieAlex – Love the Highlander quote! :wink:

SimKid, sorry, KDsim, if you INCLUDE a file, id can NOT have headers or doctypes in it.
It must be straight code with out , , or doctype’s.

You show a MENU.PHP file that has tags, tags in it. YOU CAN NOT place that inside of another page. Your index.php file starts without any HTML or BODY tags, but, ends with them. So, that is not a valid page either. Your layout should be something like this:
index.php file:

header coding... HTML and PHP coding... Now inside the sections, you can INCLUDE another file. BUT, that file can only be HTML and PHP codings no page coding. SO, NONE of these above tags should be used inside the INCLUDE'd file. So, if you placed <?PHP include("menu.php"); ?> inside the tags in the above sample, it would have to look like this: menu.php file: blah blah2 whatever you want but no tags to tell the system that it is a HTML page, no html/body/head tags.

Does that make sense?

Do you have a current test page working online so we can see it again?
Create a simple one page sampler based on what I just showed you and send us the link to it online and we can test it for you. Also, to verify it, go to the w3-validator site to check it out for errors. (here is the link to it, you just type in or paste your one page sampler test URL and press validate… Easy!)
http://validator.w3.org/

Hi ErnieAlex,
I did what you suggested and what I discovered is that when I Run it in Notepad++ (via Chrome) it does not show the include the file but…
When I put the files on the web to share with you it did include the file.

Maybe making the few changes to the code that you suggested was the key because it did not work (even on the web) yesterday! ::slight_smile: OY…now I have to play with the actual file and see if I can get it to work!?! ;D So excited!!

By the way, the code ended up looking like this:

menu.php

<a href="http://www.tutorialspoint.com/index.htm">Home</a> - <a href="http://www.tutorialspoint.com/ebxml">ebXML</a> - <a href="http://www.tutorialspoint.com/ajax">AJAX</a> - <a href="http://www.tutorialspoint.com/perl">PERL</a> <br />

index1.php

[code]

Test <?php include ('menu.php'); ?>

This is my home page that uses a common menu to save me time when I add new pages to my website!

[/code]

Thanks,
WHEN (not if ;)) I get it working I’ll let y’all know since you’ve been very patient in helping me through this!

Well, you have to run everything from a server.
So, either you upload it all to the server and test it there,
OR, you can install a local server on your computer and test there.
As far as I know, you can NOT run a PHP program from Notepad++.
(It does not serve out PHP programs.)

No problem being patient. That is what we are here for.
(That is actually why sites like this have the karma system.
But, I don’t care about karma, I just like to help out when I have time to do so…)

Now, just remember that includes (all files that are included in a page) use anything installed before they are included. So, if you have a CSS file included or installed in a page and then have an included file like your sample, they they also use the CSS file. What is nice about this is that you can put all of your styling code into one CSS file and put it in the main code and all of your includes will use it. Included files are actually very easy to use, once you remember not to set them up as separate pages and leave out the doctypes, html/body tags, etc…

Glad you got it working. I think we can mark this solved. Any further questions, let’s start a new topic.
Good going, kdsim !

Sponsor our Newsletter | Privacy Policy | Terms of Service