Jump to content

Archived

This topic is now archived and is closed to further replies.

completeamateur

1 menu for a whole site?

Recommended Posts

Hello, I am new to php and I have a problem that I am sure php can solve, but don't know how.

Firstly, I suppose it may help if I tell everyone what I have got:
Windows XP
Apache 2.0.55 win32 (for testing offline)
PHP 4.4.2 win32
IE/firefox

What I am trying to do is have an include file (which is the menu for the whole site) which I can then just 'load' into all individual files, making changes easier. The problem I have is defining the location of the include file (as it is located in a different directory).

The source files are stored on a local server in:
C:\Program Files\Apache Group\Apache2\htdocs

and can be viewed by opening (in a browser):
[a href=\"http://localhost/\" target=\"_blank\"]http://localhost/[/a]

I had thought of

$root = "http://localhost/"; //can be changed to address of website

then constructing the location of the include file:

include ($root . "include/menu.php")

...but this doesn't seem to work on my local server. I get the "Apache HTTP Server has encountered a problem and needs to close. We are sorry for the inconvenience." pop-up box. I haven't tried uploading it.

I suppose what I really need is to be able to access the root directory or something? I don't really know what I'm talking about so if anyone can make any sense of this, that would be great! Below is the location & content of the files to try and give a better explanation.

[a href=\"http://localhost/index.php\" target=\"_blank\"]http://localhost/index.php[/a]
[blockquote]<?php

$root = "http://localhost/";

echo("<html>
<body>

This is some text.");

include ($root . "include/menu.php");

echo("This is some more text. <a href='" . $root . "subdir/anotherindex.php'>Click here</a>.

</body>
</html>");

?>[/blockquote]

[a href=\"http://localhost/subdir/anotherindex.php\" target=\"_blank\"]http://localhost/subdir/anotherindex.php[/a]
[blockquote]<?php

$root = "http://localhost/";

echo("<html>
<body>

This is some text.");

include ($root . "include/menu.php");

echo("This is some more text. <a href='" . $root . "index.php'>Back to main index</a>.

</body>
</html>");

?>[/blockquote]

[a href=\"http://localhost/include/menu.php\" target=\"_blank\"]http://localhost/include/menu.php[/a]
[blockquote]<?php

echo("<hr>This is the menu<hr>");

?>[/blockquote]

Share this post


Link to post
Share on other sites
Try this:

$root = "http://localhost/";
$menu = "include/menu.php";
$file = $root.menu;

include ($file);

and see if it still gives you an error

Share this post


Link to post
Share on other sites
[!--quoteo(post=360716:date=Apr 1 2006, 11:12 PM:name=play_)--][div class=\'quotetop\']QUOTE(play_ @ Apr 1 2006, 11:12 PM) [snapback]360716[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Try this:

$root = "http://localhost/";
$menu = "include/menu.php";
$file = $root.menu;

include ($file);

and see if it still gives you an error
[/quote]

No joy play, but thanks for the quick reply. Do you think it is a problem with my set up but might work if I upload it to my webspace?

Share this post


Link to post
Share on other sites
Alternatively you could use this...

[code]<?php

include($_SERVER['DOC_ROOT'] . "/includes/menu.php");

?>[/code]

Should be able to use that on each and every page with no problem.

Bear in mind that apache shutting down could be as a result of a different error in your scripts... even if it only occure when you implemented this.

Share this post


Link to post
Share on other sites
If the file you need to include is in a different directory, then you can just use:

[code]include ("../inc/file.php");[/code]

Share this post


Link to post
Share on other sites
[!--quoteo(post=360725:date=Apr 1 2006, 05:25 PM:name=sford999)--][div class=\'quotetop\']QUOTE(sford999 @ Apr 1 2006, 05:25 PM) [snapback]360725[/snapback][/div][div class=\'quotemain\'][!--quotec--]
If the file you need to include is in a different directory, then you can just use:

[code]include ("../inc/file.php");[/code]
[/quote]

No no, the file is in the same directory always, but he calls the file from different directories.

[!--quoteo(post=360723:date=Apr 1 2006, 05:22 PM:name=ToonMariner)--][div class=\'quotetop\']QUOTE(ToonMariner @ Apr 1 2006, 05:22 PM) [snapback]360723[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Alternatively you could use this...

[code]<?php

include($_SERVER['DOC_ROOT'] . "/includes/menu.php");

?>[/code]

Should be able to use that on each and every page with no problem.

Bear in mind that apache shutting down could be as a result of a different error in your scripts... even if it only occure when you implemented this.
[/quote]


Yea..maybe the intallation went wrong somewhere?

SO yea, completeamateur, try uploading it to your web host.

Also, replace 'include' with 'require'

Share this post


Link to post
Share on other sites
That will only work if the call is from a file in a directory of teh same level as the inc directory.

if inc is in teh root dir then a call to "../inc/menu.php" from root/dir1/dirx/diry/file.php would NOt call the correct file (the only time infact, that no error would be returned in that situation is if this existed.. root/dir1/dirx/inc/menu.php)

MAKE SURE YOUR PATH IS CORRECT AT ALL TIMES (which is why $_SERVER['DOC_ROOT'] is so very useful)

Share this post


Link to post
Share on other sites
[!--quoteo(post=360727:date=Apr 1 2006, 05:33 PM:name=ToonMariner)--][div class=\'quotetop\']QUOTE(ToonMariner @ Apr 1 2006, 05:33 PM) [snapback]360727[/snapback][/div][div class=\'quotemain\'][!--quotec--]
That will only work if the call is from a file in a directory of teh same level as the inc directory.

if inc is in teh root dir then a call to "../inc/menu.php" from root/dir1/dirx/diry/file.php would NOt call the correct file (the only time infact, that no error would be returned in that situation is if this existed.. root/dir1/dirx/inc/menu.php)

MAKE SURE YOUR PATH IS CORRECT AT ALL TIMES (which is why $_SERVER['DOC_ROOT'] is so very useful)
[/quote]

If inc was in the root dir, wouldn't he use "./inc/menu.php" ?

I think you put an extra '.' in there ;)

Share this post


Link to post
Share on other sites
[!--quoteo(post=360723:date=Apr 1 2006, 11:22 PM:name=ToonMariner)--][div class=\'quotetop\']QUOTE(ToonMariner @ Apr 1 2006, 11:22 PM) [snapback]360723[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Alternatively you could use this...

[code]<?php

include($_SERVER['DOC_ROOT'] . "/includes/menu.php");

?>[/code]

Should be able to use that on each and every page with no problem.

Bear in mind that apache shutting down could be as a result of a different error in your scripts... even if it only occure when you implemented this.
[/quote]

Toon, this works for the index.php file in the main directory but not for the anotherindex.php file in the "subdir" directory. Is this because it calls the root of where the file is? I suppose the root I require will always be the root to the main directory? Is there a function for this... or am I just talking garbage!

Share this post


Link to post
Share on other sites
Sorry mate its my short hand...

$_SERVER['DOCUMENT_ROOT']

get that used to talking bout it!!!!

sorry my bad (embarrassed smily ;))

Share this post


Link to post
Share on other sites
DOCUMENT_ROOT seems to return:

C:/Program Files/Apache Group/Apache2/htdoc

on my local server, rather than

[a href=\"http://localhost/\" target=\"_blank\"]http://localhost/[/a]

which is what I think I require, so unfortunately it doesn't work. Suppose it will if I upload it but would be nice to get it working in both. Is this something to do with the setup of apache?

Thanks.

Share this post


Link to post
Share on other sites
Got it working. It seems this doesn't work:

[blockquote]<?php

echo("<html>
<body>

This is some text.<p>");

echo("DOCUMENT_ROOT " . $_SERVER['DOCUMENT_ROOT']);

include($_SERVER['DOCUMENT_ROOT'] . "/include/menu.php");

echo("This is some more text. <a href='" . [b]$_SERVER['DOCUMENT_ROOT'][/b] . "/subdir/anotherindex.php'>Click here</a>.

</body>
</html>");

?>[/blockquote]

But this does:

[blockquote]<?php

[b]$root = ("http://localhost");[/b]

echo("<html>
<body>

This is some text.<p>");

echo("DOCUMENT_ROOT " . $_SERVER['DOCUMENT_ROOT']);

include($_SERVER['DOCUMENT_ROOT'] . "/include/menu.php");

echo("This is some more text. <a href='" . [b]$root[/b] . "/subdir/anotherindex.php'>Click here</a>.

</body>
</html>");

?>[/blockquote]

I guess it all comes with being a complete amateur! I will post back when I get a chance to upload it to my webspace, but fingers crossed it is all sorted now.

Big thank you to everyone who has helped, particularly the tyneside massive!

Share this post


Link to post
Share on other sites
it will return C:\\blah blah on your machine but that is fine!

did it include the menu file correctly? You should have no probs using it at ll just makes sure that all the paths are correct.

Share this post


Link to post
Share on other sites
Tested it all on my webspace and local server and it seems to all work swimmingly, I just need to design the rest of the site now!!!

No doubt i'll be back on here before the end of the day! Thanks for your help.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.