[web] Using php Include()

This topic is 2982 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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">

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

</body>
</html>


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

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

<div id="wrapper">

<div id="access">
<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;
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
{
background-image: url(Images/ContentStripeBackground.png);
background-repeat: repeat-y;
margin: auto;
width: 896px;
height: 100%;
}

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

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

{
list-style-type: none;
}

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

{
color: #000000;
font-weight: bold;
text-align: center;
text-decoration: none;
width: 114px;
height: 34px;
float: left;
}

{
background: url('Images/NavButtonHover.png');
background-repeat: no-repeat;
float: left;
width: 114px;
height: 34px;
text-decoration: none;
}

float: right; /* <- make last float to right */
}



Share on other sites
Your document structure gets totally messed up by Header.php when you include it in Index.php.

Share on other sites

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.

Share on other sites
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>

Share on other sites
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

Exactly, that's the error.

Share on other sites
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

Share on other sites

http://gamexcore.co.uk/Home.php

Share on other sites
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?

Share on other sites
*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

Share on other sites
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.

Share on other sites
Can you install something like that locally to develop locally? (If that made any sense) For the moment since I have Visual Studio web developer I've switched to doing all my work on my live server and using the FTP project, having it take forever to do anything (open files, save files, etc) and also crash when I press F5 to run it is a proper PITA.

Share on other sites
Take a look at the Microsoft Web Platform Installer. This allows you to quickly and easily configure your local copy of IIS (Microsoft's web server technology), including the option to install PHP support.

I also recommend sticking

error_reporting(E_ALL);ini_set('display_errors', true);

at the top of your main source file when developing PHP scripts - it'll spit out error messages/warnings/notices to help you write more robust scripts. require() may be a better choice instead of include(), as require will trigger a fatal error if the requested file is not found.

Share on other sites
I run MySQL and PHP on my Windows XP system for local development. Both have Windows Installer packages available, and are (fairly) easy to set up and get working.