I have been playing around with a new site lately, and co-incidentally, as soon as I started with the PHP etc, I have people on MSN Messenger flooding me with questions, mostly in the form of "help me, help me" etc.
Well, a few small tips and explanations on them I gave over MSN Messenger, gave me the idea of posting an article/tutorial (or whatever you want to call it) here about it.
RELATIVE PATHING:
The first thing to get out of the way, is relative pathing.
I would hope that most people are already using this, but surprisingly many newer web developers aren't (I do of course as I come from a heavy list of general programming and scripting language background).
The basic essence of relative pathing is merely using path's to a file, folder etc in a "relative" rather than "full" manner, for example:
Let's say you are messing around in the file editor of your Asta Host control panel, you would normally put you index file in the WWW folder (or at least I do), so let us assume that you are working with your index file in this folder for ease of use.
Some people may use a "full" path to access, say, there contact page (which also resides in the WWW directory directly) like so:
CODE
http://www.mysite.com/contact.html
But this is completely unnecessary, we can instead, use "relative" pathing like so:
CODE
contact.html
Wow it's much shorter isn't it? This is because (ideally in this article/tutorial) you have linked this in your index file, we are working with a file that resides in the exact same directory as our contact file, so we don't need to do anything but reference the actual filename.
However, what to do if the file you wish to access resides in directories deeper from the file we are working with, or perhaps the file resides in directories higher up from the file we are working with.
Well in the case of the former:
CODE
myfolder/myfile.html
Of course if it were several folders deeper, we would add them also, it's no differant then a full path, except that we remove the web address from the picture.
Now in the case of the latter:
CODE
../myfolder/myfile.html
This one takes a bit of a different approach, the ../ basically tells it to go backwards by one directory/folder.
So for example, you have a members folder in your WWW folder, you are working in members.html or what ever, but you need to access/include a database connection file from the DB folder which is in the WWW folder also.
In this case, we do like above, ../ from our members folder (which will takes up back to the WWW folder), ../DB (takes us back to the DB folder in the WWW folder) ../DB/file.php (should be obvious now
If you want to go back more than 1 directory/folder, just use more ../'s.
USING INCLUDE:
There is more than 1 way to use this, and several different calls, but primarily I just use include 'file';
This statement is very much like using external CSS and Javascript (although obviously different), but a similar concept. It will literally "include" all code from the file you specify inbetween the quotes, as if it was there to begin with.
Why is this useful?
Well we could easily split our sites layout up into several files, I personally have a header, menu and footer (the main content file being the one we use the include statements in).
You'll basically want to move any header specific code to a seperate file, menu specific code to a seperate file and footer specific code to a seperate file. Don't worry, other than this, there is no other change necessary what so ever (you don't need to go messing around with opening/closing html tags or php tags etc, as I said, it will literally include the code from the file, as if it had been placed in there manually).
This is extremely usefull, as we can re-use code from these files, in all of our main files, just like we do with external CSS and Javascript files. And as you might already imagine, 1 change to any of these external files we include, all of our "main" files including them will be all updated automatically (no need to manually edit every single main file).
Ok, so whether you are running things locally (with something like WAMP Server or EasyPHP), or doing things via the file editor in the Asta Host hosting control panel (CPanel), let's do a very simple test that should help enlighten you as to what exactly happens.
You will need to create 2 PHP files, name 1 of them main.php (our core file in this test), and header.php (the file we will include from our main.php file).
In the header.php file, put:
CODE
<?php echo "<h1>OUR HEADER</h1>"; ?>
And in our main.php file, put:
CODE
<?php include 'header.php'; ?>
<p>Look our header has been *included* above.</p>
<p>Look our header has been *included* above.</p>
After saving the changes to these files after there creation, go ahead and try accessing main.php, hopefully you should get the h1 HTML code place at the very top of the generate page, and our little paragraph below it.
As we used the include statement to include our header.php file above the paragraph html, it of course is generate first and above.
Well that's it for now, this has ended up much longer than I had intended but I tried to write it so most people should be able to understand it. I was going to add some explanatory material on creating what I call "constants" and using them in conjunction with both "relative pathing" and "file includes".
That particular part, would basically help you to have core header, menu, footer etc files in your main (or WWW directory if that's the case) and yet be able to access these in pages in other folders still using "relative pathing" as oppose to "full pathing".
Any comments, feedback, suggestions etc would be greatly appreciated (especially when I one day intend to build up a site filled with such articles/tutorials on many different programming and scripting languages).

