Ver Mensaje Individual
  #11 (permalink)  
Antiguo 07/01/2014, 17:47
Avatar de guardarmicorreo
guardarmicorreo
 
Fecha de Ingreso: noviembre-2012
Ubicación: Córdoba
Mensajes: 1.153
Antigüedad: 11 años, 5 meses
Puntos: 84
Respuesta: Paginador: Aporte y también pido consejo para mejorarlo

Código PHP:
Ver original
  1. /*
  2.      * crea el boton anterior
  3.      */
  4.     public  function    beforeButtons()
  5.     {
  6.         //resta 1 a la pagina actual
  7.         $pageBefore  =   ceil($this->page  -   1);
  8.        
  9.         //si el resultado anterior indica que no da un valor negativo
  10.         if($pageBefore >=0)
  11.         {
  12.             $this->beforeButton  =   array(
  13.                                                                 'page'  =>$pageBefore,
  14.                                                                 'url'      =>$this->createURL($this->url, $this->id, $pageBefore)
  15.                                                                );
  16.         }
  17.        
  18.         //sino asigna false para no mostrar boton anterior
  19.         else
  20.       {
  21.             $this->beforeButton  =   FALSE;
  22.         }
  23.      
  24.         return $this->beforeButton;
  25.     }
  26.    
  27.     /*
  28.      * crea el boton siguiente
  29.      */
  30.     public  function    afterButtons()
  31.     {  
  32.         //suma a la pagina actual 1
  33.         $afterButton    =   $this->page   +   1;
  34.        
  35.         //si el resultado anterior es menor o igual a las paginas totales posibles
  36.         if($afterButton <  $this->totalPages)
  37.         {
  38.             $this->afterButton  =   array(
  39.                                                              'page'  =>$afterButton,
  40.                                                              'url'      =>$this->createURL($this->url, $this->id, $afterButton)
  41.                                                             );
  42.         }
  43.         else
  44.         {
  45.             $this->afterButton  =   FALSE;
  46.         }
  47.        
  48.         return  $this->afterButton;
  49.     }
  50.    
  51.     /*
  52.      * crea el array definitivo con todos los valores necesarios para todos los botones
  53.      */
  54.     public  function    paginator()
  55.     {
  56.         //comprueba que la pagina que ha enviado el usuario no sea mayor que el total de resultados posibles
  57.         //tampoco que sea inferior a 0
  58.         if($this->page  <=  $this->totalPages   AND $this->page>=0)
  59.         {
  60.             $this->result   =   array(
  61.                                                         'page'=>$this->page,
  62.                                                         'surround_before'   =>$this->surroundPagesBefore(),
  63.                                                         'surround_after'    =>$this->surroundPagesAfter(),
  64.                                                         'before_button' =>$this->beforeButtons(),
  65.                                                         'after_button'  =>$this->afterButtons(),
  66.                                                         'total_pages'   =>$this->totalPages,
  67.                                                         'current_pages' =>$this->currentPages,
  68.                                                         'limit_since' =>$this->currentPages,
  69.                                                         'limit_result_set'  =>$this->resultsPage
  70.                                                     );
  71.                                                                                
  72.         }
  73.        
  74.         //si el usuario intentara romper el paginado poniendo páginas falsas entonces asigna FALSE
  75.         else
  76.       {
  77.             $this->result   =   FALSE;
  78.         }
  79.        
  80.         return  $this->result;
  81.     }
  82.  
  83.     /*
  84.      * busca y sustituye un patron dentro de una URL para
  85.      * eliminar ese patron y sustituirlo para crear una nueva URL
  86.      * evitando repetir el patron en la URL
  87.      *
  88.      * por ejemplo evitar::          ?page=1&page=2&page=3
  89.      */
  90.     public  function    createURL($url, $id, $page)
  91.     {
  92.         //si el identificador no se encuentra en la url
  93.         if(substr_count($url, $id)  ==0)
  94.         {
  95.             //agrega al final de la url el identificador y el valor que le corresponde
  96.             $url  =   $url."&".$id."=".$page;
  97.         }
  98.        
  99.         //sino busca y reemplaza el identificador en la url por su nuevo valor
  100.         else
  101.       {
  102.             $url  =   preg_replace("/(".$id."=[[:digit:]])/i", $id."=".$page, $url);
  103.         }
  104.        
  105.         return  $url;
  106.     }
  107. }
  108. ?>
__________________
Ayúdame a hacerlo por mi mismo.