GetDotted Domains

Viewing Thread:
"Simple php for recurring text in your web pages"

The "Freeola Customer Forum" forum, which includes Retro Game Reviews, has been archived and is now read-only. You cannot post here or create a new thread or review on this forum.

Sun 23/07/06 at 10:05
Regular
Posts: 46
Partly in response to Digitrader's question about how to have repeating chunks of HTML throughout your webpages, such as menus, headings and footers, without having to write (and update) identical code in lots of places, I have written a teeny tutorial. It has an example webpage, which uses simple (really simple) php to do the job.

Perfect if you don't actually know any php, but are pretty au fait with html.

Simple Tutorial
Problem: You want to have the same text appear on lots of webpages, but you don't want to write it lots of times in different places.
Solution: Use some simple php.
Result: See example web pages.

An 'include' file with the common bits of HTML:
The first thing I do is create a file with all the bits of html which will be common throughout my website. In this case:
The header
The menu
And the footer.

1. Create the include file - it must be a php file, and I called mine Common.php.

Within any php file you can write a mixture of html and php. All the bits of php must be enclosed within these: so the server knows to read it as php.

Each Piece of common html will go in a separate function in the common.php file. A function looks like:
function functionName()
{
... the function code
}

Inside each function you can write the html chunk, so that my common.php file looks like:


<?
function drawTitle()
{
?>
<html>
<head>
<link href=style.css type=text/css rel=stylesheet>
<title>Simple PHP</title>
</head>

<body>
<h1>Simple Web Page</h1>
<?
}

function drawMenu()
{
?>
<p class="menu">
<b>Menu</b><br>
<a href="index.php">MenuItem A</a><br>
<a href="page2.php">Menu Item B</a><br>
Menu Item C<br>
Menu Item D<br>
</p>
<?
}

function drawFooter()
{
?>
<p class="foot">
Joanne Walker &copy;
</p>
</p>
</body>
</html>
<?
}
?>

Notice how everytime I start some php I have a .

2. So now write each common bit of html within a function.

That's the most difficult bit done.

I then have two pages so far in the site. Each of these will be able to call on the functions in common.php to write chunks of text automatically.

3. Create your first webpage, it must also be a php file. I called mine index.php.

Inside your page the first thing you're going to write is a bit of php to tell it where to look for the common bits of code:

4. Include the common file inside the webpage: at the very top of the file enter:
<?
include("common.php");

and don't forget the semi-colon on the end.

Then you call on the various functions you've written at the right point in the code. This is done by firstly making sure you're in php 'mode' - use
<?
include("common.php");

drawTitle();
drawMenu();

?>
My main webpage text, with words and images, set up as usual.<br>
Here's some text;<br>
And some more text.
<br><br>
And yet more text.
<?
drawFooter();
?>

5. Write the webpage, calling on your common functions wherever you want that html to appear.

6. Write the rest of your web pages in the same way. My page2.php looks like:

<?
include("common.php");

drawTitle();
drawMenu();

?>
Another page with some more text in it.
<?
drawFooter();
?>



I have structured my pages using css only. You can also use this method if you structure your pages with tables (my personal page uses tables), though it becomes slightly more complicated.

For the css I used in this tutoral, see here.
Wed 26/07/06 at 12:21
Regular
Posts: 1,014
Wow a missed this post Dr Jo yes this is good this explains more about PHP and its mysterys lol I will work on this and see what I can come up with, it looks like a challenge though, I do know HTML and have worked with it for many years but look forward to many more hints and tips on PHP.

Kev
Sun 23/07/06 at 10:05
Regular
Posts: 46
Partly in response to Digitrader's question about how to have repeating chunks of HTML throughout your webpages, such as menus, headings and footers, without having to write (and update) identical code in lots of places, I have written a teeny tutorial. It has an example webpage, which uses simple (really simple) php to do the job.

Perfect if you don't actually know any php, but are pretty au fait with html.

Simple Tutorial
Problem: You want to have the same text appear on lots of webpages, but you don't want to write it lots of times in different places.
Solution: Use some simple php.
Result: See example web pages.

An 'include' file with the common bits of HTML:
The first thing I do is create a file with all the bits of html which will be common throughout my website. In this case:
The header
The menu
And the footer.

1. Create the include file - it must be a php file, and I called mine Common.php.

Within any php file you can write a mixture of html and php. All the bits of php must be enclosed within these: so the server knows to read it as php.

Each Piece of common html will go in a separate function in the common.php file. A function looks like:
function functionName()
{
... the function code
}

Inside each function you can write the html chunk, so that my common.php file looks like:


<?
function drawTitle()
{
?>
<html>
<head>
<link href=style.css type=text/css rel=stylesheet>
<title>Simple PHP</title>
</head>

<body>
<h1>Simple Web Page</h1>
<?
}

function drawMenu()
{
?>
<p class="menu">
<b>Menu</b><br>
<a href="index.php">MenuItem A</a><br>
<a href="page2.php">Menu Item B</a><br>
Menu Item C<br>
Menu Item D<br>
</p>
<?
}

function drawFooter()
{
?>
<p class="foot">
Joanne Walker &copy;
</p>
</p>
</body>
</html>
<?
}
?>

Notice how everytime I start some php I have a .

2. So now write each common bit of html within a function.

That's the most difficult bit done.

I then have two pages so far in the site. Each of these will be able to call on the functions in common.php to write chunks of text automatically.

3. Create your first webpage, it must also be a php file. I called mine index.php.

Inside your page the first thing you're going to write is a bit of php to tell it where to look for the common bits of code:

4. Include the common file inside the webpage: at the very top of the file enter:
<?
include("common.php");

and don't forget the semi-colon on the end.

Then you call on the various functions you've written at the right point in the code. This is done by firstly making sure you're in php 'mode' - use
<?
include("common.php");

drawTitle();
drawMenu();

?>
My main webpage text, with words and images, set up as usual.<br>
Here's some text;<br>
And some more text.
<br><br>
And yet more text.
<?
drawFooter();
?>

5. Write the webpage, calling on your common functions wherever you want that html to appear.

6. Write the rest of your web pages in the same way. My page2.php looks like:

<?
include("common.php");

drawTitle();
drawMenu();

?>
Another page with some more text in it.
<?
drawFooter();
?>



I have structured my pages using css only. You can also use this method if you structure your pages with tables (my personal page uses tables), though it becomes slightly more complicated.

For the css I used in this tutoral, see here.

Freeola & GetDotted are rated 5 Stars

Check out some of our customer reviews below:

Many thanks!
You were 100% right - great support!
Brilliant service.
Love it, love it, love it!
Christopher

View More Reviews

Need some help? Give us a call on 01376 55 60 60

Go to Support Centre
Feedback Close Feedback

It appears you are using an old browser, as such, some parts of the Freeola and Getdotted site will not work as intended. Using the latest version of your browser, or another browser such as Google Chrome, Mozilla Firefox, or Opera will provide a better, safer browsing experience for you.