Buenas,
Dentro de una clase, tengo una función para mostrar la paginacion de una tabla (de la que obtengo los datos de mysql).
El caso es que funciona, no me da problemas... pero me parece un método feo, osco, y que no me termina de convencer:
Código PHP:
function getPages() {
if(!$this->_pagination_enabled) return "";
if($this->_table_total_rows > 0 && $this->_table_total_rows > $this->_limit) {
$total_paginas = ceil($this->_table_total_rows / $this->_limit);
$paginator = '<ul class="pagination pull-right" style="margin: 0px;">';
if($this->_page != 1) $paginator .= '<li><a href="#" class="datatable_reload" data-id="'.$this->_id.'" data-source="'.$this->_source.'" data-search="'.$this->_search_query.'" data-page="1">«</a></li>';
// <li><a href="#">3</a></li>
if($total_paginas <= 5) {
for($i = 1; $i < 6; $i++) {
$cl = "";
if($i == $this->_page) $cl = "class='active'";
$paginator .= '<li '.$cl.'><a href="#" class="datatable_reload" data-id="'.$this->_id.'" data-source="'.$this->_source.'" data-search="'.$this->_search_query.'" data-page="'.$i.'">'.$i.'</a></li>';
}
}
else {
if($this->_page > 2 && $this->_page < ($total_paginas - 2)) {
$paginator .= '<li><a href="#" class="datatable_reload" data-source="'.$this->_source.'" data-id="'.$this->_id.'" data-search="'.$this->_search_query.'" data-page="'.($this->_page - 2).'">'.($this->_page - 2).'</a></li>';
$paginator .= '<li><a href="#" class="datatable_reload" data-source="'.$this->_source.'" data-id="'.$this->_id.'" data-search="'.$this->_search_query.'" data-page="'.($this->_page - 1).'">'.($this->_page - 1).'</a></li>';
$paginator .= '<li class="active" ><a href="#" class="datatable_reload" data-id="'.$this->_id.'" data-source="'.$this->_source.'" data-search="'.$this->_search_query.'" data-page="'.($this->_page).'">'.($this->_page).'</a></li>';
$paginator .= '<li><a href="#" class="datatable_reload" data-source="'.$this->_source.'" data-id="'.$this->_id.'" data-search="'.$this->_search_query.'" data-page="'.($this->_page + 1).'">'.($this->_page + 1).'</a></li>';
$paginator .= '<li><a href="#" class="datatable_reload" data-source="'.$this->_source.'" data-id="'.$this->_id.'" data-search="'.$this->_search_query.'" data-page="'.($this->_page + 2).'">'.($this->_page + 2).'</a></li>';
}
else if($this->_page == 1) {
$paginator .= '<li class="active"><a href="#" class="datatable_reload" data-id="'.$this->_id.'" data-source="'.$this->_source.'" data-search="'.$this->_search_query.'" data-page="'.($this->_page).'">'.($this->_page).'</a></li>';
if($this->_page + 1 <= $total_paginas) $paginator .= '<li><a href="#" class="datatable_reload" data-id="'.$this->_id.'" data-source="'.$this->_source.'" data-search="'.$this->_search_query.'" data-page="'.($this->_page + 1).'">'.($this->_page + 1).'</a></li>';
if($this->_page + 2 <= $total_paginas) $paginator .= '<li><a href="#" class="datatable_reload" data-id="'.$this->_id.'" data-source="'.$this->_source.'" data-search="'.$this->_search_query.'" data-page="'.($this->_page + 2).'">'.($this->_page + 2).'</a></li>';
if($this->_page + 3 <= $total_paginas) $paginator .= '<li><a href="#" class="datatable_reload" data-id="'.$this->_id.'" data-source="'.$this->_source.'" data-search="'.$this->_search_query.'" data-page="'.($this->_page + 3).'">'.($this->_page + 3).'</a></li>';
if($this->_page + 4 <= $total_paginas) $paginator .= '<li><a href="#" class="datatable_reload" data-id="'.$this->_id.'" data-source="'.$this->_source.'" data-search="'.$this->_search_query.'" data-page="'.($this->_page + 4).'">'.($this->_page + 4).'</a></li>';
}
else if($this->_page == $total_paginas) {
if($this->_page - 4 > 0) $paginator .= '<li><a href="#" class="datatable_reload" data-id="'.$this->_id.'" data-source="'.$this->_source.'" data-search="'.$this->_search_query.'" data-page="'.($this->_page - 4).'">'.($this->_page - 4).'</a></li>';
if($this->_page - 3 > 0) $paginator .= '<li><a href="#" class="datatable_reload" data-id="'.$this->_id.'" data-source="'.$this->_source.'" data-search="'.$this->_search_query.'" data-page="'.($this->_page - 3).'">'.($this->_page - 3).'</a></li>';
if($this->_page - 2 > 0) $paginator .= '<li><a href="#" class="datatable_reload" data-id="'.$this->_id.'" data-source="'.$this->_source.'" data-search="'.$this->_search_query.'" data-page="'.($this->_page - 2).'">'.($this->_page - 2).'</a></li>';
if($this->_page - 1 > 0) $paginator .= '<li><a href="#" class="datatable_reload" data-id="'.$this->_id.'" data-source="'.$this->_source.'" data-search="'.$this->_search_query.'" data-page="'.($this->_page - 1).'">'.($this->_page - 1).'</a></li>';
$paginator .= '<li class="active" ><a href="#" class="datatable_reload" data-source="'.$this->_source.'" data-id="'.$this->_id.'" data-search="'.$this->_search_query.'" data-page="'.($this->_page).'">'.($this->_page).'</a></li>';
}
else if($this->_page == 2) {
$paginator .= '<li><a href="#" class="datatable_reload" data-source="'.$this->_source.'" data-id="'.$this->_id.'" data-search="'.$this->_search_query.'" data-page="'.($this->_page - 1).'">'.($this->_page - 1).'</a></li>';
$paginator .= '<li class="active"><a href="#" class="datatable_reload" data-source="'.$this->_source.'" data-id="'.$this->_id.'" data-search="'.$this->_search_query.'" data-page="'.($this->_page).'">'.($this->_page).'</a></li>';
if($this->_page + 1 <= $total_paginas) $paginator .= '<li><a href="#" class="datatable_reload" data-id="'.$this->_id.'" data-source="'.$this->_source.'" data-search="'.$this->_search_query.'" data-page="'.($this->_page + 1).'">'.($this->_page + 1).'</a></li>';
if($this->_page + 2 <= $total_paginas) $paginator .= '<li><a href="#" class="datatable_reload" data-id="'.$this->_id.'" data-source="'.$this->_source.'" data-search="'.$this->_search_query.'" data-page="'.($this->_page + 2).'">'.($this->_page + 2).'</a></li>';
if($this->_page + 3 <= $total_paginas) $paginator .= '<li><a href="#" class="datatable_reload" data-id="'.$this->_id.'" data-source="'.$this->_source.'" data-search="'.$this->_search_query.'" data-page="'.($this->_page + 3).'">'.($this->_page + 3).'</a></li>';
}
else if($this->_page == ($total_paginas - 1)) {
if($this->_page - 3 > 0) $paginator .= '<li><a href="#" class="datatable_reload" data-id="'.$this->_id.'" data-source="'.$this->_source.'" data-search="'.$this->_search_query.'" data-page="'.($this->_page - 3).'">'.($this->_page - 3).'</a></li>';
if($this->_page - 2 > 0) $paginator .= '<li><a href="#" class="datatable_reload" data-id="'.$this->_id.'" data-source="'.$this->_source.'" data-search="'.$this->_search_query.'" data-page="'.($this->_page - 2).'">'.($this->_page - 2).'</a></li>';
if($this->_page - 1 > 0) $paginator .= '<li><a href="#" class="datatable_reload" data-id="'.$this->_id.'" data-source="'.$this->_source.'" data-search="'.$this->_search_query.'" data-page="'.($this->_page - 1).'">'.($this->_page - 1).'</a></li>';
$paginator .= '<li class="active"><a href="#" class="datatable_reload" data-id="'.$this->_id.'" data-source="'.$this->_source.'" data-search="'.$this->_search_query.'" data-page="'.($this->_page).'">'.($this->_page).'</a></li>';
if($this->_page + 1 <= $total_paginas) $paginator .= '<li><a href="#" class="datatable_reload" data-id="'.$this->_id.'" data-source="'.$this->_source.'" data-search="'.$this->_search_query.'" data-page="'.($this->_page + 1).'">'.($this->_page + 1).'</a></li>';
}
}
if($this->_page != $total_paginas) $paginator .= '<li><a href="#" class="datatable_reload" data-id="'.$this->_id.'" data-source="'.$this->_source.'" data-search="'.$this->_search_query.'" data-page="'.$total_paginas.'">»</a></li>';
$paginator .= '</ul>';
return $paginator;
}
else return "";
}
Creo que el código se entiende.... pero estoy "bloqueado" en cuanto lo miro. Me sobran condiciones, estoy casi seguro, pero me ofusco. Y eso que es código mío xD
Sé que es algo básico... pero no se me ocurre otra forma de condicionarlo.
¿Alguna idea?
PD: Sólo muestra 5 páginas, e inicio y final en caso de que no estés en ellas!