dongxun1142 2013-06-28 21:40
浏览 164
已采纳

在整个页面上使用if ... else if语句

ok so i have this if statement it is in the header.php

<?php 
    if (is_page('estate planning')) {
        $estate = array ("eplnp","1","2","3","4","5");} 
        elseif(is_page('probate & trust administration')){
        $trust = array ("eplnp","1","2","3","4","5");}
        elseif(is_page('elder law')){
        $elder = array ("eplnp","1","2","3","4","5");}
        elseif(is_page('meet sara')){
        $meet = array ("eplnp","1","2","3","4","5");}
        elseif(is_page('blog')){
        $blog = array ("eplnp","1","2","3","4","5");}
        elseif(is_page('client forms')){
        $form = array ("eplnp","1","2","3","4","5");}   
        elseif(is_page('request a consultation')){
        $request = array ("eplnp","1","2","3","4","5");}    
            else {
            $home = array ("homebg","hmline1","hmline","phone","home","blurb-hm");}
?>

how would i use this statement globally... like i want it to apply to the index.php header.php and footer.php.

i realize i can save it as bla.php and use include() on all the pages. is there a more efficient way to do it like loading it globally from one page so the code looks neater and can be used on the the currently loaded pages?

  • 写回答

2条回答 默认 最新

  • douyan8266 2013-06-28 21:46
    关注

    Including the file with an include('bla.php') is the most efficient way of doing this. It's a single line of code to include the block and you're not repeating yourself.

    Also re. Voitcus comment, you should be using switch instead of if to simplify this block. Get the page's name as a variable then do:

    switch ($page) {
        case "estate planning":
            $estate = array ("eplnp","1","2","3","4","5");
            break;
        case "probate & trust administration":
            $elder = array ("eplnp","1","2","3","4","5");}
            break;
        ...
        default:
            $home = array ("homebg","hmline1","hmline","phone","home","blurb-hm");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试