Forum Moderators: coopster

Message Too Old, No Replies

Including a nav bar

Changing one file to change the content of the nav bar

         

Hapless

11:42 am on Jan 16, 2004 (gmt 0)

10+ Year Member



Ok, at first I thought it might be possible to do this with CSS [webmasterworld.com] but I got pointed to using this code to include the nav bar in PHP...

<body>

<div id="maincontent">
<p>blah blah blah</p>
</div>

<?php include 'navbar.html'?>

</body>
</html>


So, is that the PHP to insert? Because I got this error...

Warning: main(): stream does not support seeking in /home/hapless/public_html/test.php on line 10

Also, can you do the same thing for the rest of your layout? I'm not talking about appearance like with CSS, but structure and content of the pages.

jatar_k

4:44 pm on Jan 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld Hapless,

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

Hapless

9:03 pm on Jan 16, 2004 (gmt 0)

10+ Year Member



Ah, that works better, thank you. Also, how do you use divs to make the content appear inside the template that will be imported from an external file with PHP?

jatar_k

9:12 pm on Jan 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



<div id="maincontent">
<? include $_SERVER['DOCUMENT_ROOT'] . "/content.html";?>
</div>

should work

Hapless

12:17 am on Jan 17, 2004 (gmt 0)

10+ Year Member



I kinda meant something more than that...I don't know much of anything about divs, like I said.

Hapless

1:17 am on Jan 17, 2004 (gmt 0)

10+ Year Member



Err, to be a bit more specific, I don't know how I should define the "maincontent" div in the CSS file. Neither do I know where I'm supposed to put the actual content in that code.

coopster

5:29 pm on Jan 19, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The DIV [w3.org] and
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>

This example shows you how you can change the <body> class on the fly. You wanted to change the links, correct? Go ahead! You can change them in any number of ways. Keeping with the example here, let's pull the actual link navigation out and put it into a separate file that we will include using PHP. Replace the entire <div> above with a PHP include:

.
.
.
<body<?php if ($body_attributes) print " $body_attributes";?>>
<?php include("included.htm");?>
</body></html>

Create a new html file called included.htm and put the <div> code in it:

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>

Save the
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?

Hapless

6:41 pm on Jan 19, 2004 (gmt 0)

10+ Year Member



Ok, I think that problem I have solved, but is it possible to put content in the original file inside a table included from another file? Or must I just leave that code in place? Let me show you my code...

<?
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...

coopster

9:57 pm on Jan 19, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You can use a variable if you want, simply change the content.html to a .php script and print the variable there. Also, you only have to close PHP (?>) and reopen it (<?php) when you are actually parsing PHP. You can process multiple lines at once, then exit PHP parsing mode:

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

Then, in content.php, update your HTML to make your paragraph print out your variable text...
content.php

<div>
<!-- a bunch more html -->
<p>
<?php
// Start PHP parsing and pring my variable text out...
print $my_variable_text;
</p>
?>

Hapless

10:10 pm on Jan 19, 2004 (gmt 0)

10+ Year Member



Thank you very much, it works exactly like I wanted it to. I'm glad someone has finally had the answer after so many days...

Hapless

2:11 am on Jan 20, 2004 (gmt 0)

10+ Year Member



Also, is there any way to get around using HTML entities for ' (apostrophes)?

coopster

1:50 pm on Jan 20, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Not sure we follow you on this yet. Can you offer more detail? Are you referring to a feature in PHP called Magic Quotes [webmasterworld.com]?

Hapless

2:06 pm on Jan 20, 2004 (gmt 0)

10+ Year Member



Ok well say I have something like this...

$my_variable_text = 'I love teacher work days, don't you?'; 

Are there any other easier ways to get around using \\ to skip the single quote (or apostrophe, however you want to put it)?

BitBanger

2:35 pm on Jan 20, 2004 (gmt 0)

10+ Year Member



If your string has an embedded single quote, then use double quotes to contain the entire string. If you have embedded double quotes, then use single quotes as the container. If you have both types of quotes embedded, then you will need to escape the alternate style.

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.

coopster

2:54 pm on Jan 20, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



There is also a heredoc syntax [php.net]. There are a few different ways to specify PHP String [php.net] literals.

Hapless

3:42 pm on Jan 20, 2004 (gmt 0)

10+ Year Member



For some reason the heredoc syntax didn't seem to work, but after reading it again, I should probably check it later. But I've got to stop working on this for now anyway.