php - 使用 PHP 的动态导航包括

标签 php dynamic include nav

我对如何动态更改网页中显示的内容有疑问。 我修复了网站的几个部分 - 页眉、导航、页脚、启动画面和侧边栏。

我只希望我网站的中间部分根据用户点击的菜单链接进行更改。

下面是我的 index.php 代码

<?php
include "/templates/header.php";
include "templates/menu.php";
include "/templates/splash.php";
$action = "index"; 
$disallowed_paths = array('header','menu','splash','bottom_page', 'footer'); 
if (!empty($_GET['action'])) 
{ 
    $tmp_action = basename($_GET['action']); 
       if (!in_array($tmp_action, $disallowed_paths) && file_exists("/content/$tmp_action.php")) 
        $action = $tmp_action; 
} 
include "/content/$action.php";  
include "/templates/bottom_page.php";
include "/templates/footer.php";
?>

我的menu.php 包含主页关于产品服务登录链接 我只想将 ma​​in_index.php 更改为 include以上基于用户点击的内容。

请告知这种方法是否好。 或者我应该根据在菜单上单击的链接多次创建与 index.php 类似的文件并包含到每个文件

最佳答案

你的答案是

GET method

您可以为此使用get 方法

 <ul>
        <li><a href="?page=example_1">example 1</a></li>
        <li><a href="?page=example_2">example 2</a></li>
        <li><a href="?page=example_3">example 3</a></li>
        <li><a href="?page=example_4">example 4</a></li>
</ul>

    After User Clicks on the link 

    <?php 
        if(isset($_GET['page']) && $_GET['page']!=""){
            $page = "";
            switch ($_GET['page']) {
                case 'example_1':
                    $page = "Page_1.php";
                    break;

                case 'example_2':
                    $page = "Page_2.php";
                    break;

                case 'example_3':
                    $page = "Page_3.php";
                    break;

                case 'example_4':
                    $page = "Page_4.php";
                    break;

                default:
                    $page = "any_default_page.php";
                    break;
            }

            include($page);
        }
     ?>

还有其他方法。但这是最简单高效的

关于php - 使用 PHP 的动态导航包括,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31341637/

相关文章:

php - 无法更改 DATETIME 格式

python - 在 Tkinter 中动态创建函数和绑定(bind)按钮

php - PHP 中的命名空间声明 (5.3.5)

powershell - 如何在Powershell中以递归模式查找包含字符和扩展名的文件?

发送邮件时来自字段的 PHP()

javascript - 如何统计过滤后显示的表格行数?

php - 关闭 PHP 和 MySQL 上的警告和错误

c++ - 使用包含类的动态实例化后调用 C++ 重载运算符 [] 似乎不起作用

C# 4 表达式树中的 "dynamic"

php - 缓存包含在 PHP 中以供迭代重用