The logic seems sound, so it could be a whitespace & non-alphanumeric character issue. So I recommend doing this:
$raw_page = preg_replace('/[^a-zA-Z0-9]+/', '', trim($_GET['p']));
if(!empty($raw_page) && array_key_exists($raw_page, $data)){
$page = $raw_page;
}
else {
$page = "index";
}
I also added an array_key_exists
so the logic can handle a request that is being made to a non-existant page or array index.
EDIT: Also, your array data is inconsistent. Dong an echo $data['index']['title'];
will result in:
<img src = 'logo.png' alt=''>
But doing an echo $data['gallery1']['title'];
will result in:
Gallery 1
So I think you need to iron that out as well.
EDIT FOR ADDITIONAL INFO ON THE MIXED ARRAY VALUE STUFF: Okay, from a programming standpoint there is still an issue with the way the array has mixed value types for the value $data['title']
. I see that as a problem waiting to happen. So I would recommend reworking the $data array structure as so:
$data =
array(
"index" => array(
"name" => "title",
"title" => array("type" => "image", "value" => "<img src = 'logo.png' alt=''>"),
"fallback_html" => "main.php",
"gallery" => array("1" => "nothing")),
"gallery1" => array(
"name" => "gallery1",
"title" => array("type" => "text", "value" => "Gallery 1"),
"fallback_html" => "",
"gallery" => array("1" => "jpg1.jpg","2" => "jpg2.jpg")
)
);
Specifically I did this to the $data["index"]["title"]
that has an image tag:
"title" => array("type" => "image", "value" => "<img src = 'logo.png' alt=''>"),
And this to the $data["gallery1"]["title"]
that has a text string:
"title" => array("type" => "text", "value" => "Gallery 1"),
That will help you on the rendering side of things better handle each case. So you could have code as follows:
if ($data["gallery1"]["title"]["type"] == "image") {
echo $data["gallery1"]["title"]["value"] . "<br clear="all" />";
}
else if ($data["gallery1"]["title"]["type"] == "text") {
echo "<h1>" . $data["gallery1"]["title"]["value"] . "</h1>";
}
The example above shows that if you tag the type at the datasource, you can do different things to each type when it comes time to render. If you feel it’s too much work, that’s fair. But mixing datatypes in values is never a good idea without someway to clearly differentiate them.