我对如何动态更改网页中显示的内容有疑问。 我修复了网站的几个部分 - 页眉、导航、页脚、启动画面和侧边栏。
我只希望我网站的中间部分根据用户点击的菜单链接进行更改。
下面是我的 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 包含主页、关于、产品、服务的链接strong> 和登录链接
我只想将 main_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/