dp926460 2009-11-30 06:23
浏览 70
已采纳

如何使用PHP包括在网站的所有页面中插入HTML存根

I am developing a simple website. It will have close to 20 pages. It has a 3 level hierarchy.

Home
MenuStub
   Category1
      Page1
      Page2
   Category2
      Page1
      Page2
   ....
      ....
      ....

The main navigation will have 4 - 5 items representing each 'category' This will be constant for all the pages. I am not even planning to highlight the current category or anything.

Previously I decided to put the menu HTML stub alone in a separate file and use PHP include to include it in all pages.

But, relative paths might be frustrating. Assume that the menu stub file is located at the root directory.

So, in the root-level pages, the php include will read like

include "menustub.html";

in the second level pages, it should say

include "../menustub.html";

and in third level pages, it should say

include "../../menustub.html";

First, is this the best way to include a single file across all pages in a website?

Second, if the website grows big, and many more levels are added, maintaining this will be a problem. If I suddenly decide to move an entire set of pages one (or several) levels up or down, I should manually go and change the relative paths in each file.

Am I missing something here? Is there a universal way to point to a particular file, that every page will understand, regardless of where it is located?

What is the best way to have a stub and include it in all the pages without having these maintenance nightmares?

  • 写回答

2条回答 默认 最新

  • doudong7256 2009-11-30 06:44
    关注

    It would be much more dynamic to have the index file do all the including, and use GET parameters to tell it which page is to be include.

    For example, consider a directory structure like this:

    /
      index.php
      /ui
        header.html
        menu.html
        footer.html
        Cat1/
          Page1.html
          Page2.html
        Cat2/
          Page3.html
          Page4.html
    

    If you were to always call the index file, including the name of the category and page you wanted to see, like: index.php?category=Cat1&page=Page1, you could do something like this:

    <?php
    // Include a header and a menu, defined in their own HTML files.
    include('ui/header.html');
    include('ui/menu.html');
    
    // Set up a list of valid categories and their sub pages.
    $pages = array(
        'Cat1' => array(
            'Page1',
            'Page2'
        ),
        'Cat2' => array(
            'Page3',
            'Page4'
        )
    );
    
    // Find the category and page to use.
    if(isset($_GET['category'], $pages[$_GET['category']])) {
        $category = $_GET['category'];
    } else {
        $category = 'Cat1';
    }
    if(isset($_GET['page'], $pages[$category][$_GET['page']])) {
        $page = $_GET['page'];
    } else {
        $page = 'Page1';
    }
    
    // Include the selected content page.
    include("ui/{$category}/{$page}.html");
    
    // Include a footer
    include('ui/footer.html');
    ?>
    

    This way you can expand the content as far and deep as you want without having to repeat your includes in every single new file.

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

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系