including a menu using php code in html files

 
Post new topic   Reply to topic    34SP.com Forum Index // Scripting Support
View previous topic :: View next topic  
Author Message
innersound
34SP Newbie
34SP Newbie


Joined: 02 Oct 2014
Posts: 2
Location: London

PostPosted: Sun Mar 01, 2015 9:47 pm    Post subject: including a menu using php code in html files Reply with quote
Hi there,

I would dearly like to use the php "include" function to add an html menu to my website with multiple pages to maintain, so hoping to update headers & footers by using one file for each instead.

I'm a complete "novice" regarding php, so have watched numerous php tutorial videos to gain direction, most of which say 'all i have to do' is create the relevant html list in a php file & then include a php script in the relevant locations of my web pages.

However, despite trying different options for hours, I'm struggling to get the menu - or anything - to appear at all.

my questions are:
1) I've assumed php coding is allowed on professional accounts (which I have) - is that the case or not?
2) if yes, could a php-savvy saviour kindly assist me in identifying where i'm clearly going wrong?

I used sublime text 2 & an html editor software program to achieve my aim & neither is yet returning the result I'm hoping for. I tried uploading the files to my website & trying the code in a web browser (mozilla) and neither is working so I'm guessing it's "operator error@ somewhere!

here's the php code I've used in my html doc to include the menu list:

<?php include $_SERVER['DOCUMENT_ROOT'] "header.php"; ?>


and in the header.php file, I put:

<?php
echo "<li><a href="index.html">Home</a></li>
<li><a href="about.html">About</a></li>
<li><a href="music.html">Music</a>"; ?>

the list is actually much longer and includes two sub-menus but didn't want to include the whole menu here.

Should I have added any script to the .htaccess file? Any glaring omissions in the include command/code in either file?

Any assistance would be greatly appreciated!

regards, sheila
Back to top
View user's profile Send private message Visit poster's website
peterwt
34SP User


Joined: 22 Mar 2001
Posts: 230
Location: South Wales

PostPosted: Mon Mar 02, 2015 1:01 pm    Post subject: Reply with quote
Hi Sheila,


I am sure that php is by default included in professional hosting.

There is a major problem in the way you have tried to create your menu page. The php code is executed on the server by a php interpreter. In order for the interpreter to see the php code the file must have a .php extension. The default setting is that only php files are passed to the php interpreter, although this can be changed. Your index.html should be changed to index.php. There is no problem here as the server will look for index.html to serve your home page but if it does not find it will look for index.php.

One of the advantages of php is that you can put php code in a page written in html. The interpreter will pass the html out until it sees the first php opening tag <?php and then interpret the php code up to the closing tag ?> and output the result to the page at that point in the page. It will then continue to output the html until it sees any more php opening tags. So you can insert php code at any multiple points in your page.

As a first test I would advise you to change your index.html to index.php and remove the php include part you added. You can only include php in a php page anyway. If you browse then to your site you should see your original index page as you did before.

If this works (it should) then in your index.php put the following after the <BODY> tag -

<?php include("menu.php"); ?>

You do not need to use DOCUMENT_ROOT if you have the menu.php in the root directory with index.php. Create menu.php in the root containing -

<p>Test of menu</p>

This should put that text at the top of your home page.

Regards

Peter
_________________
Peter
Back to top
View user's profile Send private message Send e-mail Visit poster's website
innersound
34SP Newbie
34SP Newbie


Joined: 02 Oct 2014
Posts: 2
Location: London

PostPosted: Tue Mar 03, 2015 8:36 am    Post subject: Reply with quote
Dear Peter,
thank you kindly for highlighting my error - and many thanks for a prompt comment - ...I will give that a go
kind regards
sheila
Back to top
View user's profile Send private message Visit poster's website
Post new topic   Reply to topic    34SP.com Forum Index // Scripting Support All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Powered by phpBB © 2001, 2002 phpBB Group