?
Solved

Create a dynamic array

Posted on 2007-10-11
7
Medium Priority
?
292 Views
Last Modified: 2008-02-26
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','drinksspecials','bulletin','pictures','media','designfriends');
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
0
Comment
  • 4
  • 3
7 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 20059608
you can use e.g. file_exists.

e.g:

if (!empty($_GET['p']) && file_exists(basename($_GET['p']) . '.php')) {
    $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
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20059648
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
0
 
LVL 48

Expert Comment

by:hernst42
ID: 20059669
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20059716
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?
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20059848
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.
0
 
LVL 48

Accepted Solution

by:
hernst42 earned 2000 total points
ID: 20060430
Guess should be more like:

<?php
if (!empty($_GET['p']) && file_exists(basename($_GET['p']) . '.htm')) {
    $page = basename($_GET['p']) . '.htm';
} else if (!empty($_GET['p']) && file_exists(basename($_GET['p']) . '.php')) {
    $page = basename($_GET['p']) . '.php';
} else if (empty($_GET['p'])) {
    $page = 'default.php';
} else {
   $page = '404.hp';
}

if ($page=='default.php') {
    $header='header';
} else {
    $header='header-home';
}

include $header.".php";    
include $page;
include "footer.html";
?>
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20060808
Thank you! Worked great!

Ryan
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question