欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

利用PHP与Smarty模板技术生成分页静态页面的代码

程序员文章站 2022-05-15 16:19:14
...
lear_all_cache();//清除所有缓存

<?php
//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.="<ahref=\"user_".$j.".htm\">".$j."</a>";

for ($i = 0;$i<$pages; $i++)
{
$start_page = $i *$page_count;
if($pages>1)
{
$indexpath ="user_".($i+1).".htm"; //filename
$showpagestr= "<ahref=\"user_1.htm\">第一页</a>".$showpages." <ahref=\"user_".$pages.".htm\">最尾页</a>";
}
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文件代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN""http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved fromurl=(0046)http://localhost/overseatrade/keyword/User.php-->
<HTMLxmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE><{$SiteName}></TITLE>
<META http-equiv=Content-Type content="text/html;charset=utf-8"><LINK
href="templates/css.css" type=text/cssrel=stylesheet>
<META content="MSHTML 6.00.2900.2722"name=GENERATOR>
<style type="text/css">
<!--
.style1 {
font-size: 18px;
font-weight: bold;
}
-->
</style>
</HEAD>
<BODY>
<TABLE cellSpacing=0 cellPadding=0 width="90%"border=0>
<TBODY>
<TR>
<TD height="50" class=tipsstyle1><spanclass="style1">Example of create html file usesmarty template with php</span></TD>
</TR>
</TBODY></TABLE>
<TABLE class=tablestyle="WORD-BREAK: break-all" borderColor=#000000
cellSpacing=0 cellPadding=2 width="100%"align=center border=1>
<TBODY>
<TRclass="red1bg_head1">
<TD class=linebg1 align=middle>編號</TD>
<TD class=linebg1 align=middle>用戶名</TD>
<TD class=linebg1 align=middle>姓名</TD>
<TD class=linebg1align=middle>類別</TD>
<TD class=linebg1align=middle>郵件地址</TD>
</TR>
<{sectionname=adminlist loop=$Admin}><TRclass="linebg<{$Bg_Type}>1">
<TDalign=middle> <{$Admin[adminlist].Id}></TD>
<TDalign=middle> <{$Admin[adminlist].Name}></TD>
<TDalign=middle> <{$Admin[adminlist].Nick}></TD>
<TDalign=middle> <{$Admin[adminlist].Type}></TD>
<TD align=middle> <Aclass=B
href="mailto:<{$Admin[adminlist].Email}>"><{$Admin[adminlist].Email}></A></TD>
</TR>
<{/section}>
<TR>
<TD align=middlecolSpan=6><{$Page_Str}></TD></TR></TABLE>
<DIVclass=sepdiv>
<p></p>
<palign="left">Powered by <ahref="http://www.ybfq.com"target="_blank">www.ybfq.com</a> Rossy'sblog:<a href="http://www.ybfq.com/blog"target="_blank">http://www.ybfq.com/blog</a></p>
</DIV>
</BODY></HTML>

<?php
$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= "上一页 | <ahref=".$pager_URL."&pager_PageID=".($pager_PageID+1).">下一页</a>";
}elseif($pager_PageID == $pager_Number&& $pager_Number>1){
//最后一页
$pager_Links = "<ahref=".$pager_URL."&pager_PageID=".($pager_PageID-1).">上一页</a>| 下一页";
} elseif($pager_PageID > 1&& $pager_PageID <=$pager_Number) {
//中间
$pager_Links = "<ahref=".$pager_URL."&pager_PageID=".($pager_PageID-1).">上一页</a>| <ahref=".$pager_URL."&pager_PageID=".($pager_PageID+1).">下一页</a>";
} 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");
?>

<?php
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 = '}>';
?>
<?php require_once('conn/function.php');?>
<?php require_once('conn/conn.php');?>
<?php//require_once('class/plugins/function.pager.php');?>
<?php
$page_count = "3"; //每一页的记录数
$tatol=$aa->selectpage("news","","");
$pages = ceil($tatol / $page_count);

if($tatol<2)
{
echo "首页 / 上一页/ 下一页 / 末页";
}
if($tatol>2)
{
$p=2;
$e=0;
$name="首页 /上一页 / <ahref='news2.html'>下一页</a>/ <ahref='news".$pages.".html'>末页</a>";

for($pi=0;$pi<=$tatol;$pi++)
{
$start_page = $pi *$page_count;

if(($pi+1)%$page_count==0)
{
$p--;
//生成HTML页面
echo "<br>".$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.="<ahref=news1.html>首页</a> / ";
$name.="<ahref='news".$p.".html'>上一页</a> / ";
$p++;
$p++;
if($p<>$pages+1)
{
$name.="<ahref='news".$p.".html'>下一页</a> / ";
$name.="<ahref='news".$pages.".html'>末页</a>";
}
else
{
$name.="下一页 / 末页";
}
}

}
if($tatol%$page_count<>0)
{
$name="<ahref=news1.html>首页</a> / <ahref='news".($pages-1).".html'>上一页</a> / 下一页 / 末页";

}

}

//这是生成另一个的代码
//for ($j=1;$j<=$pages;$j++)
// $showpages.= "<ahref=\"user_".$j.".htm\">".$j."</a>";
//
//for ($i = 0;$i<$pages; $i++)
//{
// $start_page = $i *$page_count;
// if($pages>1)
// {
// $indexpath ="user_".($i+1).".htm"; //file name
// $showpagestr = "<ahref=\"user_1.htm\">第一页</a>".$showpages." <ahref=\"user_".$pages.".htm\">最尾页</a>";
// }
// 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中文网(www.php.cn)

相关标签: Smarty模板,PHP