I decided to build a booking calendar and embed it to my website. But I don't know how to include it in the html file. I tried the <?php ?> tag but it doesn't display at all. I looked around and found this htaccess file thing. How do I generate it? Is it a way to use my php code without this?
Here is what included in my big html file (I did not include other html here)
<!DOCTYPE html>
<hmtl>
<head>
</head>
<body>
<?php
include 'calendar.php';
$calendar = new Calendar();
echo $calendar->show();
?>
</body>
</html>
I used sublime text edit and there is no "color" on the key words meaning it is not recognizing the php tag.
Place the php code of the calendar in a file and save it as say, calendar.php and then add the following line in the pages where you want the calendar to appear in:
<?php include("calendar.php"); ?>
N.B. Make sure to replace the file extension of all your html files with .php extension instead of the .html extension.
For example: index.html should be index.php
Related
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.
so here is a question that I shouldn't be having so much trouble researching, but I am. Basically I want to create a webpage that loads in a header and a side bar. The header is it's own file header.php and the sidebar is leftBar.php. The following code is my index page, yet I am failing to have these pages loaded. I believe it has something to do with a lacking css page. But I have not found anything useful to help me solve this problem. What I would like to do is have the leftBar.php display its text on the left side of the page and the header.php file at the top. Below is the linked pages.
index.php
<html>
<head>
<title>junk</title>
</head>
<body>
junk
<?php
include ('styles/header.php');
include ('styles/leftBar.php');
?>
</body>
</html>
leftBar.php
<html>
left
</html>
header.php
<html>
header
</html>
In your include files, just place the code snippet that you want to appear on the page where it's included. You certainly don't want extra <html> elements (etc.) included at various places on the page.
I have a similar setup myself for the menu bar of a test site.
I think you have unnecessary brackets around your include file paths.
index.php:
<?php
include 'menu.php';
echo "$MENU";
?>
menu.php:
<?php
$MENU = '<table class="mainmenu"><tr><td>Home</td></tr><tr><td>Useful-Sites</td></tr></table>';
?>
You don't need to declare html tag for header.php and leftBar.php. A php file should start with <?php and end with ?>. Try learning php first http://w3schools.com/php/default.asp.
trouble researching?
first page of google:
http://www.w3schools.com/php/php_includes.asp
http://www.tizag.com/phpT/include.php
http://php.about.com/od/tutorials/ht/template_site.htm
http://www.tutorialspoint.com/php/php_file_inclusion.htm
Please look at these pages, and then update your question if you have to.
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.
My problem is that the webpage that I do is in complete PHP files. Everywhere there is tutorial how to generate, but there is no instructions what to do when it is a PHP file.
The main file is also index.php. When I insert the code that is generated in HTML.
Comes with a lot of errors, so I convert it to PHP.
it will start like
echo "..bla bla" "\n" ;
echo "<div property=\"gr:legalName\" content=\"G & Sziladi\"></div>\n";
which is good already, google recognize it, BUT this will show on the top of my page.How can i insert this markup into index.php without showing,but also google has to recognize it properly.
If you prefer to copy and paste HTML, the paste it into a PHP file but outside of the PHP tags:
<?php
/*
* May brand new PHP Website
*/
$title_prefix = '- Escaping the Tags';
?>
<html>
<head>
<title>Supersite Deluxe <?php echo $title_prefix; ?></title>
</head>
<body>
....
</body>
</html>
That is especially useful if you have to a lot of HTML, because you can just paste it in.
See Escaping from HTML
I designed an order form using pForm. It is an HTML/CSS automated code. But now I think, it should be a PHP file because I need to add PHP code into it.
If I add just when the HTML file starts and at the end, probably HTML will stop working.
How to change this file to a PHP file and also preserve its present look and feel?
You can literally change the file extension to ".php".
Then you can pepper your new php file with php code like so:
<html>
<head></head>
<body>
<?php
echo 'Hello world!<br />' ;
?>
</body>
</html>
just change the extension to php
and you can add any PHP code using tags. this doesnt need to be in the top but in any place of the code :)