[web] Using php Include()

Started by
11 comments, last by ID Merlin 14 years, 3 months ago
Hi, First off I'm new to webdev, so new I started about 3 hours ago. Im trying to rebuild my portfolio from just using wordpress to something new and nice. So far I have managed to get my headers and menu working, upon realising the maintenece nightmare of copying this onto every page and then deciding I want to add a link, I googled for a solution to this problem, and came across using php Include(). The problem is, I cant get it to work, nor for the life of me, and using alot of google-fu can I work out why, maybe someone can help me out here. Thanks, Scott Index.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<LINK rel="StyleSheet" href="DefaultStylesheet.css" type="text/css" />

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>gameXcore Studios</title>
</head>
<body>

		<?php include("Header.php"); ?>


</body>
</html>

Header.php

<LINK rel="StyleSheet" href="DefaultStylesheet.css" type="text/css" />

<html xmlns="http://www.w3.org/1999/xhtml" >
<body>

	<div id="wrapper">
		<div id="header"> </div>
		
		<div id="access">
			<div id="menu">
				<ul>
					<li><a href="http://gamexcore.co.uk">Home</a></li>
					<li><a href="http://gamexcore.co.uk/?page_id=5" title="Games">Games</a></li>
					<li><a href="http://gamexcore.co.uk/?page_id=94" title="Projects">Projects</a></li>
					<li id="rightAlign"><a href="http://gamexcore.co.uk/?page_id=8" title="Contact">Contact</a></li>
				</ul>
			</div>
		</div> <!-- #access -->
	</div>

</body>
</html>

Defaultstylesheet.css

@charset "UTF-8";
/* CSS Document */

body {
	margin: 0px;
	padding-top: 0px;
	font-size: 14px;
	background-color: #4c4c4c;
	background-image: url('Images/Background.png');
	background-repeat: no-repeat;
	background-attachment: fixed;
	font-family: Georgia, Times New Roman, Times, serif;
	color: #FFFFFF;
	height: 100%;
	min-height: 100%;
}

#wrapper
{
	padding-top: 8px;
	background-image: url(Images/ContentStripeBackground.png);
	background-repeat: repeat-y;
	margin: auto;
	width: 896px;
	height: 100%;
}

#header
{
	background: transparent url(Images/Header.png) no-repeat scroll 0 0;
	height: 144px;
	width: 832px;
	position: relative;
	margin: auto;
}

#menu
{
	background-image: url('Images/NavBackground.png');
	font-size: 13px;
	height: 56px;
	width: 844px;
	margin: auto;
	position: relative;
}

#menu ul
{
	padding-top: 10px;
	padding-left: 8px;
	list-style-type: none;
}

#menu li
{
	background: url('Images/NavButton.png');
	background-repeat: no-repeat;
	float: left;
	width: 114px;
	height: 34px;
	text-align: center;
	padding-right: 8px;
}

#menu li a
{
	color: #000000;
	font-weight: bold;
	text-align: center;
	text-decoration: none;
	width: 114px;
	height: 34px;
	float: left;
	padding-top: 8px;
}

#menu li a:hover, #menu li .on
{
	background: url('Images/NavButtonHover.png');
	background-repeat: no-repeat;
	float: left;
	width: 114px;
	height: 34px;
	text-decoration: none;
}

#menu li#rightAlign {
float: right; /* <- make last float to right */
}

Game development blog and portfolio: http://gamexcore.co.uk
Advertisement
Your document structure gets totally messed up by Header.php when you include it in Index.php.
The CSS link is to be placed between <head></head>, and the content between <body></body>.
Also, case matters. It's not <link rel="styleSheet">, rather it's <link rel="stylesheet">.

If you're on Linux/Unix, case of file names matters too. So if your Defaultstylesheet.css does have this exact spelling, you must also refer to it with that spelling in the <link> element.
I made some changes but still nothing, I'm not sure if im misunderstanding what Include() does, does it simply place the text of that file into the file that calls it, thus I shouldnt have the tags such as <html><head><body> or does it merge the files?

My modified code:
	<html xmlns="http://www.w3.org/1999/xhtml" ><head>	<LINK rel="stylesheet" href="DefaultStylesheet.css" type="text/css" /></head><body> 	<div id="wrapper">		<div id="header"> </div>				<div id="access">			<div id="menu">				<ul>					<li><a href="http://gamexcore.co.uk">Home</a></li>					<li><a href="http://gamexcore.co.uk/?page_id=5" title="Games">Games</a></li>					<li><a href="http://gamexcore.co.uk/?page_id=94" title="Projects">Projects</a></li>					<li id="rightAlign"><a href="http://gamexcore.co.uk/?page_id=8" title="Contact">Contact</a></li>				</ul>			</div>		</div> <!-- #access -->	</div></body></html>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head>    <title>gameXcore Studios</title>		<LINK rel="stylesheet" href="DefaultStylesheet.css" type="text/css" /></head><body>		<?php include("Header.php"); ?></body></html>
Game development blog and portfolio: http://gamexcore.co.uk
Quote:does it simply place the text of that file into the file that calls it,

It places the code at that place. That can be a mixture of PHP and HTML.
Quote:thus I shouldnt have the tags such as <html><head><body>

Exactly, that's the error.
See now this is what I assumed, but stripping down my Header.php file to simply nothing but:

	<div id="wrapper">		<div id="header"> </div>				<div id="access">			<div id="menu">				<ul>					<li><a href="http://gamexcore.co.uk">Home</a></li>					<li><a href="http://gamexcore.co.uk/?page_id=5" title="Games">Games</a></li>					<li><a href="http://gamexcore.co.uk/?page_id=94" title="Projects">Projects</a></li>					<li id="rightAlign"><a href="http://gamexcore.co.uk/?page_id=8" title="Contact">Contact</a></li>				</ul>			</div>		</div> <!-- #access -->	</div>


or even just "ARGHHHHHHHHHHHHH" still leavs me with nothing from "Header.php" displaying, is there something I'm missing here, because its got me frazzled?

Thanks for the help, Scott
Game development blog and portfolio: http://gamexcore.co.uk
Incase it helps, heres what I should have once this include works, this page is made by pasting the header and menu code in:

http://gamexcore.co.uk/Home.php
Game development blog and portfolio: http://gamexcore.co.uk
Are you 100% sure that you use correct spelling for the filenames? I can't see anything that's wrong here.
Do you have the code running on the server, so I can take a look at what it renders?
*uploads code to server*, aaaand it works. This is interesting, I was running it localy, I'm assuming with php you have to be running on a webserver which supports php?

Thankyou very much for the help, it has ben greatly appreciated.

Scott
Game development blog and portfolio: http://gamexcore.co.uk
I'm glad I could help. :)

Yes, you need a PHP enabled web server. I recommend XAMPP, it's a complete and free web server solution and really easy to set up and use.

This topic is closed to new replies.

Advertisement