Jump to content


Photo

1 menu for a whole site?


  • Please log in to reply
13 replies to this topic

#1 completeamateur

completeamateur
  • Members
  • PipPipPip
  • Advanced Member
  • 69 posts

Posted 01 April 2006 - 10:08 PM

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]

#2 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 01 April 2006 - 10:12 PM

Try this:

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

include ($file);

and see if it still gives you an error
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#3 completeamateur

completeamateur
  • Members
  • PipPipPip
  • Advanced Member
  • 69 posts

Posted 01 April 2006 - 10:18 PM

[!--quoteo(post=360716:date=Apr 1 2006, 11:12 PM:name=play_)--][div class=\'quotetop\']QUOTE(play_ @ Apr 1 2006, 11:12 PM) View Post[/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?

#4 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 01 April 2006 - 10:22 PM

Alternatively you could use this...

<?php

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

?>

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.
follow me on twitter @PHPsycho

#5 sford999

sford999
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 01 April 2006 - 10:25 PM

If the file you need to include is in a different directory, then you can just use:

include ("../inc/file.php");


#6 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 01 April 2006 - 10:32 PM

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

include ("../inc/file.php");
[/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) View Post[/div][div class=\'quotemain\'][!--quotec--]
Alternatively you could use this...

<?php

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

?>

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'
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#7 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 01 April 2006 - 10:33 PM

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)
follow me on twitter @PHPsycho

#8 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 01 April 2006 - 10:35 PM

[!--quoteo(post=360727:date=Apr 1 2006, 05:33 PM:name=ToonMariner)--][div class=\'quotetop\']QUOTE(ToonMariner @ Apr 1 2006, 05:33 PM) View Post[/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 ;)
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#9 completeamateur

completeamateur
  • Members
  • PipPipPip
  • Advanced Member
  • 69 posts

Posted 01 April 2006 - 10:47 PM

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

<?php

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

?>

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!

#10 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 01 April 2006 - 11:07 PM

Sorry mate its my short hand...

$_SERVER['DOCUMENT_ROOT']

get that used to talking bout it!!!!

sorry my bad (embarrassed smily ;))
follow me on twitter @PHPsycho

#11 completeamateur

completeamateur
  • Members
  • PipPipPip
  • Advanced Member
  • 69 posts

Posted 01 April 2006 - 11:19 PM

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.

#12 completeamateur

completeamateur
  • Members
  • PipPipPip
  • Advanced Member
  • 69 posts

Posted 01 April 2006 - 11:32 PM

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='" . $_SERVER['DOCUMENT_ROOT'] . "/subdir/anotherindex.php'>Click here</a>.

</body>
</html>");

?>[/blockquote]

But this does:

[blockquote]<?php

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

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='" . $root . "/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!

#13 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 01 April 2006 - 11:55 PM

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.
follow me on twitter @PHPsycho

#14 completeamateur

completeamateur
  • Members
  • PipPipPip
  • Advanced Member
  • 69 posts

Posted 02 April 2006 - 02:20 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users