2 Replies - 4110 Views - Last Post: 18 July 2013 - 02:05 PM Rate Topic: -----

#1 squibby   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 91
  • Joined: 21-January 12

Setting root folder in PHP problem

Posted 18 July 2013 - 01:45 PM

Hi I have a problem with setting the root folder in PHP.

I want to be able to set the root folder of my website in a config file and then include that at the top of the page so i can reference all my include files like header.php, menu.php etc. from any folders i choose to split and organise my pages.


My file Structure is like this:

[inc]
header.php
menu.php
footer.php
etc
[assets]
js
css
etc
[enquiry]
add-enquiry.php
[customers]
add-customer.php
etc
-------------


index.php
config.php



inside my config file i have

$path = $_SERVER['DOCUMENT_ROOT'].'/pav/';
// pav is the folder of my project in htdocs





Then at the top of each page i declare what i want to include like this:

<?php  
include_once('config.php');
include_once($path ."inc/header.php"); 
include_once($path ."inc/top-nav.php"); 
include_once($path ."inc/menu.php"); 
include_once($path ."inc/title-and-breadcrumb.php"); 
?>




Inside my menu.php i have nav menu which has links to other pages like this:

<?php echo $path ?>enquiry/add-enquiry.php">




However even though the includes work the links dont. They go to like this X:/xampp/htdocs/pav/enquiry/add-enquiry.php


I know this is very noobish but does anyone have an ideal way of setting up the path so it always starts from top level and moves down to required page.


Much appreciation if you could help me out.

Is This A Good Question/Topic? 0
  • +

Replies To: Setting root folder in PHP problem

#2 lukasz_ltt   User is offline

  • New D.I.C Head

Reputation: 18
  • View blog
  • Posts: 29
  • Joined: 12-October 12

Re: Setting root folder in PHP problem

Posted 18 July 2013 - 01:51 PM

Consider using two variables, try this:
in your config

$path = '/pav/';
$root = $_SERVER['DOCUMENT_ROOT'].$path;



in top of each page:
include_once('config.php');
include_once($root ."inc/header.php"); 
include_once($root ."inc/top-nav.php"); 
include_once($root ."inc/menu.php"); 
include_once($root ."inc/title-and-breadcrumb.php"); 



And your menu.php leave without changes

This post has been edited by lukasz_ltt: 18 July 2013 - 01:52 PM

Was This Post Helpful? 2
  • +
  • -

#3 squibby   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 91
  • Joined: 21-January 12

Re: Setting root folder in PHP problem

Posted 18 July 2013 - 02:05 PM

Yes that has worked. Thank you so much for your help :-)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1