Php 如何重用页脚的html
我看到了许多关于创建php文件以重用页眉/页脚的答案,但都不够具体。我似乎无法让它工作 php文件到底是什么样的,我的html文件到底是什么样的(下面给出了当前的代码)?我是否必须将所有html文件转换为php文件才能使用php包含行Php 如何重用页脚的html,php,html,footer,reusability,Php,Html,Footer,Reusability,我看到了许多关于创建php文件以重用页眉/页脚的答案,但都不够具体。我似乎无法让它工作 php文件到底是什么样的,我的html文件到底是什么样的(下面给出了当前的代码)?我是否必须将所有html文件转换为php文件才能使用php包含行 <div id="footer2-wrap"> <div class="container"> <table id="header"> <tr>
<div id="footer2-wrap">
<div class="container">
<table id="header">
<tr>
<td class="phone-number"><span class='wsite-text wsite-phone'>Copyright 2015 | xxx Corporation</span></td>
</tr>
</table>
</div>
版权所有2015 | xxx公司
您应该使用PHP中的include()、require()或require_once函数来包含您的文件,这取决于您的情况 例如,假设您有一个名为index.php的基本php文件,并且希望添加sidebar.php、footer.php、navigation.php index.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<?php
include("sidebar.php");
include("navigation.php");
include("footer.php"); // we include footer.php here. you can use .html extension, too.
?>
</body>
</html>
footer.php(或html)
您应该使用PHP中的include()、require()或require_once函数来包含您的文件,这取决于您的情况 例如,假设您有一个名为index.php的基本php文件,并且希望添加sidebar.php、footer.php、navigation.php index.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<?php
include("sidebar.php");
include("navigation.php");
include("footer.php"); // we include footer.php here. you can use .html extension, too.
?>
</body>
</html>
footer.php(或html)
使用页脚数据创建一个新文件。让我们给它命名:
footer.php
:
<div id="footer2-wrap">
<div class="container">copyright etc...</div>
<div>
使用页脚数据创建一个新文件。让我们给它命名:
footer.php
:
<div id="footer2-wrap">
<div class="container">copyright etc...</div>
<div>
是的,您必须有一个
.php
文件才能使用php,您的服务器也必须安装了php(但大多数已经安装了)。PHP还增加了页面的加载时间,所以在使用它时要考虑到这一点。要使用PHP添加页脚,您可以使用PHP函数,或者,我不确定这是否正确,使用:
include()
:
file-get-contents()
:
您也可以使用JavaScript执行相同的操作:
var xmlhttp, text;
xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', 'http://www.example.com/file.txt', false);
xmlhttp.send();
text = xmlhttp.responseText;
document.getElementById("footer").innerHTML=text;
代码取自
注意:该文件必须位于同一域才能使用JS。是的,您必须有
.php
文件才能使用php,您的服务器也必须安装php(但大多数已经安装)。PHP还增加了页面的加载时间,所以在使用它时要考虑到这一点。要使用PHP添加页脚,您可以使用PHP函数,或者,我不确定这是否正确,使用:
include()
:
file-get-contents()
:
您也可以使用JavaScript执行相同的操作:
var xmlhttp, text;
xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', 'http://www.example.com/file.txt', false);
xmlhttp.send();
text = xmlhttp.responseText;
document.getElementById("footer").innerHTML=text;
代码取自
注意:要使用JS,该文件必须位于同一域。首先创建一个名为menu.php的文件(如果使用.html,则无法使用) 在该php文件中,为菜单编写html代码
<div id="navbar">
more code
</div>
At your main html file write
<?php
include("menu.php");
?>
更多代码
在主html文件中写入
首先创建一个名为menu.php的文件(如果使用.html,则无法使用)
在该php文件中,为菜单编写html代码
<div id="navbar">
more code
</div>
At your main html file write
<?php
include("menu.php");
?>
更多代码
在主html文件中写入
代码应该能够运行。确保您正在通过Apache运行代码。下载Xammp或wammp。启动Apache,将项目文件夹复制粘贴到httdocs下的xammp文件夹中。然后在浏览器上键入localhost/[您的项目名称]
,并确保html
文件更改为.php
,代码应该能够运行。确保您正在通过Apache运行代码。下载Xammp或wammp。启动Apache,将项目文件夹复制粘贴到httdocs下的xammp文件夹中。然后在浏览器上键入localhost/[您的项目名称]
,并确保html
文件更改为.php
将html文件更改为php文件,包括它或使用细枝并渲染它您不必将html文件重命名为php,尽管这是一种解决方案。但是,您的web服务器需要配置为将html文件视为PHP,以便包含工作。对不起,你们能详细说明一下吗?我不知道twig是什么,它需要什么样的渲染,或者如何配置我的web服务器以将html文件视为php。。。php文件是什么样子的?是否从开始?将html文件更改为php文件包含它或使用twig并呈现它您不必将html文件重命名为php,尽管这是一种解决方案。但是,您的web服务器需要配置为将html文件视为PHP,以便包含工作。对不起,你们能详细说明一下吗?我不知道twig是什么,它需要什么样的渲染,或者如何配置我的web服务器以将html文件视为php。。。php文件是什么样子的?它是以?开始的吗?因此,我创建了footer.php文件,完全如上图所示,并从index.php文件中删除了该文本,并将其替换为“include_once('footer.php');”(再次,完全如上图所示)。现在在页面上,页脚消失了(正如预期的那样),文本“include_once('footer.php');”出现在页面底部……因此我创建了footer.php文件,完全如上图所示,并从index.php文件中删除了该文本,并将其替换为“include_once('footer.php');(同样,如上所示)。现在在页面上,页脚消失了(正如预期的那样),文本“include_once('footer.php');”出现在页面底部……我也尝试了这个方法;什么都没有出现。。。我查看了源代码,由于一些奇怪的原因,我的php代码行被自动注释掉了……我也尝试过这样做;什么都没有出现。。。我查看了源代码,我的php行由于一些奇怪的原因被自动注释掉了。。。