网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > PHP技巧
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.PEAR应用之AUTH_HTTP.
.PHP中XML-RPC构造Web Service.
.复杂检索数据并分页显示的处理方.
.使用php4加速网络传输 .
.PHP+APACHE实现用户论证的方法.
.用PHP的Socket建立自己的聊天室服.
.COM in PHP (winows only).
.Smarty的保留变量问题.
.自定义PHP分页函数.
.PHP漏洞中的战争.
.PHPMailer:Featured email trans.
.PHP与SQL注入攻击[二] .
.解决phpMyAdmin2.6以上版本数据乱.
.初学者学习PHP开发应该掌握的几段.
.通过实例深入剖析require和inclu.
.如何实现给定日期的若干天以后的.
.如何去掉文章里的 html 语法&nbs.
.PHP读取文件夹内有多少个文件.
.如何在PHP中使用Oracle数据库(5).
.提升PHP速度全攻略.

Smarty中批量生成html的方法

发表日期:2008-12-16


经过一番摸索,终于找到方法,写出来大家指点一下:
首先在smartt.class.php中为Smarty类添加这么一个方法:
PHP代码:
//参数一:html文件保存路径,参数2:写入的内容
function MakeHtmlFile($file_name, $c)

      if(!$fp = fopen($file_name, "wa"))
      {    
         echo "文件打开失败!";    
         return false;    
      }    
      if(!fwrite($fp, $c))
      {    
         echo "文件写入失败!";    
         fclose($fp);    
         return false;    
      }            
      fclose($fp);    
   }


问题:我的所有文章都调用的是news.tpl这个模板,那么究竟如何批量生成呢?

我们先看看news.php
PHP代码:

<?php
include_once("config.php");
include_once("init.php");
$s->assign("title","所有的新闻分类");
$ID=$_GET["ID"]+0;
$sql="select * from artical where newsID=$ID";
$rs=$db->fetch($sql);
$s->assign("news",$rs["rec"][0]);//注意:$rs["rec"][0]是个数组
$s->display("news.html");
?>


整个模板变量就只有$news,这么写有什么好处呢?我可以以数组的方式读取内容

那么究竟如何生成:
看如下代码:
很简单的
PHP代码:

<?php
include_once("config.php");
include_once("init.php");
$sql="select * from artical";
$rs=$db->fetch($sql);
foreach ($rs["rec"] as $k=>$v)
{
$s->assign("news",$v);
$s->MakeHtmlFile("./news/news_".$v[0].".html",$s->fetch("news.html",null, null, false));
}
?>


很简单把!


PHP代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[url=http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd[/url]">
<html xmlns="[url=http://www.w3.org/1999/xhtml]http://www.w3.org/1999/xhtml[/url]">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title><{$news.titles}></title>
<link href="<{$docroot}>/CSS/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0" style="border:solid #CCCCCC 1px">
  <tr>
    <td height="25" colspan="6" align="center" bgcolor="#eeeeee"><strong><{$news.titles}></strong></td>
  </tr>
  <tr>
    <td width="74" height="25" align="right">作者:</td>
    <td width="220"><{$news.author}></td>
    <td width="40" align="right">时间:</td>
    <td width="134" align="left"><{$news.sj}></td>
    <td width="110" align="right">关键词:</td>
    <td width="220" align="left"><{$news.keyword}></td>
  </tr>
  <tr>
    <td height="25" align="right" >核心提示:</td>
    <td height="25" colspan="5" class="artical" style="border:double #FF0000 3px;"><{$news.sumary}></td>
  </tr>
  <tr>
    <td height="25" colspan="6" align="left" class="artical" style="padding:10px;"><{$news.contents}></td>
  </tr>
</table>
</body>
</html>


总结:主要使用smarty的fetch方法,和文件的读写操作

上一篇:数据抽象层->ADODB入门基础 人气:55
下一篇:一个简单的模板类--编译型的(simplet类增强后) 人气:61
浏览全部PHP的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