I see multiple answers about creating php files in order to reuse headers/footers, but nothing specific enough. I can't seem to get it to work.
What exactly would the php file look like and what exactly would my html file look like (given the code as it currently is, below)? do I have to convert all my html files to php files in order to use the php include line?
<div id="footer2-wrap">
<div class="container">
<table id="header">
<tr>
<td class="phone-number"><span class='wsite-text wsite-phone'>Copyright 2015 | xxx Corporation</span></td>
</tr>
</table>
</div>
Create a new file with your footer data. Let's give it the name: footer.php:
<div id="footer2-wrap">
<div class="container">copyright etc...</div>
<div>
Then inside your master template (or index.php file), include the footer file:
include_once('footer.php');
You should work with include(), require() or require_once functions in PHP to include your files, which depends on your situation.
For instance, let's assume you have a basic php file, called index.php and you want to add sidebar.php, footer.php, navigation.php.
index.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<?php
include("sidebar.php");
include("navigation.php");
include("footer.php"); // we include footer.php here. you can use .html extension, too.
?>
</body>
</html>
footer.php (or html)
About us
Our work
Testimonials
What we do
Contact us
Yes, you must have a .php file to use PHP, your server must also have PHP installed (but most already do). PHP also adds to the loading time of a page, so take that into consideration when using it. To add the footer with PHP, you can use the PHP function include(), or, I am not sure if this is considered correct, with file-get-contents():
include():
<?php
include("footer.html");
?>
file-get-contents():
<?php
$footer = file_get_contents('footer.html');
echo $footer;
?>
You could also do the same thing with JavaScript:
var xmlhttp, text;
xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', 'http://www.example.com/file.txt', false);
xmlhttp.send();
text = xmlhttp.responseText;
document.getElementById("footer").innerHTML=text;
Code taken from here.
Note: The file must be on the same domain to use JS.
First create a file called menu.php(if you use .html it wont work)
On that php file write the html code for your menu
<div id="navbar">
more code
</div>
At your main html file write
<?php
include("menu.php");
?>
The code should be able to run. Make sure you are running the code via Apache. Download Xammp or wammp. Start Apache, copy paste your project folder to the xammp folder under httdocs. Then on your browser type in localhost/[your project name] and make sure the html files are changed to .php.
Related
I am a young developper (15 year)
I have a problem white my website. My page does not synchronize with html and php in "localhost/sn/". Normally when I type "localhost/sn/" in my browser I should have the html page synchronized with the php. But it doesn't work. When I type that, I only have my index.php page.
my server is with XAMPP.
Please help me!!!!!
If you want to run your website with only html and css structures, you use the file with the extension html (e.x index.html) and of course you do not need to use XAMPP.
If you want to run your website and have php code then use the file with the extension php (e.x index.php) and more importantly you need to put it in the htdocs directory of XAMPP.
There are several things you can do.
First, PHP is a programming language, where you can mix PHP code and HTML output together. There are several reasons, why you shouldn't (so called spaghetti code, see here), but as you are just starting, this seems to be ok.
On the other hand, you can integrate your HTML into your PHP with require or include.
Of course you are not bount to either the one ore the other. You can mix the two approaches (and every serious PHP app does it)
Mixing HTML and PHP
<?php
// We are in PHP country now
// Set the value of variable
$header = "My 1st header";
// This ends PHP country
?>
<!-- Now we are in HTML country -->
<h1><?= $header =></h1>
<?php
// PHP country again
// the character string `<?&= some_value ?>;`
// is a shortcut for `<?php echo some_value; ?>`
$count = 4;
for ($i = 0; $i < $count; $i++) {
echo "<p>Paragraph</p>";
}
This will output <h1>My 1st header</h><p>Paragraph</p><p>Paragraph</p><p>Paragraph</p><p>Paragraph</p>
Include HTML in PHP
Here we have two files
index.php:
<?php
require "header.html";
echo "<p>Content</p>";
header.html
<h1>My 2nd header</h1>
Calling index.php in XAMPP will yield <h1>My 2nd header</h1><p>Content</p>
You only need 1 file {index.php},
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>title</title>
<link rel="stylesheet" href="style.css">
<?php
echo ("Hello Arthur!");
?>
</head>
<body>
<div id="top">
asdfasdfsd
<div id="under">
asdfsadf
</div>
</div>
<!-- page content -->
</body>
</html>
then, open Chrome or whatever browser you like, type:
localhost/index.php
make sure the {index.php} file is in htdocs folder and xampp Apache is turned on.
hope I helped.
if you want just show html page then u do not need xampp. If you want to show html page with some logic then it have to be index.php with template in html
Hye There I am new to web and want to include a .php file inside my .html file. This is my .html file:
<html>
<head>
<title>Includer Example</title>
</head>
<body>
<div style="height:100px; width:300px; border:#F00 thick; background-color:#F00">
THIS DIV IS IN HTML FILE
</div>
<?php
include 'seconddiv.php';
?>
</body>
</html>
and this is my php file:
<html>
<head>
<title>second div</title>
</head>
<body>
<div style="height:100px; width:300px; background-color:#FF0">
THIS DIV IS IN PHP FILE
</div>
</body>
</html>
I am using WampServer and totally new to Web Coding can somebody give any idea what I am doing so wrong please thanks in advance!
Rename your .html to .php, so the PHP processor processes it.
Following Patricks comment above:
Note that the (included) .php file by no means need to be a html document. It must contain only exactly what you want to be inserted into the 'main' html document.
The main file needs to be .php in order to run the PHP-processor on it.
index.php:
<html>
<body>
<div style="height:100px; width:300px; border:#F00 thick; background-color:#F00">THIS DIV IS IN HTML FILE</div>
<?php
include 'seconddiv.html';
?>
</body>
</html>
The included file can have any ending; as it is included into the .php file, it is processed by the PHP processor anyway.
seconddiv.html:
<div style="height:100px; width:300px; background-color:#FF0">
THIS DIV IS IN PHP FILE
</div>
convert file from HTML to .php.
Php code will only run in file with php.
PHP + HTML CODE === CAN RUN IN === .php file
PHP + HTML CODE === CANT RUN IN === .html file
By default PHP code is not executed in .html file, so apache does not send it to the PHP to execute it. If you rename the .html file to .php, it would be a simple solution.
Just change index.html to index.php.
Edit the .htaccess file
How? Well, here’s what you should do:
Go to your Document root or WWW root directory or folder; it commonly looks like this:
/home/akiko/public_html
Look for the file named .htaccess. If it’s not there, create a blank page using a regular text editor like Notepad and save the file as .htaccess – the file name includes that little dot in the front.
Now edit this file by adding the following lines:
RemoveHandler .html .htm
AddType application/x-httpd-php .php .html .htm
Save and close the .htaccess file. Upload it to your web server (to your Document/WWW root) and that’s it!
Sample PHP code in a .HTML webpage
Now create a test file and name it test.html
Copy the following HTML (containing PHP code) into it:
<html>
<head>
</head>
<body>
<h1>
<?php echo "I LOVE PHP!"; ?>
</h1>
</body>
</html>
Upload it to your web server and view it with your favourite browser. You will see that it works just fine.
Where is the right place to include a file when working with HTML and php?
Before the HTML code:
<?php include 'file.php' ?>
<html>
<head>
</head>
<body>
</body>
</html>
In the head tag:
<html>
<head>
<?php include 'file.php' ?>
</head>
<body>
</body>
</html>
In the body tag:
<html>
<head>
</head>
<body>
<?php include 'file.php' ?>
</body>
</html>
Include the file wherever it would otherwise be in the code...
Example:
- If the include is an html form, it would go in the body.
- If the include is a php script to process the form, it would probably go in the head.
If your imported file is just code with no characters outside the PHP blocks then it doesn't matter. I'd personally put it in the top of the file, so that I could use ini_set affecting the whole execution or send headers or cookies.
I you have content to be printed in the file's main code or outside PHP blocks you should put the file where you want the content.
Just noting, if you want keep the main HTML structure static in your main file and still want to print to both <body> and <head> I suggest you do both in functions, add the import to the file top and call the functions to print.
PHP doesn't care where you put it. For purposes of displaying your page, though, it depends on what is is the included file. For example, if file.php contains the body of your table, obviously it should go in the <body> tag.
It depends on your need.
If your file.php file has some global functions that you'd like to access throughout your code, then I would say include it at the top. Additionally, if you're doing anything with the headers in the included file, definitely include it at the top.
However, say your file.php contains a dynamic javascript code (in other words a script that is changed by php depending on the situation), then the header is probably the best location for it, since that is more or less the standard location to place javascript.
Finally, if your file.php is meant to bring in actual html or structure to the file, then definitely include it in the body.
I just learned how to include php .Here's the index or main php file
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php include 'header.php'; ?>
</body>
</html>
now in header.php file which way is better to print html
Way 1 directly use html without php
<header>
<h1>Header</h1>
</header>
Way 2 Using php and echo
<?php
echo '
<header>
<h1>Header</h1>
</header>
'
?>
Another quick question. Will it work if I use .html for the base or index file ??
sorry for my bad english
Directly use HTML without PHP:
<header>
<h1>Header</h1>
</header>
As for your second question:
The file you're using include() in must have .php extension, but the file that's being included doesn't necessarily need the .php extension. The .html extension would work fine as well.
Just include the PHP file.
BTW, in case you haven't, read about this too:
Difference between require, include and require_once?
HTH.
you should use include_once ;-)
http://us2.php.net/manual/en/function.include-once.php
There are a few ways to go around it. Rather than echoing everything, I like to go like this:
<title><?php echo $title; ?></title>
Or if I have a nice block to work with, maybe within an if statement, I also like to go like this :
<?php if($weather = "sunny") { ?>
<div id="sunny">
<p>It's a beautiful day outside.</p>
</div>
<?php } // end if($weather = "sunny")
else { ?>
<div id="sunny">
<p>Today is yucky.</p>
</div>
<?php } ?>
The answer is sometimes different. If you use the same header on a lot of pages, use Way1. If you're only doing this in one place, you want to use Way 2 (or keep it in html), since it's more readable at quick glance to someone studying the page.
You can use .html for the file if you change the server config to tell it to look at it for php first, but you shouldn't do that, it just increases complexity and may have unintended consequences if you do it wrong.
Use .php or .phtml for files with any amount php in it. The only distinction you can make is to use .phtml files for files that are mostly html.
I'm looking for ways to have my pages search for the page layout from an external template page. Please see the below example.
<head>
<title></title>
</head>
<body>
<search for header, css, layout, etc from external page>
Page contents
<search for footer>
</body>
Is there any way to do this using PHP or HTML? I want to be able to edit the layout for all the pages without having to do it page by page. I welcome any other means to achieve the same effect as long as it works on all the browsers.
Thank you very much!
This is exactly the sort of thing that PHP is for. A PHP script can include the contents of another script using the include statement.
So each page in your application could have an associated PHP script that generates the contents, and includes footer.php for the footer layout. In this way, when you change footer.php all the pages that use it will automatically get the changes.
You can't do this with pure HTML, though you could with some javascript and Ajax.
Like Andrew said, use includes. I'll set up 2 basic examples.
The simplest, have multiple layout files that are called by your main file(s):
header.php:
<div id="header">
Menu can go here.
<?php echo 'I make all my files .php, so they can use PHP functions if needed.'; ?>
</div>
footer.php
<div id="footer">
Footer Link
</div>
index.php
<html>
<head></head>
<body>
<?php include('/path/to/header.php'); ?>
Specific index.php content here.
<?php include('/path/to/footer.php'); ?>
</body>
</html>
The other option is to have one PHP file which includes all your different layout elements in functions. The reason I like this, is because you can include one file and then call specific functions for different parts. This can also be used to pass variables like a title of a page.
layout.php
<?php
function makeHeader($title) {
return 'My title is: '.$title;
}
function makeFooter() {
$html = '
<div id="footer">
Footer Link
</div>
';
return $html;
}
?>
index.php
<?php include('/path/to/include.php'); ?>
<html>
<head></head>
<body>
<?php echo makeHeader('Page Title'); ?>
Specific index.php content here.
<?php echo makeFooter(); ?>
</body>
</html>
Just make sure you use relative paths (no http://www.) when including files. This will allow variables and functions to transfer over smoothly. The easiest way to do this is using the PHP variable $_SERVER['DOCUMENT_ROOT'] so if you have a file http://mysite.com/includes/layout.php, you could include it with include($_SERVER['DOCUMENT_ROOT'].'/includes/layout.php') no matter where your file you are including from is located.