Reputation: 168
I'm trying to make a throughly thought out and comprehensive system to include pages for a small hobby project I kicked off today.
This is my code at the moment:
$page = get_uri(1);
if(!isset($page)){ $page = "Home"; }
$pages = array(
"" => "home.php",
"Home" => "home.php",
"Product" => "product.php",
"category" => "category.php"
);
if(in_array($page, $pages) && file_exists(PAGE_DIR.$pages[$page])){
include(PAGE_DIR.$pages[$page]);
} else {
echo 'Uh oh. Four Oh Four, we failed to find the page you are looking for. :( <br />';
}
PAGE_DIR is defined to be = "/pages/"
which is where I host my page files. I've created Home.php
just to proove my theory, and get_uri(1)
is, for now, a null value as I'm trying to get the theory to work. I'm sure the problem lies somewhere in file_exists
, but cannot work out where the problem's at.
Upvotes: 0
Views: 51
Reputation: 2086
Use array_key_exists instead of in_array()
Now your code will look like this.
$page = get_uri(1);
if(!isset($page)){ $page = "Home"; }
$pages = array(
"" => "home.php",
"Home" => "home.php",
"Product" => "product.php",
"category" => "category.php"
);
if(array_key_exists($page, $pages) && file_exists(PAGE_DIR.$pages[$page])){
include(PAGE_DIR.$pages[$page]);
} else {
echo 'Uh oh. Four Oh Four, we failed to find the page you are looking for. :( <br />';
}
Upvotes: 0
Reputation: 6896
Looks as if the condition should be:
if(array_key_exists($page, $pages) etc
http://php.net/manual/en/function.array-key-exists.php
Upvotes: 0
Reputation: 3536
in_array
checks for the array value, not the key, which is what you want. Use if (isset($pages[$page]) && ...)
instead.
Depending on your environment, the filenames may be case-sensitive. Create the file as home.php if you use lowercase filenames in your code.
Upvotes: 2