PHP Help

death180

Seasoned Veteran
Joined
Jul 3, 2009
Messages
4,338
Reaction score
0
FP$
1,767
How can I make it so I link to php files in my HTML document. Let me explain.

Say I wanted to make a navigation bar, I would make it in a php file, and then using one tiny bit of code, link to it in my HTML file so as I updated the php file it would automatically update it in my php file and any more files I put it in.

How would I do that?
 
Well imagine your navigation is stored in a file called nav.php, in your index.php simply add

Code:
<?php include('nav.php'); ?>

or

Code:
<?php require('nav.php'); ?>

Where you want the menu to be displayed.
 
Create a new file in notepad and paste your html into it.

Above the html place <?php and ?>

Should have something like this:
Code:
<?php
/**********************************
 * Place any required php functions in this section
*********************************/
?>
<! --place your html in this section-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html> <! --place your html in this section-->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Page Title Here </title>
<meta name="Keywords" content="list keywords here">
<meta name="Description" content="description here">
<link href="default.css" rel="stylesheet" type="text/css"> <!-- location of stylesheet -->
<script type="text/javascript">
/***********************************************
* Any page Javascript goes here
***********************************************/
</script>
</head>
<body>
<div id="wrapper">
<!-- start header -->
<div id="header">
    <div id="menu">
           </div>
</div>
<!-- end header -->

<!-- start page -->
<div id="page">
    <!-- start content -->
    <div id="content">
    
    </div>
</div>
<!-- end footer -->
</div>
</body>
</html>

Locate the div that contains the menu. Copy and paste the menu code into a new document. Save it as menu.php

Now in the first file remove the contents of the div that contains the menu (leave the div tags) and replace with:

Code:
<? include_once('menu.php'); ?>

Now save the file as index.php and you will have it. To add more links to the menu now all you have to do is edit the menu.php and it will update all the pages that are using the include.

Be sure to rename index.html or the browser will read it instead of the new index.php
 
Thanks a bunch, I will test that out when I have some free time. Both of you guys thanks, you will both be receiving +rep when I can give it.
 
This doesn't work. It doesn't display menu.php or header.php or any other ones.
 
Iron Man said:
This doesn't work. It doesn't display menu.php or header.php or any other ones.

What do the files that you're including contain? <?php include("file.php"); ?> should be all you need.
 
index.php has things like
Code:
<? include_once('menu.php'); ?>

Inside the menu div tags. And of course menu.php changes for each one, to the corresponding file.

In menu.php I have:
Code:
				<a href="index.html">Home</a> |
				<a href="about.html">About</a> |
				<a href="templates.html">Templates</a> |
				<a href="portfolio.html">Portfolio</a> |
				<a href="community.html">Community</a> |
				<a href="contact.html">Contact Us</a>

That is the code I am not sure about.
 
Try using
Code:
<?php include("menu.php"); ?>
instead. Is it giving you any error messages right now?
 
no it just isn't showing the menu and header and stuff.

EDIT: I tried what you posted and that didn't work.

Here is my index.php:

Code:
<?php
?>
<!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=UTF-8" />
		<meta name="author" content="Alex J." />
		<meta name="copyright" content="Alex J." />
		<title>Micro Studios</title>
		<link rel="shortcut icon" href="images/favicon.ico">
		<link rel="stylesheet" type="text/css" href="style.css" />
	</head>
	<body>
		<div id="container">
			<!-- Begin Header -->
				<div id="header">
<? include_once('header.php'); ?>
				</div>
		<!-- Begin Menu -->
			<div id="menu">
<? include("menu.php"); ?>
			</div>
			<!-- Begin Content -->
			<div id="content">
				<!-- Welcome Block -->
				<fieldset class="welcome">
					<legend>Welcome</legend>
						Welcome to the Micro Studios official website! Here is where you can get all the information on Micro Studios or download our templates.<br />
						This site is best viewed in <a href="http://www.mozilla.com/en-US/firefox/personal.html">Mozilla FireFox</a> and <a href="http://www.google.com/chrome">Google Chrome</a>.
				</fieldset>
				<!-- News Block -->
				<fieldset class="news">
					<legend>Latest News</legend>
						<strong>~ Tuesday, September 14, 2010:</strong> Site Goes Live.<br />
						<strong>~ Wednesday, September 15, 2010:</strong> Blog added.<br />
						<strong>~ Thursday, September 16, 2010:</strong> Footer navigation added. Access the blog via this navigation.<br />
						<strong>~ Monday, September 20, 2010:</strong> Major content pack #1. View full details <a href="#">here</a>
				</fieldset>
				<!-- Featured Site Block -->
                <fieldset id="featuredsite">
					<legend>Featured Site #2</legend>
				<p><h3>The Hazey</h3></p>
				<p>Welcome to TheHazey.co.uk, the first forum hybrid. We have combined graphics, forum services, forum, website and general discussion in to one amazing website. We are an active site, with one aim to be the best!.</p><br />
