PHP Build a Basic PHP Website Listing and Sorting Inventory Items Creating the Display Function

samtruss1986
samtruss1986
11,772 Points

Using this custom function when webpages are in sub-folders.

Greetings Alena from the UK,

Firstly thank you for your tutorials I'm learning at an incredible rate. I have a question about using this function when the webpages are in subfolders.

When we go through the tutorial the pages we create are all in the main directory. However as you may appreciate larger websites may have many sub-folders.

My question is how can I utilise this piece of code for a template across a whole website which contain many sub-folders?

I have tried implementing this but the code does not seem to load the "li" 's.

Thank you in advance.

1 Answer

Codin - Codesmite
Codin - Codesmite
8,589 Points

What I normally do is have all my include files in a folder called "includes"

So say for example we have our website root:

root/index.php
root/somefolder/articles.php

"articles.php" isn't at the root like "index.php" it is in a folder called "somefolder"

Lets say I have a file called "myinclude.php" and I need I to use it on every page including the one in the "somefolder" subfolder.

I will place "myinclude.php" into another subfolder called "includes" so now our website root looks like this:

root/index.php
root/somefolder/articles.php
root/includes/myinclude.php

On both index.php and articles.php I will write my include like this:

require_once('/includes/myinclude.php');

Relative paths that begin with "/" will start from the root regardless of where the file is located.

Whereas for example like in the video if you had:

require_once('includes/myinclude.php');

This will work from index.php as index.php is located at the root, so it will read as "root/includes/myinclude.php".

But if you were to do the same from the articles.php file it will read as "root/somefolder/includes/myinclude.php" as it will start from the same level as the myinclude.php file.

Hope this helps :)

samtruss1986
samtruss1986
11,772 Points

Hi Ashley,

Thank you for your speedy response.

I have tried this method as well as trying "include" instead of "require_once". When I use the method of adding a "/" for items to load from the root nothing but a blank page loads.

e.g require_once('/inc/functions.php'); // this method results in a blank page.

If I use an alternative method "../"

e.g require_once('../inc/functions.php'); // this method works, but isn't ideal with large websites with many sub-folders.

It all loads fine but this is not ideal for large websites so I thought I'd ask the question. It might be worth mentioning to anyone else reading that in the data.php file I have included a "/" before the "img"

i.e "img" => "/img/media/design_patterns.jpg",

and when I use the alternative method (mentioned above) all files display correctly.

In the tutorial we use a folder called "inc" for the include or require_once files to be stored in and all pages work fine because they are in the root of the website.

I created a folder called catalogs and put the catalog.php file inside it and this is where I'm having my problem.

Thank you again.

EDIT:

Hello again Ashley,

After reading some threads from stackoverflow and documentation from php.net I think I may have solved my question.

If I include

include($_SERVER['DOCUMENT_ROOT'] . '/inc/functions.php');

the code works for all all sub-folders.

I was wondering if this is: a) correct and b) the best method.

Thank you.

Reference. http://stackoverflow.com/questions/1400795/root-path-doesnt-work-with-php-include

Codin - Codesmite
Codin - Codesmite
8,589 Points

The answer from stackoverflow is perfectly ok to use, $_SERVER is a pre-defined array in PHP with many pre-defined reserved variables, $_SERVER['DOCUMENT_ROOT'] will be a pre-defined variable of the root location on your server.

One more thing you can try is './inc/functions.php', One dot refers to the current folder that the file resides in.

Both methods are perfectly fine to use.

More info on $_SERVER : http://php.net/manual/en/reserved.variables.server.php

samtruss1986
samtruss1986
11,772 Points

Many thanks for your replies and time.