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条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改