我正在用html和css构建一个站点,我想做一个index.php来调用页面的所有部分。例如调用页眉和页脚,当单击nav链接替换相同页面中的内容时,我是否可以使用php或我的选项。
我的index.php示例
Get header < ? php include("header.html"); ? >
Replace Content < div class=" MainContent "> < /div>
Get Footer < ? php include(" footer.html ") ;?>发布于 2016-02-11 11:58:56
Ditn没有进行测试,也没有在代码中进行任何验证,但我希望您能够了解如何做到这一点。只有index.php文件。如果有任何查询
header.php导航
<html>
<head></head>
<body>
navigation
<a href="mysite?page=home">home </a>
<a href="mysite?page=aboutus">About us </a>page.php
page.php
$page_name = $_GET['page'];
<?php include( $_SERVER['DOCUMENT_ROOT'] . "/path_or_folder_offile/".$page_name.".php" ); ?>footer.php
</body>
</html>index.php
<?php include('header.php'); ?>
<?php include('page.php'); ?>
<?php include('footer.php'); ?>发布于 2016-02-11 11:55:25
这是我常用的index.php页面布局
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Page Title</title>
<link rel="stylesheet" href="css/style.css" type="text/css">
</head>
<body>
<div id="page">
<?php include_once('header.php'); ?>
<?php if (isset($_GET['page']) && !empty($_GET['page'])) { include_once('/pages/' . $_GET['page'] . '.php'); } else { include_once('/pages/index.php'); } ?>
<?php include_once('footer.php'); ?>
</div>
</body>
</html>这使用了一个GET变量(?page=pageName)来选择页面,我通常在Apache中使用该页面的重定向规则。
或者,您可以从数据库中填充div内容。例如:
<div id="content">
$pageId = $this->getPageId();
$db = new mysqli_connect('','','','');
$stmt = db->prepare("SELECT pageContent FROM pages WHERE page_id='$pageId'");
$stmt->execute();
$stmt->bind_result($curPageContent);
$stmt->fetch();
$stmt->close();
echo $curPageContent;
</div>对于ajax调用,可以这样做:
$.get( "/get_page/",
{ page_id: '<?php echo $_GET['page']; ?>' })
.done(function(data) {
$('#content').html(data);
});为此使用的文件夹结构设置如下:
root/
css/
js/
pages/
index.php
header.php
footer.php
index.php显然,您可以使用任何您想要的结构,但这可能会有所帮助(尽管assets/文件夹通常很好:)
祝好运。
https://stackoverflow.com/questions/35338440
复制相似问题