如何在模板中动态加载内容

时间:2014-02-06 12:13:34

标签: php html

我有6个文件。

  1. 的header.php
  2. footer.php
  3. home.php
  4. page1.php中
  5. 使page2.php
  6. 的index.php
  7. 的header.php

    <html>
    <head>
    <title>test</title>
    </head>
    <body>
    <div class="menu">
     <ul>
    <li><a href="home.php>Home</a></li>
    <li><a href="page1.php>Page 1</a></li>
    <li><a href="page2.php>Page 2</a></li>
    </ul>
    </div>
    
    <div class="content">
    

    footer.php

    </div>
    </body>
    </html>
    

    的index.php

    <?php
    
        include "header.php";
        //content goes here
        include "home.php";  //this include must change when i click on page 1 or page 2 link    
        //content goes here
        include "footer.php";
    
    ?>
    

    当我点击链接时,如何动态更改index.php的内容?

2 个答案:

答案 0 :(得分:2)

主要结构:

index.php --> Layout + handles which page to display
header.php --> Included in index
footer.php --> Include in index

链接类似于index.php?page=home

的index.php:

<html>
    <head>
        <title></title>
    </head>
    <body>

    <?php include 'header.php'; ?>

    <?php 

       // Handle here what page to include : 
       // - Store $_GET['page']
       // - Sanitize the var
       // - Check if you have a file that would correspond to this page
       // - Include it

    ?>

    <?php include 'footer.php'; ?>

    </body>
</html>

header.php:

<header>
    ....
</header>

footer.php:

<footer>
 .....
</footer>

答案 1 :(得分:-2)

每个网址都需要不同的脚本:

<强>的index.php

<?php

    include "header.php";
    //content goes here
    include "home.php";  
    //content goes here
    include "footer.php";

?>

<强> page1.php中

<?php

    include "header.php";
    //content goes here
    include "page1_content.php"; //contains contents of page1
    //content goes here
    include "footer.php";

?>

<强>使page2.php

<?php

    include "header.php";
    //content goes here
    include "page2_content.php"; //contains contents of page2
    //content goes here
    include "footer.php";

?>