Forum Moderators: coopster
<body> <div id="maincontent">
<p>blah blah blah</p>
</div>
<?php include 'navbar.html'?>
</body>
</html>
Warning: main(): stream does not support seeking in /home/hapless/public_html/test.php on line 10
Is the path right?
When you are including a file in another file you need the path relative to the file that is doing the including (test.php).
Most often I use root relative paths. I start at the root of the site and put the full path to the file. This way no matter where on the site I include it will be the same path.
If your navbar.html is in the root directory of your site you can do it like so.
include $_SERVER [ca.php.net]['DOCUMENT_ROOT'] . "/navbar.html";
SPAN elements, in conjunction with the id and class attributes, offer a generic mechanism for adding structure to documents. These elements define content to be inline (SPAN) or block-level (DIV) but impose no other presentational idioms on the content. Thus, authors may use these elements in conjunction with style sheets, the lang attribute, etc., to tailor HTML to their own needs and tastes. That's the official text from the W3C. OK, do you want a real working example? Copy and paste the text below into any text editor, save it as
tab1.php and post it to your server. Open it in a browser and you'll see how this stuff can work: tab1.php
<html>
<head>
<title>PHP include() and navigation</title>
<style>
body {background-color: #CCC; }
#topnav a:hover, #topnav a:active {background-color: #603; color: #FFC; }
.tab1 #topnav #tab1 {color: #FFC; background: transparent;}
.tab2 a#tab2, .tab3 a#tab3, .tab4 a#tab4 {color: #FFC; background: transparent;}
</style>
</head>
<?php
$tab = basename($_SERVER['PHP_SELF'], ".php");
$body_attributes = '';
if ($tab) $body_attributes = "class=\"$tab\"";
?>
<body<?php if ($body_attributes) print " $body_attributes";?>>
<div id="topnav">
<a href="tab1.php" id="tab1" title="Tab1">tab1</a>
<b>¦</b><a href="tab2.php" id="tab2" title="Tab2">tab2</a>
<b>¦</b><a href="tab3.php" id="tab3" title="Tab3">tab3</a>
<b>¦</b><a href="tab4.php" id="tab4" title="Tab4">tab4</a>
</div>
</body></html>
.
.
.
<body<?php if ($body_attributes) print " $body_attributes";?>>
<?php include("included.htm");?>
</body></html>
included.htm
<div id="topnav">
<a href="tab1.php" id="tab1" title="Tab1">tab1</a>
<b>¦</b><a href="tab2.php" id="tab2" title="Tab2">tab2</a>
<b>¦</b><a href="tab3.php" id="tab3" title="Tab3">tab3</a>
<b>¦</b><a href="tab4.php" id="tab4" title="Tab4">tab4</a>
</div>
tab1.php file with the new changes (<div> has been removed) and save this new included.htm file and FTP them to your server (make sure they are in the same directory as I did not put any path on the PHP include() function). Open it in your browser. Starting to make sense what you can do?
<?
include $_SERVER['DOCUMENT_ROOT'] . "/header.html";
?>
<?php
include $_SERVER['DOCUMENT_ROOT'] . "/navbar.html";
?>
<?php
include $_SERVER['DOCUMENT_ROOT'] . "/content.html";
?>
</div>
<?php
include $_SERVER['DOCUMENT_ROOT'] . "/navbar.html";
?>
<?php
include $_SERVER['DOCUMENT_ROOT'] . "/footer.html";
?> Now, say I want to put the text "Well, I sure hope this works right..." inside the table that is included from content.html. Can I actually do that? Or maybe I could split the code into two parts, the first part of the table's code included before the content, and the second half included after the content, clsoing the table? Sorry about the long, confusing sentence...
<?php
include $_SERVER['DOCUMENT_ROOT'] . "/header.html";
include $_SERVER['DOCUMENT_ROOT'] . "/navbar.html";
$my_variable_text = 'Well, I sure hope this works right...';
include $_SERVER['DOCUMENT_ROOT'] . "/content.php";
?>
</div>
<?php
include $_SERVER['DOCUMENT_ROOT'] . "/navbar.html";
include $_SERVER['DOCUMENT_ROOT'] . "/footer.html";
?>
<div>
<!-- a bunch more html -->
<p>
<?php
// Start PHP parsing and pring my variable text out...
print $my_variable_text;
</p>
?>
There are several other rules regarding how quoted strings are parsed that may override the above. For example while double quoted strings are parsed for variable replacement, single quoted strings are not. This means that if your string does not have any variable replacement, then using single quotes is more efficient as the string does not need to be parsed.
Watch out for subtle errors caused by using the wrong quoting style.
If your string is coming from or going to a database or some other external source, then you will probably want to look at using addslashes() and it's siblings.