Monday, June 6, 2011

Php pages (root directory)?

hello I am a novice in php, I have built a couple of sites on my local host. My php page all have a single include in on all of them (left_nav-right_nav etc...) to make it easy for site wide link exchanges and what not. What if I wanted to have a directory of php pages lets say all the pages had to do with one thing. I would like to have them in a lower diectory, how can you still have includes in the lower directory without changing the link codes ../ etc...Php pages (root directory)?
Since you're using a programming language, you're not really limited to using the include function in this way. This, at it's root, is the difference between just using templates, and a full-blown web application. A specific example will make things more clear.



Say you had files called header.php, navbar.php, and footer.php and they all appear on each page. The natural tendancy is to then include those files on other pages where you need them. My normal recommendation to students and trainees is to do exactly the opposite. Put all those components on ONE page, in ONE place, and then call the script you need in a VARIABLE in your include file. For example, let's say you had an ';index.php'; file, that looks something like this...

%26lt;html%26gt;

%26lt;head%26gt;

%26lt;title%26gt;%26lt;?php echo $page_title ?%26gt;%26lt;/title%26gt;

%26lt;/head%26gt;

%26lt;body%26gt;

%26lt;?php require_once('header.php') ?%26gt;

%26lt;table%26gt;

%26lt;tr%26gt;

%26lt;td%26gt;%26lt;?php require_once('navbar.php') ?%26gt;%26lt;/td%26gt;

%26lt;td%26gt;%26lt;?php require_once($_GET['page'] . '.php') ?%26gt;%26lt;/td%26gt;

%26lt;/tr%26gt;

%26lt;/table%26gt;

%26lt;?php require_once('footer.php') ?%26gt;

%26lt;/body%26gt;

%26lt;/html%26gt;





Now, let's say you had a page called ';hello.php';. You could call it from your index page like this...



http://www.example.com?page=hello



And of course, with a bit of trickery using Apache's mod_rewrite module, you could easily convert that to...



http://www.example.com/helloPhp pages (root directory)?
You could try and use php's 'predefined variables'.



$link = $DOCUMENT_ROOT . ';/left_nav.inc';;

include $link;



You could edit the above to suit your needs..
  • myspace pages
  • advice and suggestions
  • No comments:

    Post a Comment