2007-8-10 23:17
伶俜
i do this, but i don't know if any way better?????
[B]une idée.[/B]
1. add[QUOTE]$sql .= " SQL_CALC_FOUND_ROWS ";[/QUOTE] in line 133 zend/db/select.php
or rewrite the method
[B]2. Model[/B]
[php]
class Album extends Zend_Db_Table
{
protected $_name = 'album';
//get le total nombre de résultat
public function getNombre()
{
$res = $this->_db->fetchRow("SELECT FOUND_ROWS() as total_nombre;");
return $res["total_nombre"];
}
}[/php]
[B]3. Controler[/B]
[php]
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$Album = new Album();
$page = (int)$this->_request->getParam('page', 0);
$where = array();//à ajouter
$sortby = 'title';//à modifier
$this->view->albums = $Album->fetchAll($where,$sortby,NUM_PAR_PAGE,$page*NUM_PAR_PAGE)->toArray();
$pagination = new Custom_Pagination($Album->getCountQuery(),$this->_request);
$this->view->pagination = $pagination->render();
}
}
[/php]
NUM_PAR_PAGE is a const
[B]4.View (smarty)[/B]
[php]
<table>
{section name=album loop=$albums}
<tr bgcolor="#FFFFFF">
<td align="center">{$albums[album].title}</td>
<td align="center">{$albums[album].artist}</td>
</tr>
{/section}
</table>
{$pagination}
[/php]
[B]5. class Custom_Pagination[/B]
[php]
class Custom_Pagination
{
private $total_page;
private $total_nombre;
private $page_courante;
private $_request;
function __construct($total_nombre,$request)
{
$this->_request = clone $request;
$this->page_courante = $this->_request->getParam("page");
$this->total_page = ceil($total_nombre/NUM_PAR_PAGE);
$this->total_nombre = $total_nombre;
}
function render()
{
//à ajouter les pages précédentes, suivantes .......
$html = '';
if($this->total_page > 1)
{
for($i=0;$i<$this->total_page;$i++)
{
if($i>0)$html.= "/";
$this->_request->setParam("page",$i);
$html.= '<a href="'.$this->rebuildUrl().'">'.($i+1).'</a>';
}
}
return $html;
}
// reconstruire l'URL /album/index/index/page/1,/album/index/index/page/2,...
//à améliorer. j'ai essayé $this->_request->setParam("page", $num_page); mais $this->_request->getRequestUri ne chage pas, si autre méthod?
function rebuildUrl()
{
$url = $this->_request->getBaseUrl();
$params = $this->_request->getParams();
$url .="/".$params["module"];
$url .="/".$params["controller"];
$url .="/".$params["action"];
foreach ( $params as $key=>$value)
{
if(!in_array($key,array("module","controller","action")))
$url.="/".$key."/".$value;
}
return $url;
}
}
[/php]