在PHP上使用模板
我为我的网站创建了一个模板文件...就像
i create a template file for my site... it's like:
<!-- template.php -->
<?php function showheader() { ?>
<head><body>
<!-- some of style files and menus -->
<div class="content">
<?php } ?>
<?php function showfooter() { ?>
</div></body></html>
<?php } ?>
我将此文件用作这样的模板:
i use this file as a template like this:
<?php include_once("template.php"); showheader(); ?>
content text or photo or ... etc.
<?php showfooter(); ?>
仅此而已...但是,如果我尝试在模板文件上使用连接,那就搞砸了! 我使用了一个外部文件,例如:
that's all... but if i try to use a connection on template file, it screw up! i used an external file like:
<?php
//
// include_once connection file
// query strings goes here
//
do {
echo $row_table['id']; //example
} while ($row_table = mysql_fetch_assoc($table));
?>
并且我将此文件用作include_once("filename.php");模板文件上的内容...此时会出现错误...诸如此连接变量是什么,此连接字符串是什么...等等.它无法到达连接字符串...
and i use this file as include_once("filename.php"); on template file... at this point it gives errors... like what is this connection variable, what is this connection string... etc. it cannot reach connection strings...
顺便说一句,我使用了另一个外部连接,例如:
by the way, i use another external connection like:
<?php
global $hostname_conn,$database_conn,$username_conn,$password_conn,$conn;
$hostname_conn = "localhost";
$database_conn = "test";
$username_conn = "****";
$password_conn = "****";
$conn = mysql_pconnect($hostname_conn, $username_conn, $password_conn) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_query("SET NAMES 'utf8'");
?>
我要哭了!有什么问题...您知道使用模板的另一种方法吗... 非常感谢...
i'm gonna cry! what's the problem... and do you know another way to use template... thanks much...
PS:我将conn.php上的变量更改为全局变量(并且没有起作用),并且更改了include,include_once,require,require_once的位置,其中我包含了文件,但没有给出任何内容.
PS: i change variables on conn.php as global (and it didnt work) and i change include, include_once, require, require_once where i include files but it didnt give anything.
这会将页面分为两个PHP文件:(1)第一个获取数据,(2)第二个显示数据.
This separates the page into two PHP files: (1) the first obtains the data, and (2) the second displays the data.
在获取数据时,不应打印单个字符.
如果发生某些错误,请显示错误页面.
While getting data, not a single character should be printed out.
If some errors occurred, display an error page.
一旦您获得所有数据而没有错误-是时候添加模板了.该模板还具有两个PHP文件:该页面本身的模板以及该站点中所有页面共同共享的模板.
Once you get all your data with no errors - it's time to include a template. The template has two PHP files as well: the template for the page itself and the template that is shared in common by all the pages in the site.
通过这种方式进行分类,您将解决当前和将来的所有模板问题.
By sorting things this way you'll solve all your present and future templating problems.
典型的脚本可能看起来像
A typical script may look like
<?
//include our settings, connect to database etc.
include dirname($_SERVER['DOCUMENT_ROOT']).'/cfg/settings.php';
//getting required data
$DATA=dbgetarr("SELECT * FROM links");
$pagetitle = "Links to friend sites";
//etc
//and then call a template:
$tpl = "links.php";
include "template.php";
?>
其中template.php
是您的主要网站模板,包括公共部分,例如页眉,页脚,菜单等:
where template.php
is your main site template, including common parts, like header, footer, menu etc:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My site. <?=$pagetitle?></title>
</head>
<body>
<div id="page">
<? include $tpl ?>
</div>
</body>
</html>
和links.php
是实际的页面模板:
<h2><?=$pagetitle?></h2>
<ul>
<? foreach($DATA as $row): ?>
<li><a href="<?=$row['link']?>" target="_blank"><?=$row['name']?></a></li>
<? endforeach ?>
<ul>
简单,干净和可维护.
settings.php包含所有常用设置:
settings.php contains all common settings:
<?php
$hostname_conn,$database_conn,$username_conn,$password_conn,$conn;
$hostname_conn = "localhost";
$database_conn = "test";
$username_conn = "****";
$password_conn = "****";
$conn = mysql_connect($hostname_conn, $username_conn, $password_conn)
or trigger_error(mysql_error(),E_USER_ERROR);
mysql_query("SET NAMES 'utf8'") or trigger_error(mysql_error(),E_USER_ERROR);
$tpl = "default.php";
$pagetitle = "";
function dbgetarr(){
$a = array();
$args = func_get_args();
$query = array_shift($args);
$query = str_replace("%s","'%s'",$query);
foreach ($args as $key => $val) {
$args[$key] = mysql_real_escape_string($val);
}
$query = vsprintf($query, $args);
$res = mysql_query($query);
if (!$res) {
trigger_error("dbget: ".mysql_error()." in ".$query);
} else {
while($row = mysql_fetch_assoc($res)) $a[]=$row;
}
return $a;
}
?>