Reputation: 105
I would like to create a function for the common navigation links on my website. I thought I could create a php function and call it but I don't know how to set it up.
This is what I have:
<div class="left_side">
<ul>
<li><a href="page1.php">First Page</a></li>
<li><a href="page2.php">Second Page</a></li>
<li><a href="page3.php">Third Page</a></li>
</ul>
</div>
<div class="right_side">
<!-- contents of this page -->
</div>
Since the links on the left_side are the same for every page, how do I create a php function so that it can be called from several locations?
Ie. I would like it to look something like:
html:
<?php left_side() ?>
<div class="right_side">
<!-- content of this page -->
</div>
php:
function left_side()
{
echo
<div class="left_side">
echo
<ul>
echo
<li><a href="page1.php">First Page</a></li>
echo
<li><a href="page2.php">Second Page</a></li>
echo
<li><a href="page3.php">Third Page</a></li>
echo
</ul>
echo
</div>
}
Most of the server code is in php. So it would have to look something like this:
<?php
session_start()
require_once("php.php"); // the above php code with the left_side() function
function showPage()
{
include("html.html"); // the above html code
}
?>
Thanks.
Upvotes: 0
Views: 142
Reputation: 14949
Put your function in a file called function.php like this:
<?php
function left_side()
{
echo <div class="left_side">
echo <ul>
echo <li><a href="page1.php">First Page</a></li>
echo <li><a href="page2.php">Second Page</a></li>
echo <li><a href="page3.php">Third Page</a></li>
echo </ul>
echo </div>
}
?>
Then on each page, do this at the top somewhere:
<?php require_once('/path/to/function.php'); ?>
You can then use it with
<?php left_side() ?>
Generally, require() is better than include() because you will want it to throw an error if the file is missing (it will break your site not to have the HTML output). Also, require_once() is better than require() because the function only needs to be read one time by the webserver. If you start to include other files e.g. a standard header and footer in your page, which also call function.php, then require_once() will not run on those subsequent pages, leading to a slight performance gain.
Having said that, if you are just outputting HTML, you don't necessarily need a function wrapper at all. Just use echo statements in function.php (you can also just use HTML without echo) and use include() to have the stuff outputted every time you call it.
Upvotes: 0
Reputation: 1595
Work with include
.
Put the left side content in a file like :
//leftside.php
<div class="left_side">
<ul>
...
</ul>
</div>
and then you include this section with:
<?php include_once('leftside.php'); ?>
Upvotes: 1
Reputation: 1792
Make a header file, such as header.html. In that file:
<div class="left_side">
<ul>
<li><a href="page1.php">First Page</a></li>
</ul>
</div>
In your output page, you need to set the file extension to PHP (or do something more advanced to make the server know it needs to execute PHP), and include:
<?PHP include('header.html'); ?>
<div class="right_side">
<!-- content of this page -->
</div>
Also note: While the last line in PHP immediately proceeding a ?> does not need a semi colon at the end of the line, all other lines in PHP do. For the sake of consistency, you should consider putting semi colons on the end of all your PHP lines.
Upvotes: 0
Reputation: 36970
You don't want to create a function for this .
Please see the following
1)create the following page left.menu.inc.php will contain menu items
<ul>
<li><a href="page1.php">First Page</a></li>
<li><a href="page2.php">Second Page</a></li>
<li><a href="page3.php">Third Page</a></li>
</ul>
2)After main page look like
<div class="left_side">
<?php include_once "left.menu.inc.php";?>
</div>
<div class="right_side">
<!-- contents of this page -->
</div>
Upvotes: 1
Reputation: 324640
Put your left_side
stuff as HTML in a file, such as left_side.html
. Then, when you want to include it, put <?php include("left_side.html"); ?>
. You can also include PHP in the navigation, if needed, and that'll be parsed and run. If this will never be needed, you can also use <?php readfile("left_side.html"); ?>
Upvotes: 0
Reputation: 4161
You can make template.php file for such functions and include it above of all code in layout:
<?php include('template.php') ?>
Some HTML
<?php left_side() ?>
Upvotes: 1