簡體   English   中英

如何在網站的索引頁面中加載其他頁面

[英]how to load different page in a index page of website

我正在嘗試建立一個具有不同頁面的網站。 我是PHP的新手,我有一個包含某些表的索引頁,用於保存頁眉,導航菜單,主體,側邊欄和頁腳。 索引頁使用include("filename.extension");附加到其中的所有上述元素include("filename.extension"); 問題是我試圖在更改菜單時動態加載網站的主體,即網站內容。

下面是我的代碼,對此的任何建議都是非常可取的。 提前致謝。

<body>
<div> <?php Include('header.php'); </div>

<div id="menu" align="center">
    <table width="790" height="35">
        <tr>
            <td>Home</td>
            <td><a href="pages/students/reg.php">Register</a></td> // this page need to display on the same window with other elements, after click.
        </tr>
    </tabel>
</div>

<div id="sidebar" align="right"> <?php include ("sidebar.php");?> </div>

<div id="Content ">
    <?php include("FILE"); // here i need to display the hyper linked page.?>
</div>

<div id="footer"> <?php include("footer.php");?> </div>
</body> `

猜Diogo的答案將使您獲得想要的東西。 像這樣創建您的index.php文件。

//menu area 
<div id="menuWrap">
  <a href="?page=page1">link to page1</a>
  <a href="?page=page2">link to page2</a>
</div>

//side bar
<div id="sidebarWrap">...</div>

//content area
<div id="contentWrap">
<?php
switch($_GET['page']) 
{
    case 'page1':
        include '/pages/page1.php';
        break;
    case 'page2':
        include '/pages/page2.php';
        break;
    default:
        include '/pages/notfound.php';

}
?>
</div>

//footer
<div id="footerWrap">...</div>

這樣,您可以在同一窗口中顯示站點中的其他頁面,只有內容區域會更改

更新:

我已經糾正了您的輸入錯誤,這是根據您的代碼段的答案。

<body>
<div>
    <?php Include('header.php'); ?>
</div>
<div id="menu" align="center">
    <table width="790" height="35">
        <tr>
            <td><a href="?page=page1">Home</a></td>
            <td><a href="?page=page2">Register</a></td>
        </tr>
    </table>
</div>
<div id="sidebar" align="right">
    <?php include ("sidebar.php"); ?>
</div>
<div id="Content ">
    <?php
        switch($_GET['page']) 
    {
        case 'page1':
            include '/pages/home.php';//file path of your home page
            break;
        case 'page2':
            include '/pages/students/reg.php';
            break;
        default:
            include '/pages/notfound.php';

    }
    ?>
</div>
<div id="footer">
    <?php include("footer.php");?>
</div>
</body>

首先,創建一個名稱完全相同的文件:(包括點)

.htaccess

並將其放在索引的同一文件夾中。

在.htaccess中,編寫以下代碼:

# .htaccess mod_rewrite

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]

在這種情況下,.htaccess文件將重寫URL來更改http://127.0.0.1/index.php?page=register以接受“博客漂亮的URL” http://127.0.0.1/register

在index.php文件夾中,創建一個新文件夾“ pages”

在文件夾頁面內,創建4個簡單的PHP文件(home.php,register.php,anyother.php和notfound.php),其內容類似於

<?php
   echo "I'm the Register Page.";
?>

這將是基於您的代碼的索引示例。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8"><title>¨title</title></head>
<body>
    <div>
        <?php include 'header.php'; ?>
    </div>
    <div id="menu" align="center">
        <table width="790" height="35">
            <tr>
                <td><a href="/home">Home</a></td>
                <td><a href="/register">Register</a></td>
                <td><a href="/anyother">AnyOther</a></td>
                <td><a href="/misstypo" title="This will lead to the notfound.php">Broken Sample</a></td>
            </tr>
        </table>
    </div>
    <div id="sidebar" align="right">
        <?php include 'sidebar.php'; ?>
    </div>
    <div id="Content ">
        <?php
            if(!isset($_GET['page']) || $_GET['page'] == ''){
                $page = 'home'; //If no page specified
            } else {
                $page = $_GET['page'];
            }

            switch($page)
            {
                case 'home':
                    include '/pages/home.php';//file path of your home page
                    break;
                case 'register':
                    include '/pages/register.php';
                    break;
                case 'anyother':
                    include '/pages/anyother.php';
                    break;
                default:
                    include '/pages/notfound.php'; //If any page that doesn't exists, then get back to home.

            }
        ?>
    </div>
    <div id="footer">
        <?php include("footer.php");?>
    </div>
</body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM