There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
black screen blue screen boot bsod computer connection crash css dell display driver drivers email error explorer firefox firefox 3 hard drive internet internet explorer itunes laptop lcd malware monitor network networking nvidia outlook outlook 2003 outlook express password printer problem problems ram router security slow software sound sprtcmd.exe trojan usb video virus vista windows windows xp wireless
Web Design & Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
Solved: PHP Newbie questions


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
skyhigh007's Avatar
Senior Member with 393 posts.
 
Join Date: Jun 2004
25-Feb-2008, 12:04 PM #1
Solved: PHP Newbie questions
Hi

Here's my situation, I have created a website using Xhtml & CSS, the problem is that the header, footer and other constant contents are re-loaded in very page that I have created and I think that will slow the loading speed of the pages. So, I decide to try php where you can re-use the header, footer in every page and I don't have to go edit in every page. My question is which function in PHP should I use to re-use the header, footer , etc ? Include, require, include-once, require once ?

My files are: header.php, footer.php, ...etc

In index.php

<?php include( "header.php") ?>

middle content

<?php include("footer.php") ?>


is set up correct?


Thanks
Sequal7's Avatar
Computer Specs
Distinguished Member with 2,369 posts.
 
Join Date: Apr 2001
Location: Around the corner!
Experience: Including today?
25-Feb-2008, 01:01 PM #2
It would be include, but don't forget the closing ;
Code:
<?php include(header.php); ?>
You could also use require, they perform the same function, just handle errors differently.
TheRobatron's Avatar
Computer Specs
Senior Member with 480 posts.
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
25-Feb-2008, 03:04 PM #3
I always use
PHP Code:
<?php include "file.txt" ?>
because
PHP Code:
<?php include(file.txt?>
never works for me...
MMJ's Avatar
MMJ MMJ is offline
Distinguished Member with 3,252 posts.
 
Join Date: Oct 2006
25-Feb-2008, 03:11 PM #4
Quote:
Originally Posted by Sequal7 View Post
It would be include, but don't forget the closing ;
Although thats good practice it isn't really necessary since you close the PHP block right after it.
skyhigh007's Avatar
Senior Member with 393 posts.
 
Join Date: Jun 2004
25-Feb-2008, 04:31 PM #5
I have another question.

Do I have to put the following code in every php page, even in the header.php and footer.php?
Or I just have to put it in the individual main page?

"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>"
TheRobatron's Avatar
Computer Specs
Senior Member with 480 posts.
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
25-Feb-2008, 04:38 PM #6
No, the include command inputs the text direclty into the document, so it's as if you actually wrote it in. Because PHP is server-side, the browser doesn't see any of the PHP and so just the text in the file. You still need to put in formatting tags though (<p>), unless you wrap them round the PHP on every page:
HTML Code:
<p> <?php include "file.php" ?> </p>
__________________
There's no place like 127.0.0.1

skyhigh007's Avatar
Senior Member with 393 posts.
 
Join Date: Jun 2004
25-Feb-2008, 05:02 PM #7
This is my footer.php

<div id="footer">
<ul id="foot">
<li><a href="index.php">Home</a></li>
<li><a href="shipReturn.php">Shipping&amp;Return</a></li>
<li><a href="privacy.php">Privacy Policy</a></li>
<li><a href="contact.php">Contact Us </a></li>
<li><a href="news.php">News</a></li>
<li><a href="#">Site Map</a></li>
</ul>
<p class="copy"> Copyright &copy; 2007. All Rights Reserved </p>
</div>

Is this standard or I need to put some <?php ?> tag around the whole cde?
TheRobatron's Avatar
Computer Specs
Senior Member with 480 posts.
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
25-Feb-2008, 05:14 PM #8
No, just write it exactly as if you were writing it directly into the HTML file. Remember, everything that is in the file will be put into the HTML file.
skyhigh007's Avatar
Senior Member with 393 posts.
 
Join Date: Jun 2004
25-Feb-2008, 06:01 PM #9
Would you explain it more detail by giving examples that I use in the above code?
Sequal7's Avatar
Computer Specs
Distinguished Member with 2,369 posts.
 
Join Date: Apr 2001
Location: Around the corner!
Experience: Including today?
26-Feb-2008, 01:04 AM #10
There are several ways of doing it, if you dont want to write the code for each page and don't mind a static page title, then redo your header like this:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Website</title>
</head>
Name that header.php

And redo your footer like this;
Code:
<div id="footer">
<ul id="foot">
<li><a href="index.php">Home</a></li>
<li><a href="shipReturn.php">Shipping&amp;Return</a></li>
<li><a href="privacy.php">Privacy Policy</a></li>
<li><a href="contact.php">Contact Us </a></li>
<li><a href="news.php">News</a></li>
<li><a href="#">Site Map</a></li>
</ul>
<p class="copy"> Copyright &copy; 2007. All Rights Reserved </p>
</div></body>
</html>
Name that footer.php

then your pages can look like this;
Code:
<?php include(header.php); ?>
<div class body>
<ul>
  <li>Bulleted list</li>
  <li>Bullett two</li>
</ul>
<ol>
  <li>Numbered lists</li>
  <li>second number</li>
  <li>third</li>
</ol>
<h3>Header 3</h3>
<h2>Header 2  </h2>
<h1>Header 1 </h1></div>
<?php include(footer.php); ?>
and the result would be;
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<div class body>
<ul>
  <li>Bulleted list</li>
  <li>Bullett two</li>
</ul>
<ol>
  <li>Numbered lists</li>
  <li>second number</li>
  <li>third</li>
</ol>
<h3>Header 3</h3>
<h2>Header 2  </h2>
<h1>Header 1 </h1></div>
<div id="footer">
<ul id="foot">
<li><a href="index.php">Home</a></li>
<li><a href="shipReturn.php">Shipping&amp;Return</a></li>
<li><a href="privacy.php">Privacy Policy</a></li>
<li><a href="contact.php">Contact Us </a></li>
<li><a href="news.php">News</a></li>
<li><a href="#">Site Map</a></li>
</ul>
<p class="copy"> Copyright &copy; 2007. All Rights Reserved </p>
</div></body>
</html>
Don't forget to define your div body, this is just an example. If you don't define it in css then just add <body> at the very first line to each page taht is including the head page.
__________________
Good Luck on your fix

My real hobby..JoyCo
My real Job..(Second Hobby) IAFF Local 1865
Like the sites? My hobby is the one that created them!
skyhigh007's Avatar
Senior Member with 393 posts.
 
Join Date: Jun 2004
26-Feb-2008, 12:19 PM #11
Hi

Thank you for the example. What do you mean when you said "if you dont want to write the code for each page and don't mind a static page title" ?
Sequal7's Avatar
Computer Specs
Distinguished Member with 2,369 posts.
 
Join Date: Apr 2001
Location: Around the corner!
Experience: Including today?
27-Feb-2008, 02:34 PM #12
The page title would remain the same if you add the
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Website</title>
</head>
Look at the page title here, it says "php Newby Question - Tech Support Guy Forums", then go to the forums, it says Tech Support Guy - Free help for ....."

You could define the page title displayed in the header page, with a php variable but you would need to define that on each page, adding something like;
Code:
<?
$subtitle = "your page title here"; //now include the header.php page
include('header.php');
?>
NOTE: Each page you are writing (other than the header and footer.php pages) will need this on it at the top. Change the "your page title here" to the page title you want on that particular page.

Then change your header page to this;
Code:
if (isset($subtitle)) {
   echo $subtitle;
} else {
   echo "default subtitle"; //change to a  default name if an error is discovered
}
Then change your title content in the header.php page to this;
Code:
<title>
   MAIN TITLE &raquo; <? if (isset($subtitle)) { echo $subtitle; } ?>
</title>
Full example of header.php
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?if (isset($subtitle)) {
   echo $subtitle;
} else {
   echo "default subtitle";
}?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
   Page Title &raquo; <? if (isset($subtitle)) { echo $subtitle; } ?>
</title>
</head>
This should give you defined page titles.
__________________
Good Luck on your fix

My real hobby..JoyCo
My real Job..(Second Hobby) IAFF Local 1865
Like the sites? My hobby is the one that created them!
tomdkat's Avatar
Computer Specs
Distinguished Member with 3,716 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
27-Feb-2008, 03:05 PM #13
Quote:
Originally Posted by skyhigh007 View Post
the problem is that the header, footer and other constant contents are re-loaded in very page that I have created and I think that will slow the loading speed of the pages.
How so?

Peace...
rick22's Avatar
Junior Member with 21 posts.
 
Join Date: Feb 2008
28-Feb-2008, 09:21 AM #14
well, all of this just went up my head.... didnt understand even a single thing.. :P
skyhigh007's Avatar
Senior Member with 393 posts.
 
Join Date: Jun 2004
28-Feb-2008, 04:15 PM #15
Thanks for the example again. How do i or where should i declare a variable that can be used for many pages? Does it has to be on an individual page that is for global variables only ?

I hope all this reusing or sharing will speed up the loading process.
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 11:12 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.