<a href="http://forumpromotion.net"><img src="images/featuredsite_preview.png"></a><br />
<p class="newsbar2">If you would like to be the next featured site send us a message via the Contact Us page.</p>
<div id="table"
   <table class="table1" cellspacing="1">
   <thead>
   <tr>
      <th class="name" colspan="4"><span style="font-weight: bold;">Table Advertisements</span></th> 
   </tr>
   </thead>
   <tbody>
	<tr class="bg1">
      <td align="center" width="25%"><a href="#" target="_blank"><img src="images/table_ad.png" border="0" width="88" height="31" alt="The Ethereal Nation Online"></a></td>
      <td align="center" width="25%"><a href="#" target="_blank"><img src="images/table_ad.png" border="0" width="88" height="31" alt="Mega Victory"></a></td>
      <td align="center" width="25%"><a href="#" target="_blank"><img src="images/table_ad.png" border="0" width="88" height="31" alt="John Casey"></a></td>
      <td align="center" width="25%"><a href="#" target="_blank"><img src="images/table_ad.png" border="0" width="88" height="31" alt="Xbotica"></a></td>
	</tr>
	<tr class="bg1">
      <td align="center">You can get your ad description here, it can say anything that we will allow. It must be less than 140 characters.</td>
      <td align="center">You can get your ad description here, it can say anything that we will allow. It must be less than 140 characters.</td>
      <td align="center">You can get your ad description here, it can say anything that we will allow. It must be less than 140 characters.</td>
      <td align="center">You can get your ad description here, it can say anything that we will allow. It must be less than 140 characters.</td>
	</tr>
	<tr class="bg1"><td class="lastrow" colspan="4" align="center">Want your ad placed here for free? <a href="contact.html#tips"><u>Click here</u></a> for more information.</td></tr>
   </tbody>
   </table>
</div>
				</fieldset>
			</div>
			<!-- Begin bnav -->
			<div id="bnav">
			<a href="index.html">Index</a> | <a href="about.html">About Us</a> | <a href="templates.html">Templates</a> | <a href="portfolio.html">Portfolio</a> | <a href="community.html">Community</a> | <a href="/miniblog">Blog</a> | <a href="contact.html">Contact Us</a> | <a href="licensing.html">Licensing Details</a>
			</div>
			<!-- Begin Footer -->
			<div id="bottom">
			<a href="http://www.mozilla.com/en-US/firefox/personal.html"><img src="images/social/firefox.png"></a> <a href="http://www.google.com/chrome"><img src="images/social/chrome.png"></a><br />
			<a href="http://twitter.com/microstudios"><img src="images/social/twitter.png"></a> <a href="http://www.facebook.com/pages/Micro-Studios/110066179054521"><img src="images/social/FaceBook.png"></a><br />
			<a href="contact.html"><img src="images/social/Contact.png"></a> <a href="http://google.com"><img src="images/social/google.png"></a>
			</div>
			<div id="footer">
			</div>
		</div>
			<div id="footer">
<? include_once('footer.php'); ?>
			</div>
<!-- twitter follow badge by go2web20 -->
<script src='http://www.go2web20.net/twitterfollowbadge/1.0/badge.js' type='text/javascript'></script><script type='text/javascript' charset='utf-8'><!--
tfb.account = 'microstudios';
tfb.label = 'follow-us';
tfb.color = '#81bfed';
tfb.side = 'l';
tfb.top = 200;
tfb.showbadge();
--></script>
<!-- end of twitter follow badge -->
	</body>
</html>
 
Did you make a separate directory for the include files, or is everything in the same directory?
 
Try this code instead:

