Jump to content

Recommended Posts

Posted

Hi everyone,

 

Got another PHP coding question for y'all.

 

In trying to modularize my web site, I decided to create a "header.php" page that contains all the code common at the top of each of my web pages and use the php include() command to add the header to each web page. The header.php file contains the standard protocol for each web page (i.e., <!doctype>,<head>,<title>, and <meta> tags). In fact, here is the code for my header page.

><?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">"; ?>
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-Type" content="text/html; charset=windows-1252" />
<meta http-equiv="content-Language" content="en-us" />
<meta http-equiv="imagetoolbar" content="no" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="expires" content="-1" />
<meta name="copyright" content="Copyright 2004, Kasey's Creations" />
<meta name="mytopic" content="Kasey's Creations" />
<meta name="robots" content="noimageclick,noimageindex,nocache,noarchive" />
<meta name="description" content="Kasey's Creations web site graphics for your personal and small business web sites." />
<meta name="keywords" content="kasey's creations, Kasey's Creations, kaseyscreations, KaseysCreations, Kaseys Creations, kaseys creations" />

<title></title>

<!-- StyleSheet -->
<link rel="stylesheet" href="css/interface3.css" type="text/css" />
</head>

<body class="background">
<div id="container_page">
 <div id="image_header">
	 <img src="graphics/Interface3_Header.gif" width="753 px" height="208px" alt="Page Header" />
 </div>
 <div id="contactbutton">
	 <a href="" onmouseover="mouseover(0)" onmouseout="mouseout(0)">
	 <img src="graphics/Interface3_ContactButton_up.gif" width="123px" height="18px" alt="Contact Us" id="Interface3_Contactbutton" name="Interface3_ContactButton" /></a>
 </div>
 <div id="privacybutton">
	 <a href="privacypolicy.php" onmouseover="mouseover(1)" onmouseout="mouseout(1)">
	 <img src="graphics/Interface3_PrivacyButton_up.gif" width="123px" height="18px" alt="Privacy Policy" id="Interface3_PrivacyButton" name="Interface3_PrivacyButton" /></a>
 </div>
 <div id="date-time" class="smalltext">
	 <?php
   $hour = date(g)-3;
   if ( $hour<=0 )
  	 $hour += 12;
   echo( "$hour:".date(i)." ".date(a) );
   echo("   ::  &nbsp");
   echo( date("d-M-Y") );
	 ?>
 </div>

 

You'll notice that my header file contains some of the body content, including buttons for "contact us", "privacy policy", the date and time, etc.

 

You'll also notice that the <title> block of the header is blank. In order to make the header file truly modular, I want to be able to pass a unique page <title> for each page I use it on.

 

This header file is called from each web page in my site using the php include() command as the very first line of the page file, as seen here:

><?php include("header.php"); ?>
<div id="hansel-gretel" class="smalltext">
	 You are here:  Home
</div>
<div id="menu" class="smalltext" align="right" style="position:absolute; left:635px; top: 150px; visibility:visible; z-index:1;"> 	 Services<br />Pricing<br />Portfolio<br />Privacy Policy<br />Terms &
	 Conditions<br />Members/Clients<br />Contact Us<br />About Us<br />
 </div>
.
.
.

 

I have no problem getting the scripts to run correctly when I use them in this manner. When I try to pass the page title (from index.php to header.php) as a variable, however, the header does not execute and any page content directly coded within the page header (including my css file) does not load. So, for example, if header.php is "included" as the first line in index.php, only the content in index.php will load into the browser.

 

Here is the code I was trying to use to insert a unique page title for each page.

 

In index.php:

><?php include("header.php?mytitle=ThisIsThePageTitle"); ?>

 

In header.php:

>.
.
.
<title>
<?php
 $thetitle = $_GET['mytitle'];
 echo("$thetitle");
?>
</title>
.
.
.

 

I need a fresh set of eyes to look at this. I'm so cross-eyed now that I honestly don't see what I'm doing wrong.

 

Thanks, in advance, to anyone who takes some time out of their day to help me!

 

Good luck...it seems like a needle in a haystack to me!

Kasey

Posted

Well, I dont' have much time, but here's my initial thought.

 

Assign the variable before the header. Since the code will treat the included statement as if it is part of the original page, just set the variable right above the include instead of trying to pass it in. The code inside your header will still see it and make the change.

 

Hopefully someone can clarify that or I can elaborate after work tonight if I can get time.

 

- Vendlus

Posted

Hi Vendlus,

 

Well, your suggestion worked. :)

 

Still, I'd like to know what's going on with passing the variable and why the header won't load if I try to pass the title as a variable.

 

I'm very new to PHP, but I'm trying to learn all I can, so when something doesn't make sense, I really want to know why. ;)

 

If you, or anyone else out there who may be "listening" can provide some explanation and/or commentary, I'd really appreciate it.

 

Thanks a bunch!

Kasey

Posted (edited)

Kasey, it doesn't work because the include() function does not read the file you're including by using an HTTP GET command and the $_GET[] array only contains variables that are sent to the server through an HTTP GET request - the "index.php?variable=something" stuff.

 

To put it in another way, that would only work if you could do the equivalent of typing the address in your browser location box. Since PHP's include() reads the included file directly from the filesystem, I'd say your only solution is the one Vendlus described.

 

 

And keep that "if it doesn't work, I want to know why" spirit. I think that's one of the most important things someone can do in order to learn about something! ;)

 

If you need any help, just ask :)

Edited by TCH-Raul
Posted

I just don't indlude the title and meta tags in my header file. I leave them as part of the the main file and start by header just below these items. Simple...but it works with minimal work and then my keywords, descriptions and titles are all unique to each page, if I choose them to be!! :)

Posted
Hi Vendlus,

 

Well, your suggestion worked. ^_^

Glad to hear it. And like Raul said, keep that attitude of wanting to know why things do or don't work. It'll serve you well. :unsure:

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...