Domanda

Ok so even I'm not too sure about what I'm asking, this is a whole part of web development I'm yet to look into.
I want to have a static header/footer with dynamic page content. I.E my header and footer stay constant throughout every page of my website, whereby the links to different pages on my website can contain pure content (no header or footer!) for easy editing?

Correct me if I'm wrong but I think I can do this with php includes. Am I correct and is there any other better way?

Cheers :)

È stato utile?

Soluzione

PHP includes are pretty much the standard way, yep.

include "header.php";
echo "Hello World"; 
include "footer.php"; 

(See php.net's manual on include, alternatively use require)

Note that your header / footer don't actually have to stay constant on every page, you could do checks for variables set beforehand or checks for what page the user is on.

$page = "contact";
include "header.php"; 
echo "Contact page stuff";
include "footer.php";

and in header, you could have "contact" in your navigation bolded or something if ($page == 'contact'. There's certainly other ways to do this (such as javascript or $_SERVER variables), but I use $page for other things so I like doing it that way.

Altri suggerimenti

Yes, you can do that including, for example, a header.php and a footer.php at the beginning and end of your dynamic page.

If you want to explore some solutions better solutions like templating, I recommand you using something like Smarty : http://www.smarty.net/
See the doc. You can do things like foreach without including <?php ?> tags, and many others.

Basic site structure with the use of the include() PHP function.

Site structure could look something like the following and could contain:

index.php

<!DOCTYPE html>
<head>
<title>Page title</title> // this could contain <?php include 'title.php'; ?>
</head>

<body>

<?php include 'header.php'; ?>

<?php include 'profile_content.php'; ?> // <-- contains your easy editing part

<?php include 'footer.php'; ?>

</body>

</html>

header.php

Can contain your navigation menu, image header etc.

<div id="header" align="center">
<img src=\"/images/header.jpg border='0' \">
<p>
<a href="index.php">Home</a> - 
<a href="profile.php">Profile</a> - 
<a href="links.php">Links
</a>- <a href="contact.php">Contact</a>
</p>

footer.php

Can contain your navigation menu including copyright information, author's name etc.

<div id="footer" align="center">
<p>
<a href="index.php">Home</a> - 
<a href="profile.php">Profile</a> - 
<a href="links.php">Links
</a>- <a href="contact.php">Contact</a>
© Copyright Your name here <?php include 'date.php'; ?>
</p>
</div>

The date can also be dynamically changed, using an include tag for date.php for instance, using the date() PHP function.

  • Visit PHP.net and you can browse through their Web site for more information.


Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top