catonthecouchproductions
asked on
Create a dynamic array
I have the basic header/footer navigation using GET and putting file names in to an arrray:
<?php
if (isset($_GET['p'])) {
$pages = array('contact','drinksspe cials','bu lletin','p ictures',' media','de signfriend s');
if (in_array($_GET['p'], $pages)) { // if exists, then include it
$page=$_GET['p'] ;
}
else {
$page='404';
}
}
else { // default page
$page='default';
}
if ($page=='default') {
$header='header';
}
else {
$header='header-home';
}
include $header.".php";
include $page.".php";
include "footer.php";
?>
I have alot of HTML files and I am wondering if it is possible to keep them as HTML and stil be called by the PHP? And also is there a way to create the array dynamically and just put in the filenames like above in the code?
Thanks
Ryan
<?php
if (isset($_GET['p'])) {
$pages = array('contact','drinksspe
if (in_array($_GET['p'], $pages)) { // if exists, then include it
$page=$_GET['p'] ;
}
else {
$page='404';
}
}
else { // default page
$page='default';
}
if ($page=='default') {
$header='header';
}
else {
$header='header-home';
}
include $header.".php";
include $page.".php";
include "footer.php";
?>
I have alot of HTML files and I am wondering if it is possible to keep them as HTML and stil be called by the PHP? And also is there a way to create the array dynamically and just put in the filenames like above in the code?
Thanks
Ryan
ASKER
Will PHP allow me to keep the files as HTML? And I will change it to '.html'
Will that work?
So my code will be:
<?php
if (!empty($_GET['p']) && file_exists(basename($_GET ['p']) . '.html')) {
$page = basename($_GET['p']);
} else if (empty($_GET['p'])) {
$page = 'default';
} else {
$page = '404';
}
}
else { // default page
$page='default';
}
if ($page=='default') {
$header='header';
}
else {
$header='header-home';
}
include $header.".php";
include $page.".php";
include "footer.php";
?>
Does that look correct?
Thanks,
Ryan
Will that work?
So my code will be:
<?php
if (!empty($_GET['p']) && file_exists(basename($_GET
$page = basename($_GET['p']);
} else if (empty($_GET['p'])) {
$page = 'default';
} else {
$page = '404';
}
}
else { // default page
$page='default';
}
if ($page=='default') {
$header='header';
}
else {
$header='header-home';
}
include $header.".php";
include $page.".php";
include "footer.php";
?>
Does that look correct?
Thanks,
Ryan
if you then include:
include $page.".html";
it looks ok to me. If you have php and html-files you must change the code so $page inculdes also the extension.
include $page.".html";
it looks ok to me. If you have php and html-files you must change the code so $page inculdes also the extension.
ASKER
i missed that, so like you said.
Change all of the:
include $header.".php";
include $page.".php";
include "footer.php";
to HTML?
Or can those still be PHP? Or would it be better practice to have all HTML?
Change all of the:
include $header.".php";
include $page.".php";
include "footer.php";
to HTML?
Or can those still be PHP? Or would it be better practice to have all HTML?
ASKER
I have:
<?php
if (!empty($_GET['p']) && file_exists(basename($_GET ['p']) . '.htm')) {
$page = basename($_GET['p']);
} else if (empty($_GET['p'])) {
$page = 'default';
} else {
$page = '404';
}
}
else { // default page
$page='default';
}
if ($page=='default') {
$header='header';
}
else {
$header='header';
}
include $header.".html";
include $page.".htm";
include "footer.html";
?>
And when I test it, I get a blank page when I go to index.php
Any suggestions?
I checked my filenames, etc.
<?php
if (!empty($_GET['p']) && file_exists(basename($_GET
$page = basename($_GET['p']);
} else if (empty($_GET['p'])) {
$page = 'default';
} else {
$page = '404';
}
}
else { // default page
$page='default';
}
if ($page=='default') {
$header='header';
}
else {
$header='header';
}
include $header.".html";
include $page.".htm";
include "footer.html";
?>
And when I test it, I get a blank page when I go to index.php
Any suggestions?
I checked my filenames, etc.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you! Worked great!
Ryan
Ryan
e.g:
if (!empty($_GET['p']) && file_exists(basename($_GET
$page = basename($_GET['p']);
} else if (empty($_GET['p'])) {
$page = 'default';
} else {
$page = '404';
}
the basename is needed to avoid remote inclusion of files like ?p=http://example.com/evil.script.txt