ZendFramework中的分页类实现

参考自:http://www.phpfans.net/ask/question2/7822122562.html

其实上文写得比较清楚了,我这也只是把上面的方法推广一下而已。

主要是分页类的实现。

如上图文件结构所示,在library目录下新建Custom模块。各个文件的代码依次是:

Mysql.php

  1. <?php
  2. require_once 'Zend/Db/Adapter/Pdo/Mysql.php';
  3. class Custom_Db_Adapter_Pdo_Mysql extends Zend_Db_Adapter_Pdo_Mysql
  4. {
  5.     public function fatchPage($sql$pagesize=20, $currentpage=1)
  6.     {
  7.         $currentpage = is_numeric($currentpage) ? $currentpage : 1 ;
  8.         $result = array();
  9.         $result_array = $this->query($sql);
  10.         $result['count'] = count($result_array->fetchAll());
  11.         $result['pagecount'] = $result['count']/$pagesize>1 ? ceil($result['count']/$pagesize) : 1;
  12.         $offset = ($currentpage-1)*$pagesize;
  13.         $result['currentpage'] = $currentpage;
  14.         $result['firstpage'] = 1;
  15.         $result['lastpage'] = $result['pagecount'];
  16.         $sql .= ' '.$this->select()->limit($pagesize$offset)->__toString();
  17.         $result['table'] = $this->query($sql)->fetchAll();
  18.         return $result;
  19.     }
  20. }
  21. ?>

Table.php

  1. <?php
  2. require_once 'Zend/Db/Table.php';
  3. class Custom_Db_Table extends Zend_Db_Table
  4. {
  5.     function preDispatch()
  6.     {
  7.     }
  8.     
  9.     public function fetchPage($where = null, $order = null, $pagesize = 20, $currentpage = 1)
  10.     {
  11.         $result = array();
  12.         $result['count'] = $this->fetchAll($where$order)->count();
  13.         $result['pagecount'] = $result['count']/$pagesize>1 ? ceil($result['count']/$pagesize) : 1;
  14.         $offset = ($currentpage-1)*$pagesize;
  15.         $result['currentpage'] = $currentpage;
  16.         $result['firstpage'] = 1;
  17.         $result['lastpage'] = $result['pagecount'];
  18.         $result['table'] = $this->fetchAll($where$order$pagesize$offset)->toArray();
  19.         return $result;
  20.     }
  21. }
  22. ?>

Db.php

  1. <?php
  2. require_once 'Zend/Db.php';
  3. require_once 'Custom/Db/Adapter/Pdo/Mysql.php';
  4. class Custom_Db extends Zend_Db
  5. {
  6.     public static function myfactory($config = array())
  7.     {
  8.         $adapterName = 'Custom_Db_Adapter_Pdo_Mysql';
  9.         $dbAdapter = new $adapterName($config);
  10.         return $dbAdapter;
  11.     }
  12. }
  13. ?>

Page.php

  1. <?php
  2. class Custom_Page
  3. {
  4.     public static function mToPage(array $Page)
  5.     {
  6.         $url = 'http://'.$_SERVER ['HTTP_HOST'].$_SERVER["REQUEST_URI"];
  7.         $result = '共'.$Page['count'].'条记录 共'.$Page['pagecount'].'页 第'.$Page['currentpage'].'页';
  8.         $result .= '<select id="selectPage">';
  9.         for($i=1; $i<=$Page['pagecount']; $i++)
  10.         {
  11.             if (preg_match("/page[/=|//]{1}(/d+)/i"$url))
  12.             {
  13.                 if(preg_match("/page[/=]{1}(/d+)/i"$url))
  14.                 {
  15.                     $pageurl = preg_replace("/page[/=]{1}(/d+)/i"'page='.$i$url);
  16.                 }
  17.                 elseif (preg_match("/page[//]{1}(/d+)/i"$url))
  18.                 {
  19.                     $pageurl = preg_replace("/page[//]{1}(/d+)/i"'page/'.$i$url);
  20.                 }
  21.             }
  22.             else
  23.             {
  24.                 $pageurl = '?page='.$i;
  25.             }
  26.             $selected = $Page['currentpage']==$i ? 'selected' : '';
  27.             $result.='<option value="'.$pageurl.'" '.$selected.'>'.$i.'</option>';
  28.         }
  29.         $result .= '</select>';
  30.         return array('showpage' => $result'table' => $Page['table']);
  31.     }
  32. }
  33. ?>

下面是对上诉组件的运用:

IndexController.php

  1. <?php
  2. /**
  3.  * IndexController - The default controller class
  4.  * 
  5.  * @author
  6.  * @version 
  7.  */
  8. require_once 'Zend/Controller/Action.php';
  9. require_once 'Zend/Registry.php';
  10. require_once 'Custom/Page.php';
  11. require_once 'Custom/Db.php';
  12. require_once 'Custom/Db/Table.php';
  13. class IndexController extends Zend_Controller_Action 
  14. {
  15.     public function init()
  16.     {
  17.         $params = array ('host' => 'localhost',
  18.                          'username' => 'root',
  19.                          'password' => 'root',
  20.                          'dbname'   => 'mysql');
  21.         
  22.         $dbAdapter = Custom_Db::myfactory($params);
  23.         Custom_Db_Table::setDefaultAdapter($dbAdapter);
  24.         $dbAdapter->query('SET NAMES UTF8');
  25.         Zend_Registry::set('dbAdapter'$dbAdapter);    
  26.     }
  27.     
  28.     public function indexAction() 
  29.     {
  30.         $dbAdapter = Zend_Registry::get('dbAdapter');
  31.         $list = Custom_Page::mToPage($dbAdapter->fatchPage('select * from user', 4, $this->_getParam('page')));
  32.         $this->view->showPage = $list['showpage'];
  33.         $this->view->table = $list['table'];
  34.     }
  35. }

index.phtml

  1. <?php
  2. echo '<?xml version="1.0" encoding="UTF-8" ?>';
  3. ?>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head>
  7.     <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  8.     <title>New Zend Framework Project</title>
  9. </head>
  10. <body>
  11.     <?= $this->showPage ?>
  12.     <? foreach ($this->table as $i => $value) {?>
  13.     <br/><?=  $this->table[$i]['Host'] ?>
  14.     <? }?>
  15. </body>
  16. </html>

当然,新版的ZF已经自带了Zend_Paginator的功能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值