PHP学习:Smarty的分页实现

Smarty中的分页有比超多办法。
1。使用Smarty的分页插件,如Pager,pagnition,sliding_page等,可是认为都不是太好,差十分的少都有点Bug。
有乐趣试用和调谐去改进的爱人能够看看:
2。使用分页类库,呵呵,这些英特网就太多了,一大把,可是自个儿尚未察觉写得很好的,特别是便于扩张的。
在检索的时候还看到三个名字为分页类终结者的,哈哈,有一些滑稽。分页类中把SQL都饱含进去了,那一个是绝对不能够忍受的,能够说小编对OO的认知还相比较浅。
但是分页类库不必然切合Smarty,特别是当记录集数据是二维数组时,小编不想屏弃斯Matty方便的Section。那是本身没有用PEA奥迪Q5::Pager的原故,不然还要先对数组举办管理,也很辛劳。
3。自身写啊。。笔者临风还没接受写成类库,然则改写相当的轻松,上面主要讲讲思路。
其实斯Matty的分页非常简单,首先我们得以在模板中如此落成:{$pager_Links}{section
name=”list” loop=$productID start=0 max=$pager_Total step=1}{if
($smarty.section.list.index = $pager_StartNum )
($smarty.section.list.index = $pager_EndNum
卡塔尔国}成品名称:{$productName[list]} 付加物连串:{$catalogName[list]}{/if}
{/section}
以上就足以将贰个记录集轻松地打印出来,并且限制每页的显得范围。{$pager_Links}
分页标签max=$pager_Total 记录总的数量$smarty.section.list.index =
$pager_StartNum ) ($smarty.section.list.index = $pager_EndNum
State of Qatar那豆蔻梢头行是用来界定记录的显得范围的,若是记录的目录落在这里个限定以内就显得出来,不然就不显得。以上方可看出,在PHP文件中,我们只要求传递4个变量给斯Matty对象:1.记录总量2.每页记录开端数3.每页记录结束数4.分页标签以下代码代参谋:标签可以本人去写了,能够扩充成越来越强硬的。以往自家未曾时间,不然会写三个?php$smartyArr
= $smarty-get_template_vars(卡塔尔(قطر‎; //记录总的数量,每页突显记录条数,总页数

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””;
<!– saved
fromurl=(0046);
<HTMLxmlns=”;
<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=”;   
 Rossy’sblog:<a
href=”;
     </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{
         //第2回访谈
         $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=”首页  /上一页  /  <a >下一页</a>/  <a
>末页</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.=”<a >上一页</a> /  “;
    $p++;
    $p++;
     if($p<>$pages+1)
     {
      $name.=”<a >下一页</a> /  “;
      $name.=”<a >末页</a>”;
     }
     else
     {
      $name.=”下一页 /  末页”;
     }
    }

   }
   if($tatol%$page_count<>0)
   {
   $name=”<ahref=news1.html>首页</a> /  <a
>上一页</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”;
?>


发表评论

电子邮件地址不会被公开。 必填项已用*标注