Code:
<!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=UTF-8" />
          <meta name="author" content="Alex J." />
          <meta name="copyright" content="Alex J." />
          <title>Micro Studios</title>
          <link rel="shortcut icon" href="images/favicon.ico">
          <link rel="stylesheet" type="text/css" href="style.css" />
       </head>
       <body>
          <div id="container">
             <!-- Begin Header -->
                <div id="header">
    <?php include "header.php" ; ?>
                </div>
          <!-- Begin Menu -->
             <div id="menu">
    <?php include "menu.php"; ?>
             </div>
             <!-- Begin Content -->
             <div id="content">
                <!-- Welcome Block -->
                <fieldset class="welcome">
                   <legend>Welcome</legend>
                      Welcome to the Micro Studios official website! Here is where you can get all the information on Micro Studios or download our templates.<br />
                      This site is best viewed in <a href="http://www.mozilla.com/en-US/firefox/personal.html">Mozilla FireFox</a> and <a href="http://www.google.com/chrome">Google Chrome</a>.
                </fieldset>
                <!-- News Block -->
                <fieldset class="news">
                   <legend>Latest News</legend>
                      <strong>~ Tuesday, September 14, 2010:</strong> Site Goes Live.<br />
                      <strong>~ Wednesday, September 15, 2010:</strong> Blog added.<br />
                      <strong>~ Thursday, September 16, 2010:</strong> Footer navigation added. Access the blog via this navigation.<br />
                      <strong>~ Monday, September 20, 2010:</strong> Major content pack #1. View full details <a href="#">here</a>
                </fieldset>
                <!-- Featured Site Block -->
                    <fieldset id="featuredsite">
                   <legend>Featured Site #2</legend>
                <p><h3>The Hazey</h3></p>
                <p>Welcome to TheHazey.co.uk, the first forum hybrid. We have combined graphics, forum services, forum, website and general discussion in to one amazing website. We are an active site, with one aim to be the best!.</p><br />
    <a href="http://forumpromotion.net"><img src="images/featuredsite_preview.png"></a><br />
    <p class="newsbar2">If you would like to be the next featured site send us a message via the Contact Us page.</p>
    <div id="table"
       <table class="table1" cellspacing="1">
       <thead>
       <tr>
          <th class="name" colspan="4"><span style="font-weight: bold;">Table Advertisements</span></th>
       </tr>
       </thead>
       <tbody>
       <tr class="bg1">
          <td align="center" width="25%"><a href="#" target="_blank"><img src="images/table_ad.png" border="0" width="88" height="31" alt="The Ethereal Nation Online"></a></td>
          <td align="center" width="25%"><a href="#" target="_blank"><img src="images/table_ad.png" border="0" width="88" height="31" alt="Mega Victory"></a></td>
          <td align="center" width="25%"><a href="#" target="_blank"><img src="images/table_ad.png" border="0" width="88" height="31" alt="John Casey"></a></td>
          <td align="center" width="25%"><a href="#" target="_blank"><img src="images/table_ad.png" border="0" width="88" height="31" alt="Xbotica"></a></td>
       </tr>
       <tr class="bg1">
          <td align="center">You can get your ad description here, it can say anything that we will allow. It must be less than 140 characters.</td>
          <td align="center">You can get your ad description here, it can say anything that we will allow. It must be less than 140 characters.</td>
          <td align="center">You can get your ad description here, it can say anything that we will allow. It must be less than 140 characters.</td>
          <td align="center">You can get your ad description here, it can say anything that we will allow. It must be less than 140 characters.</td>
       </tr>
       <tr class="bg1"><td class="lastrow" colspan="4" align="center">Want your ad placed here for free? <a href="contact.html#tips"><u>Click here</u></a> for more information.</td></tr>
       </tbody>
       </table>
    </div>
                </fieldset>
             </div>
             <!-- Begin bnav -->
             <div id="bnav">
             <a href="index.html">Index</a> | <a href="about.html">About Us</a> | <a href="templates.html">Templates</a> | <a href="portfolio.html">Portfolio</a> | <a href="community.html">Community</a> | <a href="/miniblog">Blog</a> | <a href="contact.html">Contact Us</a> | <a href="licensing.html">Licensing Details</a>
             </div>
             <!-- Begin Footer -->
             <div id="bottom">
             <a href="http://www.mozilla.com/en-US/firefox/personal.html"><img src="images/social/firefox.png"></a> <a href="http://www.google.com/chrome"><img src="images/social/chrome.png"></a><br />
             <a href="http://twitter.com/microstudios"><img src="images/social/twitter.png"></a> <a href="http://www.facebook.com/pages/Micro-Studios/110066179054521"><img src="images/social/FaceBook.png"></a><br />
             <a href="contact.html"><img src="images/social/Contact.png"></a> <a href="http://google.com"><img src="images/social/google.png"></a>
             </div>
             <div id="footer">
             </div>
          </div>
             <div id="footer">
    <?php include "footer.php"; ?>
             </div>
    <!-- twitter follow badge by go2web20 -->
    <script src='http://www.go2web20.net/twitterfollowbadge/1.0/badge.js' type='text/javascript'></script><script type='text/javascript' charset='utf-8'><!--
    tfb.account = 'microstudios';
    tfb.label = 'follow-us';
    tfb.color = '#81bfed';
    tfb.side = 'l';
    tfb.top = 200;
    tfb.showbadge();
    --></script>
    <!-- end of twitter follow badge -->
       </body>
    </html>

I changed the syntax on the include statements a bit, it might help.
 
On your include statements, I changed

Code:
<? include('file.php'); ?>

to

Code:
<?php include "file.php"; ?>

Slightly different syntax, I'm surprised that was the issue. I'm glad it works for you now though. :cheer:
 
Yes I am too, this will vastly improve my website. +rep for the help also
 
Back
Top Bottom