Generally what I used to do was this
if($_GET['page'] == 'hello') { //This page is always there
echo 'hello';
} elseif($_GET['page'] == 'world') {
echo 'world';
} elseif($_GET['page'] == 'demo') {
echo 'demo';
}
But now am getting these page names from my database dynamically so how do I set something like this if am retrieving the names dynamically? So how I can make something like this when I retrieve page names as
demo1
, demo2
, demo3
?
Something like this?
if ($_GET['page'] == 'hello') {
echo 'hello';
} else {
$pagesFromDatabase = array();
foreach ($pagesFromDatabase as $page) {
if ($page['slug'] == $_GET['page']) {
// do something with page
}
}
}
This works assuming $pagesFromDatabase
is an associative array with each row containing the key 'slug' referring to the possible 'page' parameter in $_GET.
If you're referring to Wordpress, you can retrieve a page like so:
get_page_by_path($_GET['page']);
http://codex.wordpress.org/Function_Reference/get_page_by_path
You can use something like:
$page = array('hello');
#database loop
$page[] = $row[your_page_row];
#end database loop
if(in_array($_GET['page'], $page){#for strict comparison in_array($_GET['page'], $page, true);
echo $_GET['page'];#you might need to use something like htmlspecialchars();
}