参考自:http://www.phpfans.net/ask/question2/7822122562.html
其实上文写得比较清楚了,我这也只是把上面的方法推广一下而已。
主要是分页类的实现。
如上图文件结构所示,在library目录下新建Custom模块。各个文件的代码依次是:
Mysql.php
- <?php
- require_once 'Zend/Db/Adapter/Pdo/Mysql.php';
- class Custom_Db_Adapter_Pdo_Mysql extends Zend_Db_Adapter_Pdo_Mysql
- {
- public function fatchPage($sql, $pagesize=20, $currentpage=1)
- {
- $currentpage = is_numeric($currentpage) ? $currentpage : 1 ;
- $result = array();
- $result_array = $this->query($sql);
- $result['count'] = count($result_array->fetchAll());
- $result['pagecount'] = $result['count']/$pagesize>1 ? ceil($result['count']/$pagesize) : 1;
- $offset = ($currentpage-1)*$pagesize;
- $result['currentpage'] = $currentpage;
- $result['firstpage'] = 1;
- $result['lastpage'] = $result['pagecount'];
- $sql .= ' '.$this->select()->limit($pagesize, $offset)->__toString();
- $result['table'] = $this->query($sql)->fetchAll();
- return $result;
- }
- }
- ?>
Table.php
- <?php
- require_once 'Zend/Db/Table.php';
- class Custom_Db_Table extends Zend_Db_Table
- {
- function preDispatch()
- {
- }
- public function fetchPage($where = null, $order = null, $pagesize = 20, $currentpage = 1)
- {
- $result = array();
- $result['count'] = $this->fetchAll($where, $order)->count();
- $result['pagecount'] = $result['count']/$pagesize>1 ? ceil($result['count']/$pagesize) : 1;
- $offset = ($currentpage-1)*$pagesize;
- $result['currentpage'] = $currentpage;
- $result['firstpage'] = 1;
- $result['lastpage'] = $result['pagecount'];
- $result['table'] = $this->fetchAll($where, $order, $pagesize, $offset)->toArray();
- return $result;
- }
- }
- ?>
Db.php
- <?php
- require_once 'Zend/Db.php';
- require_once 'Custom/Db/Adapter/Pdo/Mysql.php';
- class Custom_Db extends Zend_Db
- {
- public static function myfactory($config = array())
- {
- $adapterName = 'Custom_Db_Adapter_Pdo_Mysql';
- $dbAdapter = new $adapterName($config);
- return $dbAdapter;
- }
- }
- ?>
Page.php
- <?php
- class Custom_Page
- {
- public static function mToPage(array $Page)
- {
- $url = 'http://'.$_SERVER ['HTTP_HOST'].$_SERVER["REQUEST_URI"];
- $result = '共'.$Page['count'].'条记录 共'.$Page['pagecount'].'页 第'.$Page['currentpage'].'页';
- $result .= '<select id="selectPage">';
- for($i=1; $i<=$Page['pagecount']; $i++)
- {
- if (preg_match("/page[/=|//]{1}(/d+)/i", $url))
- {
- if(preg_match("/page[/=]{1}(/d+)/i", $url))
- {
- $pageurl = preg_replace("/page[/=]{1}(/d+)/i", 'page='.$i, $url);
- }
- elseif (preg_match("/page[//]{1}(/d+)/i", $url))
- {
- $pageurl = preg_replace("/page[//]{1}(/d+)/i", 'page/'.$i, $url);
- }
- }
- else
- {
- $pageurl = '?page='.$i;
- }
- $selected = $Page['currentpage']==$i ? 'selected' : '';
- $result.='<option value="'.$pageurl.'" '.$selected.'>'.$i.'</option>';
- }
- $result .= '</select>';
- return array('showpage' => $result, 'table' => $Page['table']);
- }
- }
- ?>
下面是对上诉组件的运用:
IndexController.php
- <?php
- /**
- * IndexController - The default controller class
- *
- * @author
- * @version
- */
- require_once 'Zend/Controller/Action.php';
- require_once 'Zend/Registry.php';
- require_once 'Custom/Page.php';
- require_once 'Custom/Db.php';
- require_once 'Custom/Db/Table.php';
- class IndexController extends Zend_Controller_Action
- {
- public function init()
- {
- $params = array ('host' => 'localhost',
- 'username' => 'root',
- 'password' => 'root',
- 'dbname' => 'mysql');
- $dbAdapter = Custom_Db::myfactory($params);
- Custom_Db_Table::setDefaultAdapter($dbAdapter);
- $dbAdapter->query('SET NAMES UTF8');
- Zend_Registry::set('dbAdapter', $dbAdapter);
- }
- public function indexAction()
- {
- $dbAdapter = Zend_Registry::get('dbAdapter');
- $list = Custom_Page::mToPage($dbAdapter->fatchPage('select * from user', 4, $this->_getParam('page')));
- $this->view->showPage = $list['showpage'];
- $this->view->table = $list['table'];
- }
- }
index.phtml
- <?php
- echo '<?xml version="1.0" encoding="UTF-8" ?>';
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- <title>New Zend Framework Project</title>
- </head>
- <body>
- <?= $this->showPage ?>
- <? foreach ($this->table as $i => $value) {?>
- <br/><?= $this->table[$i]['Host'] ?>
- <? }?>
- </body>
- </html>
当然,新版的ZF已经自带了Zend_Paginator的功能。