Jav*_*aaa 2 php layout include hyperlink
好的,所以我第一次建立一个完整的网站并使用PHP.现在我遇到了一个问题:
让我们说我的网站只是一个带有菜单和内容区域的标题.当然,我想有一个header.php和几个内容文件,例如content1.php content2.php和content3.php.这样您只需要更改1个文件中的菜单,就像您所理解的那样.
那么如何建立网站是最好的:
A.在每个内容文件中添加这样的内容:
<?php include 'header.php'; ?>
here the content of the content page 1
Run Code Online (Sandbox Code Playgroud)
B.制作一个索引文件,例如:
<?php include 'header.php'; ?>
<?php include 'content1.php'; ?>
Run Code Online (Sandbox Code Playgroud)
那么如何在菜单中单击指向content2.php的链接时,标题仍然在该页面上呢?
C.别的什么?也许是一个关于如何制作这类页面的好教程?
如果您希望最大化重用代码/元素的好处,那么您可以选择第二个选项:
B.制作一个索引文件,例如:
<?php include 'header.php'; ?>
<?php include 'content1.php'; ?>
Run Code Online (Sandbox Code Playgroud)
那么如何在菜单中单击指向content2.php的链接时,标题仍然在该页面上呢?
这是如何(一个简单的例子):
index.php使用查询字符串通过您的脚本路由所有类似的请求(content1,2,3)- mod_rewrite可以使这个很漂亮.然后根据请求提供主要内容部分.
例如一个链接:
<a href='index.php?page=content1'>Content 1</a>
Run Code Online (Sandbox Code Playgroud)
并检测要服务的内容:
<php
$pages['content1'] = 'content1.php';
$pages['content2'] = 'content2.php';
$pages['default'] = $pages['content1']; //set default content
$page = 'default';
if(isset($pages[$_GET['page']]){
$page = $pages[$_GET['page']]; //make sure the filename is clean
}
?>
<?php include 'header.php'; //header here?>
<?php include $page; //correct content here?>
Run Code Online (Sandbox Code Playgroud)
不仅有一个地方可以更改标题,但现在只有一个地方可以更改整个布局.
当然这只是一个简单的例子,有许多PHP框架可以为您完成所有这些(使用MVC).