【问题标题】:Is it possible to link to a header.php and footer.php from different folder sources?是否可以从不同的文件夹源链接到 header.php 和 footer.php?
【发布时间】:2019-04-19 12:02:10
【问题描述】:

我是 PHP 的初学者。 我有多个网页位于不同的位置。因此,当我希望从不同文件夹中的网页链接到 header.php 和 footer.php 时,是否可以这样做?如图所示,我必须创建三个不同的文件夹,包含相同的文件 header.php 和 footer.php,以便能够从三个不同的源链接。 致以最诚挚的问候!

【问题讨论】:

  • 是的,有可能
  • 您可以在您的main page 中使用<?php include('header.php'); ?>footer
  • 将您的header.phpfooter.php 放在另一个名为“includes”的文件夹中,并在您的索引文件夹中将任何php 文件写入以下代码。在文件开头添加头文件。 include_once("../includes/header.php"); 在文件末尾包含页脚文件。 include_once("../includes/footer.php");
  • 检查这个问题,这里已经回答了stackoverflow.com/questions/8054638/…

标签: php dynamic webpage


【解决方案1】:
   Assign values for $h_path and $f_path dynamically.
    <?php 

   $h_path = '';
   $f_path = '';

   include($h_path.'header.php'); 
   include($f_path.'footer.php'); 
   ?>

【讨论】:

    【解决方案2】:

    对于没有提供有关这些问题的足够信息,我深表歉意。我的问题是,当 index.php 分别通过“includes/header.php”和“includes/footer.php”引用页眉和页脚时,其他网页位于另一个文件夹中,需要通过“../includes/header.php”。引用文件时没有问题,但是当 headers.php 以内部网页为目标时,当它被写入仅与 index.php 一起使用时,就会出现问题。例如,仅适用于 index.php,但不适用于需要的文件夹内的 php 文件,但我会尝试使用 $h_path = '';和 $f_path = '' 很快。

    【讨论】:

      【解决方案3】:

      是的,可以使用单个 footer.php 和单个 header.php 文件并在需要时随时加载它们。

      我建议您可以做的是创建一个包含文件夹,然后在包含文件夹中创建另一个名为 common 的文件夹,您将在其中放置在整个网站中始终相同的元素,即页脚和页眉。

      然后我还将在包含我将放置我的网站函数的位置中放置一个函数文件。此函数文件中包含一个函数,我将在任何时候使用header.phpfooter.php 文件。

      函数.php

      <?php
      
          function loadView($viename,$meta=[]){
              //load footer/header page
              include_once "common/$viename.php";
      }
      
      //any other functions
      

      loadView() 函数可在您想要加载这两个动态文件的任何时候使用。此函数有两个参数 1 可选。第一个参数是您要加载的视图的名称headerfooter 然后第二个可选参数是对头文件重要的元信息,因为页面标题和元描述需要是动态的并根据到页面。

      header.php

      <!DOCTYPE html>
      <html>
      <head>
          <title><?=$meta['pagetitle']?><!-- Dynamic page title --></title>
          <meta name="description" content="<?=$meta['pagedescription']?>"><!-- Dynamic description -->
      
          <!-- load your styles -->
      </head>
      <body>
          <header>
              <nav>
                  <!-- Your page navigation -->
                  <ul>
                      <li><a href="index.php">Home</a></li>
                      <li><a href="about.php">About</a></li>
                      <li><a href="anotherpage">Another Page</a>
                  </ul>
              </nav>
          </header>
      

      footer.php

      <footer>
          footer content
      
          <p>&copy; website name <?=date('Y')?>
      </footer>
      
      </body>
      </html>
      

      主要网站页面

      您的主要网站页面是索引、关于、服务等页面。

      在这些页面中,您将加载函数文件,然后能够加载页眉和页脚。

      index.php

      <?php
          include 'includes/functions.php';
      
          //meta info
      $meta = array(
              'pagetitle' => 'Welcome to my site | site | bla bla',
              'pagedescription' => 'This is your website description'
         );
      
      loadview('header',$meta); //load heade
      ?>
      
      <section>
          <div id="content">
              <p>Page Content</p>
          </div>
      
      </section>
      <?php
      
          loadview("footer"); //load footer
      ?>
      

      关于页面

      <?php
          include 'includes/functions.php';
      $meta = array(
              'pagetitle' => 'About Us',
              'pagedescription' => 'This is about page'
         );
      
      loadview('header',$meta);
      ?>
      
      <section>
          <div id="content">
              <p>Page Content</p>
          </div>
      
      </section>
      <!-- load footer -->
      <?php
      
          loadview("footer");
      ?>
      

      希望这能让您了解如何实现目标,有很多方法可以实现。

      如果您需要任何帮助,请告诉我

      【讨论】:

        猜你喜欢
        • 2020-06-03
        • 1970-01-01
        • 2013-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多