开发者

php if-statement statement against list, need help streamlining

So I am trying to put together a small lightweight framework for some basic sites I'm building for my hosting customers and I'm attempting to call in to the index.php various includes.

I've got it figured out but feel that there's got to be a better way to write the following if statement:

<?php
        if('home'==$currentpage)
        {
        include('shared/footer.htm');
        }   
        elseif('testing'==$currentpage)
        {
        include('shared/footer.htm');
        }
        elseif('training'==$currentpage)
        {
        include('shared/footer.htm');
        }
        elseif('contact'==$currentpage)
        {
        include('shared/footer.htm');
        }
        elseif('pricing'==$currentpage)
        {
        include('shared/footer.htm');
        }
    ?>

I got the following to work sort of, it uses what ever the last ite开发者_如何学运维m in the list is:

$arr = array('home', 'testing', 'training', 'contact');
        foreach ($arr as &$value);

        if ($value==$currentpage)
        {
        include('shared/footer.htm');
        }

That one will display the footer.htm on the contact page but none of the others, if I switch it around then it show what ever item ends up last, I've also tried a foreach statement and it breaks the page so I gave it up and figured I'd ask for a lil help.

Thanks in advance.


$arr = array('home', 'testing', 'training', 'contact','pricing');
if (in_array($currentpage,$arr))
{
   include('shared/footer.htm');
}


you can use simple array list:

$arrPage = array(
 'home' => 'shared/footer.htm',
 'testing' => 'shared/footer.htm',
 'training' => 'shared/footer.htm',
 'contact' => 'shared/footer.htm',
 'pricing' => 'shared/footer.htm',
);
if( array_key_exists( $arrPage, $currentPage ))
    include($arrPage[$currentpage]);


You can have a map and then use it to call the correct page. You can drop the paths if you don't have differing paths to your files. I'm assuming that's a typo.

$pages = array(
   'home' => 'shared/footer.htm',
   'testing' => 'shared/footer.htm',
   'training' => 'shared/footer.htm'
); //and so forth

if (isset($pages[$currentpage])) {
    include($pages[$currentpage]);
} else {
   //show default page
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