douyou2368 2011-12-29 22:16
浏览 70
已采纳

有没有更简洁的方法来写这个避免包括

I'm writing a project in php that will basically give me a webpage with links to things like:

  • a page containing my favorite youtube videos stored in a mysql db
  • a page with all my favorite games
  • and music ect

All will be embeded directly inside the webpage rather than linking to the site.

To simplify this I've made a master template "index.php" and then using $_GET I pass pages and other info through to the template which is then handled by a page check script which performs a number of ifs to determine what php script to load into the website.

Where my question lies is whats the prefered method for loading theese scripts into the website as my current method is

if ($_GET["page"] == "videos"){
    include ("dynapage/scripts/videos/videos.php");
}

if ($_GET["page"] == "music"){
    include ("dynapage/scripts/music/music.php");
}

ect

Is using includes to add this code into my template a good thing to do or is there a better method you can suggest?

note: checks for isset are already included in the checks and aditional pbs are availble on request.

  • 写回答

3条回答 默认 最新

  • dongzhi4690 2011-12-29 22:23
    关注

    Maybe Something like this:

    $config = array(
        'videos' => 'dynapage/scripts/videos/videos.php',
        'music' => 'dynapage/scripts/music/music.php'
    );
    
    if(isset($config[$_GET['page']])) {
         include($config[$_GET['page']]);
    }
    

    is a bit better, then writing many if clauses (;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000