

Este artículo ha sido consultado en 2,174 ocasiones.
Paginación es simplemente presentar una tabla grande en pantallas, es decir poder "hojear" los datos.
Stranger Studios tiene un tutorial sobre como implementar una paginación estilo Digg, aqui siemplemente adapto el código a un componente.
El componente se usa en el controlador agregándolo al array $components:
public $components = array('Portal', 'Mypagination', 'Security');
por ejemplo en el método view() del controlador news_controller.php el paginador se usa asi:
public function view($page=1)
{
$this->pageTitle = 'Hacktivismo::Mononeurona';
//pagination
$total_rows = $this->News->findCount(array("status"=>1));
$lmt = 7; // limit news
$targetpage = "/news/view/"; // URL, this class->method
$pagination = $this->Mypagination->init($total_rows, $page, $lmt, $targetpage);
$this->set('pagination', $pagination);
$conditions = array("News.status"=>1);
$fields = array("News.id", "News.title", "News.body", "News.created", "Theme.img", "Theme.title");
$order = "News.id DESC";
$offset = (($page * $lmt) - $lmt);
$limit = $lmt . " OFFSET " . $offset;
$this->set('data', $this->News->findAll($conditions, $fields, $order, $limit));
}
Necesito OFFSET porque en PostgreSQL se debe declarar de manera explicita, pero si estás en MySQL u Oracle debes quitar el "OFFSET" en $limit.
$this->Mypagination->init regresa el string $pagination, que se incluye en la vista /app/views/news/view.thtml:
< ? php
//pagination
echo $pagination;
?>
El código del componente es este:
< ? php
/**
* Pagination Component, responsible for managing the DATA required for pagination.
* Manuel Montoya GPLv3
* Chipotle Software 2007
* manuel-AT-mononeurona-DOT-org
*/
class MypaginationComponent extends Object
{
//number of rows (items) returned by the query.
private $total_rows;
//limit
private $limit;
// $page is the current page number
public $page;
//URL
public $targetpage;
/**
* Startup - Link the component to the controller.
*
* @param controller
*/
public function startup(&$controller)
{
$this->controller =& $controller;
}
/**
* Initialize the pagination data.
*
* @param unknown
* @param array
* @options array
* @return array
*/
function init($tr, $page, $limit,$targetpage)
{
$this->total_rows = (int) $tr;
$this->page = (int) $page;
$this->limit = (int) $limit;
$this->targetpage = (string) $targetpage;
return $this->getPaginationString();
}
//function to return the pagination string
private function getPaginationString($adjacents = 1)
{
// NEXT CODE BY Stranger Studios http://www.strangerstudios.com/sandbox/pagination/diggstyle.php
//other vars
$prev = $this->page - 1; //previous page is page - 1
$next = $this->page + 1; //next page is page + 1
$lastpage = ceil($this->total_rows / $this->limit); //lastpage is = total items / items per page, rounded up.
//exit($lastpage);
$lpm1 = $lastpage - 1; //last page minus 1
/*
Now we apply our rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if ($lastpage > 1)
{
$pagination .= '<div class="pagination">'; //just starting
//previous button
if ($this->page > 1)
{
$pagination .= '<a href="'. $this->targetpage. $prev .'"> prev</a>';
}
else
{
$pagination .= '<span class="disabled">prev</span>'; // no such prev page
}
//pages
if ( $lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $this->page)
{
$pagination .= '<span class="current">'.$counter.'</span>'; //current
}
else
{
$pagination .= '<a href="'.$this->targetpage . $counter . '">'.$counter.'</a>';
}
}
}
elseif ($lastpage >= 7 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if ($this->page < 1 + ($adjacents * 3))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $this->page)
{
$pagination .= '<span class="current">'.$counter.'</span>';
}
else
{
$pagination .= '<a href="'.$this->targetpage . $counter.'">'.$counter.'</a>';
}
}
$pagination .= "...";
$pagination .= '<a href="'.$this->targetpage . $lpm1 .'">'.$lpm1.'</a>';
$pagination .= '<a href="'.$this->targetpage. $lastpage.'">'.$lastpage.'</a>';
}
//in middle; hide some front and some back
elseif ($lastpage - ($adjacents * 2) > $this->page && $this->page > ($adjacents * 2))
{
$pagination .= '<a href="'.$this->targetpage.'">1</a>';
$pagination .= '<a href="'.$this->targetpage.'">2</a>';
$pagination .= '...';
for ($counter = $this->page - $adjacents; $counter <= $this->page + $adjacents; $counter++)
{
if ($counter == $this->page)
{
$pagination .= '<span class="current">'.$counter.'</span>';
}
else
{
$pagination .= '<a href="'.$this->targetpage.$counter.'">'.$counter.'</a>';
}
}
$pagination .= '...';
$pagination .= '<a href="'.$this->targetpage.$lpm1.'">'.$lpm1.'</a>';
$pagination .= '<a href="'.$this->targetpage.$lastpage.'">'.$lastpage.'</a>';
}
else //close to end; only hide early pages
{
$pagination .= '<a href="'.$this->targetpage.'">1</a>';
$pagination .= '<a href="'.$this->targetpage.'">2</a>';
$pagination .= '...';
for ($counter = $lastpage - (1 + ($adjacents * 3)); $counter <= $lastpage; $counter++)
{
if ($counter == $this->page)
{
$pagination .= '<span class="current">'.$counter.'</span>';
}
else
{
$pagination .= '<a href="'.$this->targetpage.$counter.'">'.$counter.'</a>';
}
}
}
}
//next button
if ( $this->page < $counter - 1 )
{
$pagination .= '<a href="'.$this->targetpage . $next.'"> Next </a>';
}
else
{
$pagination .= '<span class="disabled"> Next </span>';
}
$pagination .= '</div>';
}
return $pagination;
}
?>
Es necesario agregar unas clases CSS para que se vea estilo Digg:
/* Holly Hack for IE \*/
* html .suckertreemenu ul li { float: left; height: 1%; }
* html .suckertreemenu ul li a { height: 1%; }
/* End */
/** PAGINATION ***/
div.pagination {
padding: 3px;
margin: 3px;
font-size:8pt;
}
div.pagination a {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #fe640d;
text-decoration: none; /* no underline */
font-size:8pt;
color: #fe640d;
}
div.pagination a:hover, div.pagination a:active {
border: 1px solid #000099;
color: #000;
}
div.pagination span.current {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #fe640d;
font-weight: bold;
background-color: #fe640d;
color: #fff;
}
div.pagination span.disabled {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #EEE;
color: #c0c0c0;
}
aarkerio
Notice (8): Undefined index: email [APP/View/Pages/display.ctp, line 26]Code Contextecho $this->Html->div('cv');echo "Ficha del autor:<br /><span class=\"login\">".$data['User']['username'].'</span><br />';echo "<b>". str_replace('@', '_ARRROBA_', $data['User']['email'])."</b><br />".$data['User']['cv'].'<br />';$___viewFn = "/var/chipotle/sites/cakephp/centauro/View/Pages/display.ctp" $___dataForView = array( "data" => array( "Page" => array(), "User" => array(), "Section" => array(), "Discution" => array() ) ) $data = array( "Page" => array( "id" => 785, "section_id" => 28, "title" => "CakePHP pagination component", "body" => "<p>Paginación es simplemente presentar una tabla grande en pantallas, es decir poder "hojear" los datos. <br /> <br /> <a href="http://www.strangerstudios.com/sandbox/pagination/diggstyle.php"> Stranger Studios </a>tiene un tutorial sobre como implementar una paginación estilo Digg, aqui siemplemente adapto el código a un componente. <br /> </p> <p><br /> El componente se usa en el controlador agregándolo al array $components:<br /> <br /> <strong><font color="#333300">public $components = array('Portal', 'Mypagination', 'Security');</font></strong><br /> <br /> por ejemplo en el método view() del controlador news_controller.php el paginador se usa asi:<br /> <br /> <font color="#800000">public function view($page=1) <br /> { <br /> </font><font color="#800000">$this->pageTitle = 'Hacktivismo::Mononeurona';<br /> </font></p> <p> <font color="#800000"><br /> </font><font color="#800000"> //pagination<br /> $total_rows = $this->News->findCount(array("status"=>1)); <br /> <br /> $lmt = 7; // limit news<br /> <br /> $targetpage = "/news/view/"; // URL, this class->method<br /> <br /> $pagination = $this->Mypagination->init($total_rows, $page, $lmt, $targetpage);<br /> <br /> <strong> $this->set('pagination', $pagination);</strong><br /> <br /> $conditions = array("News.status"=>1);<br /> <br /> $fields = array("News.id", "News.title", "News.body", "News.created", "Theme.img", "Theme.title");<br /> <br /> $order = "News.id DESC";<br /> <br /> $offset = (($page * $lmt) - $lmt);<br /> <br /> <strong>$limit = $lmt . " OFFSET " . $offset;</strong><br /> <br /> $this->set('data', $this->News->findAll($conditions, $fields, $order, $limit));<br /> }<br /> </font><br /> Necesito OFFSET porque en PostgreSQL se debe declarar de manera explicita, pero si estás en MySQL u Oracle debes quitar el "OFFSET" en $limit.<br /> <br /> <font color="#800000">$this->Mypagination->init</font> regresa el string $pagination, que se incluye en la vista <strong>/app/views/news/view.thtml</strong>:<br /> <font color="#808000"><br /> < ? php <br /> //pagination<br /> echo $pagination; <br /> ?></font><br /> <br /> El código del componente es este:<br /> <br /> <font color="#800080"> < ? php<br /> /**<br /> * Pagination Component, responsible for managing the DATA required for pagination.<br /> * Manuel Montoya GPLv3<br /> * Chipotle Software 2007<br /> * manuel-AT-mononeurona-DOT-org<br /> */<br /> class MypaginationComponent extends Object<br /> {<br /> //number of rows (items) returned by the query.<br /> private $total_rows;<br /> <br /> //limit<br /> private $limit;<br /> <br /> // $page is the current page number<br /> public $page;<br /> <br /> //URL<br /> public $targetpage;<br /> <br /> /**<br /> * Startup - Link the component to the controller.<br /> *<br /> * @param controller<br /> */<br /> public function startup(&$controller)<br /> {<br /> $this->controller =& $controller;<br /> }<br /> <br /> /**<br /> * Initialize the pagination data.<br /> *<br /> * @param unknown<br /> * @param array<br /> * @options array<br /> * @return array<br /> */<br /> function init($tr, $page, $limit,$targetpage)<br /> {<br /> $this->total_rows = (int) $tr;<br /> $this->page = (int) $page;<br /> $this->limit = (int) $limit;<br /> $this->targetpage = (string) $targetpage;<br /> <br /> return $this->getPaginationString();<br /> <br /> }<br /> <br /> //function to return the pagination string</font></p> <p><font color="#800080">private function getPaginationString($adjacents = 1)<br /> { <br /> // NEXT CODE BY Stranger Studios http://www.strangerstudios.com/sandbox/pagination/diggstyle.php<br /> //other vars<br /> $prev = $this->page - 1; //previous page is page - 1<br /> $next = $this->page + 1; //next page is page + 1<br /> $lastpage = ceil($this->total_rows / $this->limit); //lastpage is = total items / items per page, rounded up.<br /> //exit($lastpage);<br /> $lpm1 = $lastpage - 1; //last page minus 1<br /> <br /> /* <br /> Now we apply our rules and draw the pagination object. <br /> We're actually saving the code to a variable in case we want to draw it more than once.<br /> */<br /> $pagination = "";<br /> <br /> if ($lastpage > 1)<br /> { <br /> $pagination .= '<div class="pagination">'; //just starting<br /> <br /> //previous button<br /> if ($this->page > 1)<br /> {<br /> $pagination .= '<a href="'. $this->targetpage. $prev .'"> prev</a>';<br /> }<br /> else<br /> {<br /> $pagination .= '<span class="disabled">prev</span>'; // no such prev page<br /> }<br /> <br /> //pages <br /> if ( $lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up<br /> { <br /> for ($counter = 1; $counter <= $lastpage; $counter++)<br /> {<br /> if ($counter == $this->page)<br /> {<br /> $pagination .= '<span class="current">'.$counter.'</span>'; //current<br /> }<br /> else<br /> {<br /> $pagination .= '<a href="'.$this->targetpage . $counter . '">'.$counter.'</a>';<br /> }<br /> }<br /> }<br /> elseif ($lastpage >= 7 + ($adjacents * 2)) //enough pages to hide some<br /> {<br /> //close to beginning; only hide later pages<br /> if ($this->page < 1 + ($adjacents * 3)) <br /> {<br /> for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)<br /> {<br /> if ($counter == $this->page)<br /> {<br /> $pagination .= '<span class="current">'.$counter.'</span>';<br /> }<br /> else<br /> {<br /> $pagination .= '<a href="'.$this->targetpage . $counter.'">'.$counter.'</a>';<br /> }<br /> }<br /> $pagination .= "...";<br /> $pagination .= '<a href="'.$this->targetpage . $lpm1 .'">'.$lpm1.'</a>';<br /> $pagination .= '<a href="'.$this->targetpage. $lastpage.'">'.$lastpage.'</a>'; <br /> }<br /> //in middle; hide some front and some back<br /> elseif ($lastpage - ($adjacents * 2) > $this->page && $this->page > ($adjacents * 2))<br /> {<br /> $pagination .= '<a href="'.$this->targetpage.'">1</a>';<br /> $pagination .= '<a href="'.$this->targetpage.'">2</a>';<br /> $pagination .= '...';<br /> for ($counter = $this->page - $adjacents; $counter <= $this->page + $adjacents; $counter++)<br /> {<br /> if ($counter == $this->page)<br /> {<br /> $pagination .= '<span class="current">'.$counter.'</span>';<br /> }<br /> else<br /> {<br /> $pagination .= '<a href="'.$this->targetpage.$counter.'">'.$counter.'</a>';<br /> }<br /> }<br /> $pagination .= '...';<br /> $pagination .= '<a href="'.$this->targetpage.$lpm1.'">'.$lpm1.'</a>';<br /> $pagination .= '<a href="'.$this->targetpage.$lastpage.'">'.$lastpage.'</a>';<br /> }<br /> else //close to end; only hide early pages<br /> {<br /> $pagination .= '<a href="'.$this->targetpage.'">1</a>';<br /> $pagination .= '<a href="'.$this->targetpage.'">2</a>';<br /> $pagination .= '...';<br /> for ($counter = $lastpage - (1 + ($adjacents * 3)); $counter <= $lastpage; $counter++)<br /> {<br /> if ($counter == $this->page)<br /> {<br /> $pagination .= '<span class="current">'.$counter.'</span>';<br /> }<br /> else<br /> {<br /> $pagination .= '<a href="'.$this->targetpage.$counter.'">'.$counter.'</a>';<br /> }<br /> }<br /> }<br /> }<br /> <br /> //next button<br /> if ( $this->page < $counter - 1 )<br /> {<br /> $pagination .= '<a href="'.$this->targetpage . $next.'"> Next </a>';<br /> }<br /> else<br /> {<br /> $pagination .= '<span class="disabled"> Next </span>';<br /> }<br /> <br /> $pagination .= '</div>';<br /> }<br /> <br /> return $pagination;<br /> }<br /> </font></p> <font color="#800080">}<br /> ?></font><br /> <br /> Es necesario agregar unas clases CSS para que se vea estilo Digg:<br /> <br /> <font color="#008080">/* Holly Hack for IE \*/<br /> * html .suckertreemenu ul li { float: left; height: 1%; }<br /> * html .suckertreemenu ul li a { height: 1%; }<br /> /* End */<br /> <br /> /** PAGINATION ***/<br /> <br /> div.pagination {<br /> padding: 3px;<br /> margin: 3px;<br /> font-size:8pt;<br /> }<br /> <br /> div.pagination a {<br /> padding: 2px 5px 2px 5px;<br /> margin: 2px;<br /> border: 1px solid #fe640d;<br /> text-decoration: none; /* no underline */<br /> font-size:8pt;<br /> color: #fe640d;<br /> }<br /> div.pagination a:hover, div.pagination a:active {<br /> border: 1px solid #000099;<br /> color: #000;<br /> }<br /> div.pagination span.current {<br /> padding: 2px 5px 2px 5px;<br /> margin: 2px;<br /> border: 1px solid #fe640d;<br /> font-weight: bold;<br /> background-color: #fe640d;<br /> color: #fff;<br /> }<br /> div.pagination span.disabled {<br /> padding: 2px 5px 2px 5px;<br /> margin: 2px;<br /> border: 1px solid #EEE;<br /> color: #c0c0c0;<br /> }</font>", "created" => "2007-06-05 03:22:15-05", "discution" => 1, "display" => 2, "status" => 1, "user_id" => 1, "cv" => 1, "visits" => 0, "rank" => 2174, "editor" => 1, "updated" => "2009-08-20 00:32:14-05" ), "User" => array( "id" => 1, "username" => "aarkerio", "avatar" => "aarkerio_avatar.png" ), "Section" => array( "id" => 28, "description" => "Server Side", "img" => "sec-servidor.jpg" ), "Discution" => array( array(), array(), array(), array(), array(), array(), array(), array(), array(), array(), array(), array(), array(), array(), array(), array(), array() ) )include - APP/View/Pages/display.ctp, line 26 View::_render() - CORE/Cake/View/View.php, line 598 View::render() - CORE/Cake/View/View.php, line 365 Controller::render() - CORE/Cake/Controller/Controller.php, line 900 Dispatcher::_invoke() - CORE/Cake/Routing/Dispatcher.php, line 114 Dispatcher::dispatch() - CORE/Cake/Routing/Dispatcher.php, line 89 [main] - APP/webroot/index.php, line 81
Notice (8): Undefined index: cv [APP/View/Pages/display.ctp, line 26]Code Contextecho $this->Html->div('cv');echo "Ficha del autor:<br /><span class=\"login\">".$data['User']['username'].'</span><br />';echo "<b>". str_replace('@', '_ARRROBA_', $data['User']['email'])."</b><br />".$data['User']['cv'].'<br />';$___viewFn = "/var/chipotle/sites/cakephp/centauro/View/Pages/display.ctp" $___dataForView = array( "data" => array( "Page" => array(), "User" => array(), "Section" => array(), "Discution" => array() ) ) $data = array( "Page" => array( "id" => 785, "section_id" => 28, "title" => "CakePHP pagination component", "body" => "<p>Paginación es simplemente presentar una tabla grande en pantallas, es decir poder "hojear" los datos. <br /> <br /> <a href="http://www.strangerstudios.com/sandbox/pagination/diggstyle.php"> Stranger Studios </a>tiene un tutorial sobre como implementar una paginación estilo Digg, aqui siemplemente adapto el código a un componente. <br /> </p> <p><br /> El componente se usa en el controlador agregándolo al array $components:<br /> <br /> <strong><font color="#333300">public $components = array('Portal', 'Mypagination', 'Security');</font></strong><br /> <br /> por ejemplo en el método view() del controlador news_controller.php el paginador se usa asi:<br /> <br /> <font color="#800000">public function view($page=1) <br /> { <br /> </font><font color="#800000">$this->pageTitle = 'Hacktivismo::Mononeurona';<br /> </font></p> <p> <font color="#800000"><br /> </font><font color="#800000"> //pagination<br /> $total_rows = $this->News->findCount(array("status"=>1)); <br /> <br /> $lmt = 7; // limit news<br /> <br /> $targetpage = "/news/view/"; // URL, this class->method<br /> <br /> $pagination = $this->Mypagination->init($total_rows, $page, $lmt, $targetpage);<br /> <br /> <strong> $this->set('pagination', $pagination);</strong><br /> <br /> $conditions = array("News.status"=>1);<br /> <br /> $fields = array("News.id", "News.title", "News.body", "News.created", "Theme.img", "Theme.title");<br /> <br /> $order = "News.id DESC";<br /> <br /> $offset = (($page * $lmt) - $lmt);<br /> <br /> <strong>$limit = $lmt . " OFFSET " . $offset;</strong><br /> <br /> $this->set('data', $this->News->findAll($conditions, $fields, $order, $limit));<br /> }<br /> </font><br /> Necesito OFFSET porque en PostgreSQL se debe declarar de manera explicita, pero si estás en MySQL u Oracle debes quitar el "OFFSET" en $limit.<br /> <br /> <font color="#800000">$this->Mypagination->init</font> regresa el string $pagination, que se incluye en la vista <strong>/app/views/news/view.thtml</strong>:<br /> <font color="#808000"><br /> < ? php <br /> //pagination<br /> echo $pagination; <br /> ?></font><br /> <br /> El código del componente es este:<br /> <br /> <font color="#800080"> < ? php<br /> /**<br /> * Pagination Component, responsible for managing the DATA required for pagination.<br /> * Manuel Montoya GPLv3<br /> * Chipotle Software 2007<br /> * manuel-AT-mononeurona-DOT-org<br /> */<br /> class MypaginationComponent extends Object<br /> {<br /> //number of rows (items) returned by the query.<br /> private $total_rows;<br /> <br /> //limit<br /> private $limit;<br /> <br /> // $page is the current page number<br /> public $page;<br /> <br /> //URL<br /> public $targetpage;<br /> <br /> /**<br /> * Startup - Link the component to the controller.<br /> *<br /> * @param controller<br /> */<br /> public function startup(&$controller)<br /> {<br /> $this->controller =& $controller;<br /> }<br /> <br /> /**<br /> * Initialize the pagination data.<br /> *<br /> * @param unknown<br /> * @param array<br /> * @options array<br /> * @return array<br /> */<br /> function init($tr, $page, $limit,$targetpage)<br /> {<br /> $this->total_rows = (int) $tr;<br /> $this->page = (int) $page;<br /> $this->limit = (int) $limit;<br /> $this->targetpage = (string) $targetpage;<br /> <br /> return $this->getPaginationString();<br /> <br /> }<br /> <br /> //function to return the pagination string</font></p> <p><font color="#800080">private function getPaginationString($adjacents = 1)<br /> { <br /> // NEXT CODE BY Stranger Studios http://www.strangerstudios.com/sandbox/pagination/diggstyle.php<br /> //other vars<br /> $prev = $this->page - 1; //previous page is page - 1<br /> $next = $this->page + 1; //next page is page + 1<br /> $lastpage = ceil($this->total_rows / $this->limit); //lastpage is = total items / items per page, rounded up.<br /> //exit($lastpage);<br /> $lpm1 = $lastpage - 1; //last page minus 1<br /> <br /> /* <br /> Now we apply our rules and draw the pagination object. <br /> We're actually saving the code to a variable in case we want to draw it more than once.<br /> */<br /> $pagination = "";<br /> <br /> if ($lastpage > 1)<br /> { <br /> $pagination .= '<div class="pagination">'; //just starting<br /> <br /> //previous button<br /> if ($this->page > 1)<br /> {<br /> $pagination .= '<a href="'. $this->targetpage. $prev .'"> prev</a>';<br /> }<br /> else<br /> {<br /> $pagination .= '<span class="disabled">prev</span>'; // no such prev page<br /> }<br /> <br /> //pages <br /> if ( $lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up<br /> { <br /> for ($counter = 1; $counter <= $lastpage; $counter++)<br /> {<br /> if ($counter == $this->page)<br /> {<br /> $pagination .= '<span class="current">'.$counter.'</span>'; //current<br /> }<br /> else<br /> {<br /> $pagination .= '<a href="'.$this->targetpage . $counter . '">'.$counter.'</a>';<br /> }<br /> }<br /> }<br /> elseif ($lastpage >= 7 + ($adjacents * 2)) //enough pages to hide some<br /> {<br /> //close to beginning; only hide later pages<br /> if ($this->page < 1 + ($adjacents * 3)) <br /> {<br /> for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)<br /> {<br /> if ($counter == $this->page)<br /> {<br /> $pagination .= '<span class="current">'.$counter.'</span>';<br /> }<br /> else<br /> {<br /> $pagination .= '<a href="'.$this->targetpage . $counter.'">'.$counter.'</a>';<br /> }<br /> }<br /> $pagination .= "...";<br /> $pagination .= '<a href="'.$this->targetpage . $lpm1 .'">'.$lpm1.'</a>';<br /> $pagination .= '<a href="'.$this->targetpage. $lastpage.'">'.$lastpage.'</a>'; <br /> }<br /> //in middle; hide some front and some back<br /> elseif ($lastpage - ($adjacents * 2) > $this->page && $this->page > ($adjacents * 2))<br /> {<br /> $pagination .= '<a href="'.$this->targetpage.'">1</a>';<br /> $pagination .= '<a href="'.$this->targetpage.'">2</a>';<br /> $pagination .= '...';<br /> for ($counter = $this->page - $adjacents; $counter <= $this->page + $adjacents; $counter++)<br /> {<br /> if ($counter == $this->page)<br /> {<br /> $pagination .= '<span class="current">'.$counter.'</span>';<br /> }<br /> else<br /> {<br /> $pagination .= '<a href="'.$this->targetpage.$counter.'">'.$counter.'</a>';<br /> }<br /> }<br /> $pagination .= '...';<br /> $pagination .= '<a href="'.$this->targetpage.$lpm1.'">'.$lpm1.'</a>';<br /> $pagination .= '<a href="'.$this->targetpage.$lastpage.'">'.$lastpage.'</a>';<br /> }<br /> else //close to end; only hide early pages<br /> {<br /> $pagination .= '<a href="'.$this->targetpage.'">1</a>';<br /> $pagination .= '<a href="'.$this->targetpage.'">2</a>';<br /> $pagination .= '...';<br /> for ($counter = $lastpage - (1 + ($adjacents * 3)); $counter <= $lastpage; $counter++)<br /> {<br /> if ($counter == $this->page)<br /> {<br /> $pagination .= '<span class="current">'.$counter.'</span>';<br /> }<br /> else<br /> {<br /> $pagination .= '<a href="'.$this->targetpage.$counter.'">'.$counter.'</a>';<br /> }<br /> }<br /> }<br /> }<br /> <br /> //next button<br /> if ( $this->page < $counter - 1 )<br /> {<br /> $pagination .= '<a href="'.$this->targetpage . $next.'"> Next </a>';<br /> }<br /> else<br /> {<br /> $pagination .= '<span class="disabled"> Next </span>';<br /> }<br /> <br /> $pagination .= '</div>';<br /> }<br /> <br /> return $pagination;<br /> }<br /> </font></p> <font color="#800080">}<br /> ?></font><br /> <br /> Es necesario agregar unas clases CSS para que se vea estilo Digg:<br /> <br /> <font color="#008080">/* Holly Hack for IE \*/<br /> * html .suckertreemenu ul li { float: left; height: 1%; }<br /> * html .suckertreemenu ul li a { height: 1%; }<br /> /* End */<br /> <br /> /** PAGINATION ***/<br /> <br /> div.pagination {<br /> padding: 3px;<br /> margin: 3px;<br /> font-size:8pt;<br /> }<br /> <br /> div.pagination a {<br /> padding: 2px 5px 2px 5px;<br /> margin: 2px;<br /> border: 1px solid #fe640d;<br /> text-decoration: none; /* no underline */<br /> font-size:8pt;<br /> color: #fe640d;<br /> }<br /> div.pagination a:hover, div.pagination a:active {<br /> border: 1px solid #000099;<br /> color: #000;<br /> }<br /> div.pagination span.current {<br /> padding: 2px 5px 2px 5px;<br /> margin: 2px;<br /> border: 1px solid #fe640d;<br /> font-weight: bold;<br /> background-color: #fe640d;<br /> color: #fff;<br /> }<br /> div.pagination span.disabled {<br /> padding: 2px 5px 2px 5px;<br /> margin: 2px;<br /> border: 1px solid #EEE;<br /> color: #c0c0c0;<br /> }</font>", "created" => "2007-06-05 03:22:15-05", "discution" => 1, "display" => 2, "status" => 1, "user_id" => 1, "cv" => 1, "visits" => 0, "rank" => 2174, "editor" => 1, "updated" => "2009-08-20 00:32:14-05" ), "User" => array( "id" => 1, "username" => "aarkerio", "avatar" => "aarkerio_avatar.png" ), "Section" => array( "id" => 28, "description" => "Server Side", "img" => "sec-servidor.jpg" ), "Discution" => array( array(), array(), array(), array(), array(), array(), array(), array(), array(), array(), array(), array(), array(), array(), array(), array(), array() ) )include - APP/View/Pages/display.ctp, line 26 View::_render() - CORE/Cake/View/View.php, line 598 View::render() - CORE/Cake/View/View.php, line 365 Controller::render() - CORE/Cake/Controller/Controller.php, line 900 Dispatcher::_invoke() - CORE/Cake/Routing/Dispatcher.php, line 114 Dispatcher::dispatch() - CORE/Cake/Routing/Dispatcher.php, line 89 [main] - APP/webroot/index.php, line 81
Notice (8): Undefined index: quote [APP/View/Pages/display.ctp, line 30]Code Contextecho $this->Html->link($this->Html->image('avatars/'.$data['User']['avatar'], array('class'=>'imgborder', 'alt'=>$data['User']['username'], 'title'=>$data['User']['username'])), '/blog/'.$data['User']['username'], array('escape'=>False)) . "<br />";echo '<i>'.$data['User']['quote'].'</i><br />';$___viewFn = "/var/chipotle/sites/cakephp/centauro/View/Pages/display.ctp" $___dataForView = array( "data" => array( "Page" => array(), "User" => array(), "Section" => array(), "Discution" => array() ) ) $data = array( "Page" => array( "id" => 785, "section_id" => 28, "title" => "CakePHP pagination component", "body" => "<p>Paginación es simplemente presentar una tabla grande en pantallas, es decir poder "hojear" los datos. <br /> <br /> <a href="http://www.strangerstudios.com/sandbox/pagination/diggstyle.php"> Stranger Studios </a>tiene un tutorial sobre como implementar una paginación estilo Digg, aqui siemplemente adapto el código a un componente. <br /> </p> <p><br /> El componente se usa en el controlador agregándolo al array $components:<br /> <br /> <strong><font color="#333300">public $components = array('Portal', 'Mypagination', 'Security');</font></strong><br /> <br /> por ejemplo en el método view() del controlador news_controller.php el paginador se usa asi:<br /> <br /> <font color="#800000">public function view($page=1) <br /> { <br /> </font><font color="#800000">$this->pageTitle = 'Hacktivismo::Mononeurona';<br /> </font></p> <p> <font color="#800000"><br /> </font><font color="#800000"> //pagination<br /> $total_rows = $this->News->findCount(array("status"=>1)); <br /> <br /> $lmt = 7; // limit news<br /> <br /> $targetpage = "/news/view/"; // URL, this class->method<br /> <br /> $pagination = $this->Mypagination->init($total_rows, $page, $lmt, $targetpage);<br /> <br /> <strong> $this->set('pagination', $pagination);</strong><br /> <br /> $conditions = array("News.status"=>1);<br /> <br /> $fields = array("News.id", "News.title", "News.body", "News.created", "Theme.img", "Theme.title");<br /> <br /> $order = "News.id DESC";<br /> <br /> $offset = (($page * $lmt) - $lmt);<br /> <br /> <strong>$limit = $lmt . " OFFSET " . $offset;</strong><br /> <br /> $this->set('data', $this->News->findAll($conditions, $fields, $order, $limit));<br /> }<br /> </font><br /> Necesito OFFSET porque en PostgreSQL se debe declarar de manera explicita, pero si estás en MySQL u Oracle debes quitar el "OFFSET" en $limit.<br /> <br /> <font color="#800000">$this->Mypagination->init</font> regresa el string $pagination, que se incluye en la vista <strong>/app/views/news/view.thtml</strong>:<br /> <font color="#808000"><br /> < ? php <br /> //pagination<br /> echo $pagination; <br /> ?></font><br /> <br /> El código del componente es este:<br /> <br /> <font color="#800080"> < ? php<br /> /**<br /> * Pagination Component, responsible for managing the DATA required for pagination.<br /> * Manuel Montoya GPLv3<br /> * Chipotle Software 2007<br /> * manuel-AT-mononeurona-DOT-org<br /> */<br /> class MypaginationComponent extends Object<br /> {<br /> //number of rows (items) returned by the query.<br /> private $total_rows;<br /> <br /> //limit<br /> private $limit;<br /> <br /> // $page is the current page number<br /> public $page;<br /> <br /> //URL<br /> public $targetpage;<br /> <br /> /**<br /> * Startup - Link the component to the controller.<br /> *<br /> * @param controller<br /> */<br /> public function startup(&$controller)<br /> {<br /> $this->controller =& $controller;<br /> }<br /> <br /> /**<br /> * Initialize the pagination data.<br /> *<br /> * @param unknown<br /> * @param array<br /> * @options array<br /> * @return array<br /> */<br /> function init($tr, $page, $limit,$targetpage)<br /> {<br /> $this->total_rows = (int) $tr;<br /> $this->page = (int) $page;<br /> $this->limit = (int) $limit;<br /> $this->targetpage = (string) $targetpage;<br /> <br /> return $this->getPaginationString();<br /> <br /> }<br /> <br /> //function to return the pagination string</font></p> <p><font color="#800080">private function getPaginationString($adjacents = 1)<br /> { <br /> // NEXT CODE BY Stranger Studios http://www.strangerstudios.com/sandbox/pagination/diggstyle.php<br /> //other vars<br /> $prev = $this->page - 1; //previous page is page - 1<br /> $next = $this->page + 1; //next page is page + 1<br /> $lastpage = ceil($this->total_rows / $this->limit); //lastpage is = total items / items per page, rounded up.<br /> //exit($lastpage);<br /> $lpm1 = $lastpage - 1; //last page minus 1<br /> <br /> /* <br /> Now we apply our rules and draw the pagination object. <br /> We're actually saving the code to a variable in case we want to draw it more than once.<br /> */<br /> $pagination = "";<br /> <br /> if ($lastpage > 1)<br /> { <br /> $pagination .= '<div class="pagination">'; //just starting<br /> <br /> //previous button<br /> if ($this->page > 1)<br /> {<br /> $pagination .= '<a href="'. $this->targetpage. $prev .'"> prev</a>';<br /> }<br /> else<br /> {<br /> $pagination .= '<span class="disabled">prev</span>'; // no such prev page<br /> }<br /> <br /> //pages <br /> if ( $lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up<br /> { <br /> for ($counter = 1; $counter <= $lastpage; $counter++)<br /> {<br /> if ($counter == $this->page)<br /> {<br /> $pagination .= '<span class="current">'.$counter.'</span>'; //current<br /> }<br /> else<br /> {<br /> $pagination .= '<a href="'.$this->targetpage . $counter . '">'.$counter.'</a>';<br /> }<br /> }<br /> }<br /> elseif ($lastpage >= 7 + ($adjacents * 2)) //enough pages to hide some<br /> {<br /> //close to beginning; only hide later pages<br /> if ($this->page < 1 + ($adjacents * 3)) <br /> {<br /> for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)<br /> {<br /> if ($counter == $this->page)<br /> {<br /> $pagination .= '<span class="current">'.$counter.'</span>';<br /> }<br /> else<br /> {<br /> $pagination .= '<a href="'.$this->targetpage . $counter.'">'.$counter.'</a>';<br /> }<br /> }<br /> $pagination .= "...";<br /> $pagination .= '<a href="'.$this->targetpage . $lpm1 .'">'.$lpm1.'</a>';<br /> $pagination .= '<a href="'.$this->targetpage. $lastpage.'">'.$lastpage.'</a>'; <br /> }<br /> //in middle; hide some front and some back<br /> elseif ($lastpage - ($adjacents * 2) > $this->page && $this->page > ($adjacents * 2))<br /> {<br /> $pagination .= '<a href="'.$this->targetpage.'">1</a>';<br /> $pagination .= '<a href="'.$this->targetpage.'">2</a>';<br /> $pagination .= '...';<br /> for ($counter = $this->page - $adjacents; $counter <= $this->page + $adjacents; $counter++)<br /> {<br /> if ($counter == $this->page)<br /> {<br /> $pagination .= '<span class="current">'.$counter.'</span>';<br /> }<br /> else<br /> {<br /> $pagination .= '<a href="'.$this->targetpage.$counter.'">'.$counter.'</a>';<br /> }<br /> }<br /> $pagination .= '...';<br /> $pagination .= '<a href="'.$this->targetpage.$lpm1.'">'.$lpm1.'</a>';<br /> $pagination .= '<a href="'.$this->targetpage.$lastpage.'">'.$lastpage.'</a>';<br /> }<br /> else //close to end; only hide early pages<br /> {<br /> $pagination .= '<a href="'.$this->targetpage.'">1</a>';<br /> $pagination .= '<a href="'.$this->targetpage.'">2</a>';<br /> $pagination .= '...';<br /> for ($counter = $lastpage - (1 + ($adjacents * 3)); $counter <= $lastpage; $counter++)<br /> {<br /> if ($counter == $this->page)<br /> {<br /> $pagination .= '<span class="current">'.$counter.'</span>';<br /> }<br /> else<br /> {<br /> $pagination .= '<a href="'.$this->targetpage.$counter.'">'.$counter.'</a>';<br /> }<br /> }<br /> }<br /> }<br /> <br /> //next button<br /> if ( $this->page < $counter - 1 )<br /> {<br /> $pagination .= '<a href="'.$this->targetpage . $next.'"> Next </a>';<br /> }<br /> else<br /> {<br /> $pagination .= '<span class="disabled"> Next </span>';<br /> }<br /> <br /> $pagination .= '</div>';<br /> }<br /> <br /> return $pagination;<br /> }<br /> </font></p> <font color="#800080">}<br /> ?></font><br /> <br /> Es necesario agregar unas clases CSS para que se vea estilo Digg:<br /> <br /> <font color="#008080">/* Holly Hack for IE \*/<br /> * html .suckertreemenu ul li { float: left; height: 1%; }<br /> * html .suckertreemenu ul li a { height: 1%; }<br /> /* End */<br /> <br /> /** PAGINATION ***/<br /> <br /> div.pagination {<br /> padding: 3px;<br /> margin: 3px;<br /> font-size:8pt;<br /> }<br /> <br /> div.pagination a {<br /> padding: 2px 5px 2px 5px;<br /> margin: 2px;<br /> border: 1px solid #fe640d;<br /> text-decoration: none; /* no underline */<br /> font-size:8pt;<br /> color: #fe640d;<br /> }<br /> div.pagination a:hover, div.pagination a:active {<br /> border: 1px solid #000099;<br /> color: #000;<br /> }<br /> div.pagination span.current {<br /> padding: 2px 5px 2px 5px;<br /> margin: 2px;<br /> border: 1px solid #fe640d;<br /> font-weight: bold;<br /> background-color: #fe640d;<br /> color: #fff;<br /> }<br /> div.pagination span.disabled {<br /> padding: 2px 5px 2px 5px;<br /> margin: 2px;<br /> border: 1px solid #EEE;<br /> color: #c0c0c0;<br /> }</font>", "created" => "2007-06-05 03:22:15-05", "discution" => 1, "display" => 2, "status" => 1, "user_id" => 1, "cv" => 1, "visits" => 0, "rank" => 2174, "editor" => 1, "updated" => "2009-08-20 00:32:14-05" ), "User" => array( "id" => 1, "username" => "aarkerio", "avatar" => "aarkerio_avatar.png" ), "Section" => array( "id" => 28, "description" => "Server Side", "img" => "sec-servidor.jpg" ), "Discution" => array( array(), array(), array(), array(), array(), array(), array(), array(), array(), array(), array(), array(), array(), array(), array(), array(), array() ) )include - APP/View/Pages/display.ctp, line 30 View::_render() - CORE/Cake/View/View.php, line 598 View::render() - CORE/Cake/View/View.php, line 365 Controller::render() - CORE/Cake/Controller/Controller.php, line 900 Dispatcher::_invoke() - CORE/Cake/Routing/Dispatcher.php, line 114 Dispatcher::dispatch() - CORE/Cake/Routing/Dispatcher.php, line 89 [main] - APP/webroot/index.php, line 81
Ver todos los articulos de aarkerio
Última actualización: 2009-08-20 00:32:14-05
blog comments powered by Disqus




























