我的BLOG开张了,做好准备喔!!!

php 分页类 可用于smarty

上一篇 / 下一篇  2008-03-24 00:45:14 / 个人分类:技术文章

<?
 #江南大学观方站系统
 #数据库分页查徇类(适用于smarty模板调用)
 #yangzhu2008-3-23
 //include('../db_connect.php');
 Class ShowPage {
  Public $Page;//当前实际页码
  Public $NowPage;//当前显示页码
  Public $EvePage;//每页显示数据
  Public $Cstart;
  Public $PrePage;//前一页
  Public $NextPage;//下一页
  Public $CountPage;//总页数
  
  Function GetCount($sql){//返回总页数
   GLOBAL $dbh;
   $res=mysql_query($sql);
   $row=mysql_fetch_row($res);
   
   $this->CountPage = ceil($row[0]/$this->EvePage);
  }

  Function GetArr($sql){//返回查徇的数组
   GLOBAL $dbh;

   if ( $this->Page == '' or $this->Page <=0 ) $this->Page =0;
   if ( $this->Page >= $this->CountPage ) $this->Page =$this->Page - 1;
   $this->Cstart = $this->Page * $this->EvePage;
   $sql=$sql." limit $this->Cstart, $this->EvePage";
   echo $sql;
   $res=mysql_query($sql);
   while( $row = mysql_fetch_assoc($res) ){
    $arr[] = $row;
   };
   
   $this->PrePage = $this->Page - 1;
   $this->NextPage = $this->Page + 1;
   $this->NowPage = $this->Page + 1;
   return $arr;
   
  }
 
 }

 $MysqlPage = new showpage(); //创建分页类实例
 $evepage=10;     //每页显示
 $MysqlPage->EvePage=$evepage;
 $MysqlPage->Page=$page;   //当前页
 $sql = "select count(*) from pic";
 $MysqlPage->GetCount($sql);
 $sql = "select * from pic";
 $arr = $MysqlPage->GetArr($sql);  /*分页*/
 $countpage = $MysqlPage->CountPage;
 $nowpage = $MysqlPage->NowPage;
 $prepage = $MysqlPage->PrePage;
 $nextpage = $MysqlPage->NextPage;

?>

写了一个晚上才搞完这个分页,总要是想理解类,发现有点明白了...还要努力.感觉类的使用确实很方便,与函数相比个人感觉类的好处是可以返回很多的值,以前写过一个分页函数,返回数组后又要再查徇上一页下一页之类的东西.但类可以在处理类中的变量,在外部直接调用,相当于返回了很多值,也不知道理解的对不对,感觉应该是这样.

后面注解掉的是使用该类的方式.还有数据库连接语句,

smarty的使用如下.

    {section name=item loop=$arr}
    <tr>
    <td><a href="id={$arr[item].pid}"></a></td>
    <td>{$arr[item].tid}</td>
    <td>{$arr[item].content}</td>
    <td>{$arr[item].tag}</td>
    <td>{$arr[item].email}</td>
    <td>{$arr[item].addtime}</td>  
    <td>{$arr[item].pnums}</td>
    <td>{$arr[item].dnums}</td>
    <td>{$arr[item].chk}</td>
    <td>{$arr[item].picurl}</td>
   
    </tr>
    {sectionelse} 对不起,没有任何对应数据!
    {/section}
</table>
<div>共{$nowpage}/{$countpage}页 <a href=index.php?action=HotBang&page={$prepage}>上一页</a> <a href=index.php?

action=HotBang&page={$nextpage}>下一页</a> </div>
  <!--end container......-->
 {include file="footer.html"}

 

我写的东西暂时还没有怎么考虑效率..估计也不怎么样.慢慢来吧.


TAG:

引用 删除 macleo   /   2008-07-28 20:35:02
晕死。。。没有提供链接mysql 的部分啊。。。。。还要自己找去。。。楼主狠啊
引用 删除 macleo   /   2008-07-28 20:31:33
http://www.itlearner.com/article/2004/451.shtml


这个可以看看,呵呵
引用 删除 macleo   /   2008-07-28 20:30:19

谢谢了。。。我用一下。呵呵
 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2008-08-27  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 5358
  • 日志数: 92
  • 图片数: 5
  • 影音数: 2
  • 文件数: 2
  • 书签数: 5
  • 建立时间: 2007-05-01
  • 更新时间: 2008-08-22

RSS订阅

Open Toolbar