lear_all_cache();//清除所有缓存
//smarty.php
//create html file use smarty template
//Rossy.cn@gmial.com
//2007-07-27 11:15
require_once("classes/Smarty.class.php"); //smarty文件
require_once("config.php"); //配置文件
require_once("mysql_class.php"); //数据库连接文件
$smarty = new Smarty();
$smarty->template_dir = "templates";
$smarty->compile_dir ="templates_c";
$smarty->left_delimiter = "<{";
$smarty->right_delimiter ="}>";
$page_count = "3"; //每一页的记录数
$Db = new Mysql;
$query = $Db->Query("Select * FROM Tablea orDER BYId DESC");
$tatol = $Db->FetchCount($query);
$pages = ceil($tatol / $page_count);
for ($j=1;$j<=$pages;$j++)
$showpages.="
for ($i = 0;$i<$pages; $i++)
{
$start_page = $i *$page_count;
if($pages>1)
{
$indexpath ="user_".($i+1).".htm"; //filename
$showpagestr= "
}
else
{
$indexpath = "user.htm"; //file name
$showpagestr= "";
}
$sql = "Select * FROM TableaorDER BY ID DESC LIMIT $start_page,$page_count";
$query =$Db->Query($sql) or die("Invalid Query");
if(($Db->FetchCount($query))<1){
$Admin ="數據庫沒有此數據";
}else{
while ($rs =$Db->FetchArray($query)) {
$Admin[] =array('Id'=>$rs['Id'],'Name'=>$rs['Name'],'Nick'=>$rs['Username'],'Type'=>$rs['Type'],'Email'=>$rs['Email']);
}
}
$smarty->assign(array('Site_Name'=>"系统",'AdminName'=>'ybfqlyq','AdminType'=>'Administrator','Type'=>'1'));
$smarty->assign('Admin',$Admin);
$smarty->assign('Page_Str',$showpagestr);
$temp =$smarty->fetch('test.tpl', null, null, false);//模板文件
unset($Admin);
$showpagestr = "";
$fp=fopen($indexpath,'w');
fwrite($fp,$temp) ordie('create failed');
}
$Db->Close();
echo "create htm file successfully";
?>
test.tpl文件代码:
href="templates/css.css" type=text/cssrel=stylesheet>
cellSpacing=0 cellPadding=2 width="100%"align=center border=1>
編號
用戶名
姓名
類別
郵件地址
<{sectionname=adminlist loop=$Admin}>
<{/section}>
<{$Page_Str}>
$smartyArr = $smarty->get_template_vars();
//记录总数,每页显示记录条数,总页数
$pager_Total= count($smartyArr['productID']);
$pager_Size= 10;
$pager_Number = ceil($pager_Total/$pager_Size);
$pager_URL ="index.php?action=View";
//当前页的页数,从REQUEST获得
if(isset($_GET['pager_PageID'])&&!empty($_GET['pager_PageID'])){
$pager_PageID = intval($_GET['pager_PageID']);
}else{
//第一次访问
$pager_PageID = 1;
}
//每页的起,始记录数
if($pager_PageID == 1 ) {
$pager_StartNum = 0;
} else{
$pager_StartNum = ($pager_PageID -1) *$pager_Size;
}
$pager_EndNum = $pager_StartNum + $pager_Size;
if($pager_PageID == 1 &&$pager_Number>1) {
//第一页
$pager_Links= "上一页 |
}elseif($pager_PageID == $pager_Number&& $pager_Number>1){
//最后一页
$pager_Links = "
} elseif($pager_PageID > 1&& $pager_PageID <=$pager_Number) {
//中间
$pager_Links = "
} else{
$pager_Links = "上一页 | 下一页";
}
$smarty->assign('pager_Total',$pager_Total);
$smarty->assign('pager_StartNum',$pager_StartNum);
$smarty->assign('pager_EndNum',$pager_EndNum);
$smarty->assign('pager_Links',$pager_Links);
return$smarty->fetch("list.tpl");
?>
include "class/Smarty.class.php";
$tpl = new Smarty();
$tpl->config_dir = __SITE_ROOT . "/configs/";
$tpl->cache_dir = __SITE_ROOT . "/cache/";
$tpl->left_delimiter = '<{';
$tpl->right_delimiter = '}>';
?>
$page_count = "3"; //每一页的记录数
$tatol=$aa->selectpage("news","","");
$pages = ceil($tatol / $page_count);
if($tatol<2)
{
echo "首页 / 上一页/ 下一页 / 末页";
}
if($tatol>2)
{
$p=2;
$e=0;
$name="首页 /上一页 /
for($pi=0;$pi<=$tatol;$pi++)
{
$start_page = $pi *$page_count;
if(($pi+1)%$page_count==0)
{
$p--;
//生成HTML页面
echo "
".$name;
$sql ="Select * FROM news orDER BY ID DESC LIMIT$start_page,$page_count";
// $query =$Db->Query($sql) or die("Invalid Query");
$query=$aa->query($sql) ordie("sql error");
//if(($Db->FetchCount($query))<1){
// $Admin = "數據庫沒有此數據";
// }else{
while ($rs = mysql_fetch_array($query)) {
$Admin[] =array('Id'=>$rs['Id'],'title'=>$rs['title'],'content'=>$rs['content']);
}
//}
$tpl->assign(array('Site_Name'=>"系统",'AdminName'=>'ybfqlyq','AdminType'=>'Administrator','Type'=>'1'));
$tpl->assign('Admin',$Admin);
$tpl->assign('Page_Str',$name);
$temp =$tpl->fetch('test.tpl', null, null, false);//模板文件
unset($Admin);
///unset($name);
// $showpagestr = "";
$fp=fopen("news".$p.".html",'w');
fwrite($fp,$temp) or die('create failed');
$name="";
$name.="
$name.="
$p++;
$p++;
if($p<>$pages+1)
{
$name.="
$name.="
}
else
{
$name.="下一页 / 末页";
}
}
}
if($tatol%$page_count<>0)
{
$name="
}
}
//这是生成另一个的代码
//for ($j=1;$j<=$pages;$j++)
// $showpages.= "
//
//for ($i = 0;$i<$pages; $i++)
//{
// $start_page = $i *$page_count;
// if($pages>1)
// {
// $indexpath ="user_".($i+1).".htm"; //file name
// $showpagestr = "
// }
// else
// {
// $indexpath = "user.htm"; //filename
// $showpagestr = "";
// }
//
// $sql ="Select * FROM news orDER BY ID DESC LIMIT$start_page,$page_count";
// // $query =$Db->Query($sql) or die("Invalid Query");
// $query=$aa->query($sql) ordie("sql error");
// //if(($Db->FetchCount($query))<1){
//// $Admin = "數據庫沒有此數據";
//// }else{
// while ($rs = mysql_fetch_array($query)) {
// $Admin[] =array('Id'=>$rs['Id'],'title'=>$rs['title'],'content'=>$rs['content']);
// }
// //}
//
// $tpl->assign(array('Site_Name'=>"系统",'AdminName'=>'ybfqlyq','AdminType'=>'Administrator','Type'=>'1'));
// $tpl->assign('Admin',$Admin);
// $tpl->assign('Page_Str',$showpagestr);
// $temp =$tpl->fetch('test.tpl', null, null, false);//模板文件
// unset($Admin);
// unset($showpagestr);
// $showpagestr = "";
// $fp=fopen($indexpath,'w');
// fwrite($fp,$temp) or die('create failed');
//}
echo "create htm file successfully";
?>
以上就是利用PHP与Smarty模板技术生成分页静态页面的代码,希望可以帮助大家,更多相关内容请关注PHP中文网(m.sbmmt.com)