抛砖引玉_PHPlib类公共文件的使用方法
本想把这个帖子放到每周一题中去,赚点分,后来想想算了,没有什么技术含量的东西,就放这吧。
根据所长的帖子,这两天研究了一下phplib模板类(其实现在还不太清楚自己是不是将概念的东西搞清楚)。前后写了四个简单的程序。收获还不小:1、理解了为什么书建议将网页程序放到WWW文件下面;2、弄明白了为什么CONN文件中修改编码字符的语句可以随便放,而connect函数在页面中时却不行的原理。现在又碰到了一个问题,因为那个帖子讲的不明白,自己修改了一下,最后是做好了,但不知道好不好用,好在哪里,不好又在哪里,所以放出来,显一下,好听听大家的意见(原例是所长帖子中的最后一个例子,公共文件的解决办法,我按照那个办法,发现头和尾不能解决,就自己创造了下面的办法,别拿砖砸我,用玉就行了)。
1.main.html
[code=php]
<?php
include "header.html";
?>
<html>
<head>
<title>{title}</title>
</head>
<ul>
<!-- BEGIN list -->
<li> 编号为 {id}的品名为 {name},其属于第 {kind} 品种大类!</li>
<!-- END list -->
</ul>
<?php
include "foot.html";
?>
[/code]
2.header.html
[code=php]
{header}
<body>
[/code]
3.foot.html
[code=php]
{footer}
<div align="center">
<input type="button" name="back" value="Back" onclick=window.location="index.php"></div>
</body>
</body>
</html>
[/code]
4.main.php
[code=php]
<?php
include('template.inc');
$tp3=new Template;
$tp3->set_file('main','header.html');
$tp3->set_var('header','Here are starting....');
$tp3->parse('mains','main');
$tp3->p('mains');
$tp1=new Template;
$tp1->set_file('main','main.html');
$tp1->set_var('title','我是一个PHP菜鸟!');
$tp1->set_block('main','list','stufflist');
$link=mysql_connect('localhost','root','whb1978');
mysql_query("SET NAMES 'UTF8'");
$result=mysql_db_query('kxoa','select * from stuff');
while($w=mysql_fetch_array($result))
{
$tp1->set_var('id',$w[ID]);
$tp1->set_var('name',$w[name]);
$tp1->set_var('kind',$w[kind_id]);
$tp1->parse('stufflist','list',true);
}
$tp1->parse('header','my_header');
$tp1->parse('footer','my_footer');
$tp1->parse('mains','main');
$tp1->p('mains');
$tp2=new Template;
$tp2->set_file('main','foot.html');
$tp2->set_var('footer','Just a testing.....');
$tp2->parse('mains','main');
$tp2->p('mains');
?>
[/code]