-1

I want to create a page navigation that has buttons for the current page and the 4 closest existing pages. Here are a few examples.

  • If the current page is 5, then the user should be shown buttons for pages 3, 4, 5, 6, 7.
  • If the current page is 1 - 1, 2, 3, 4, 5
  • If the current page is 8 but there are only 9 pages - 5, 6, 7, 8, 9

I hope you get the idea. The following variables that can be used:

  • $firstPage
  • $previousPage
  • $currentPage
  • $nextPage
  • $lastPage

How would you do this? Or is there a better logic that can be used in regard to which pages to show buttons for in the navigation?

Jeff
  • 165
  • 11
  • 1
    dupe of so many questions –  Jun 19 '16 at 00:31
  • Possible duplicate of [Simple PHP Pagination script](http://stackoverflow.com/questions/3705318/simple-php-pagination-script) – Jeff Jun 19 '16 at 01:08
  • @Jeff Have you read the content of your link or the asked question? This is absolutely not the same. I've been flagged your comment. – keupsonite Jun 19 '16 at 01:23
  • Yes, I've read it. And there are - as @Dagon pointed out - so many examples on here, I just picked the first one. Since this should not be a coding-service I still think this should be closed because of too many dubs. – Jeff Jun 19 '16 at 01:31
  • 1
    I like that @Jeff has engaged on a question asked by Jeff. Reading and understanding the comments is fun! :D – Michael Gaskill Jun 19 '16 at 01:37
  • @MichaelGaskill Yes, I was confused myself (I'm the commenter, not the asker) – Jeff Jun 19 '16 at 09:45

2 Answers2

2

I have done my own Paginator class, it is in english but comments are on spanish.

It's really easy to use. The constructor requires the total of elements to list, current page, elements limit per page, and the section you are in (by section I mean, if you are doing pagination by sections).

So, if you have to paginate 100 items (10 items per page), in "Cars" section and you are on first page, you do this:

$paginator = new Paginador(100, 1, 10, "Cars");
echo $paginator->procesarHTML();

This will echo the HTML. To change the HTML formated modify procesarHTML(). So you will get buttons to each page (<a href="/SECTION/PAGE/#pagina">PAGE</a>). The current page will have a css class attached to it to let the user know in what page it is.

Remember to edit class variable $_secciones. Add the sections you allow to go to.

<?php

/*------------------------------------------------------------------
-- Descripcion: Clase para Paginar con la Clase Template HTML.
-- Version: v 1.1.0 | Ultima version: v 1.0.0
-- Ultima Edicion: 23/05/2016 08:41
--------------------------------------------------------------------
-- Autor: Matias Hernan Lauriti | matiaslauriti@gmail.com
-- Fecha Creacion: 25/05/2014
--------------------------------------------------------------------
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-------------------------------- CSS -------------------------------

    #paginador{ margin-bottom: 5px; }

    #paginador ol{ text-align: center; }

    #paginador ol li{
        margin-top: 8px;
        display: inline-block;
        font-family: Arial;
    }

    #paginador ol li a{
        height: 24px;
        background-color: #454648;
        background-image: linear-gradient(#454648,#2F2F31);
        background-image: -webkit-linear-gradient(#454648,#2F2F31);
        background-image: -moz-linear-gradient(top,#454648,#2F2F31);
        background-image: -o-linear-gradient(top,#454648,#2F2F31);
        background-image: -ms-linear-gradient(top,#454648,#2F2F31);
        box-shadow: 0 2px 5px 0px #000, 0px 1px 0px 0px #5B5C5D inset;
        padding: 3px 8px;
        border-radius: 3px;
        font-size: 12px;
    }

    #paginador ol li a:hover{
        background-color: #FBB92C;
        background-image: linear-gradient(#FBB92C,#D49B29);
        background-image: -webkit-linear-gradient(#FBB92C,#D49B29);
        background-image: -moz-linear-gradient(top,#FBB92C,#D49B29);
        background-image: -o-linear-gradient(top,#FBB92C,#D49B29);
        background-image: -ms-linear-gradient(top,#FBB92C,#D49B29);
        box-shadow: 0 2px 5px 0px #000, 0px 1px 0px 0px #EECF91 inset;
    }

    #paginador ol li a:active{
        background-color: #454648;
        background-image: linear-gradient(#454648,#2F2F31);
        background-image: -webkit-linear-gradient(#454648,#2F2F31);
        background-image: -moz-linear-gradient(top,#454648,#2F2F31);
        background-image: -o-linear-gradient(top,#454648,#2F2F31);
        background-image: -ms-linear-gradient(top,#454648,#2F2F31);
        box-shadow: 0px 0px 1px 0px #262626 inset, 0 -1px 1px 0 #000;
    }

    #paginador ol li a.seleccionado{
        background-color: #272728;
        background-image: linear-gradient(#272728,#2F2F31);
        background-image: -webkit-linear-gradient(#272728,#2F2F31);
        background-image: -moz-linear-gradient(top,#272728,#2F2F31);
        background-image: -o-linear-gradient(top,#272728,#2F2F31);
        background-image: -ms-linear-gradient(top,#272728,#2F2F31);
        box-shadow: 0px 0px 1px 0px #262626 inset, 0 -1px 1px 0 #000;
    }

---------------------------- Utilización ---------------------------

    $paginador = new Paginador($row['total'], PAGINA, 10, SECCION);

    echo $paginador->procesarHTML();

--------------------------------------------------------------------*/

class Paginador {

    /* Variables */

    private $_total = 1;
    private $_paginaActual = 1;
    private $_limitePorPagina = 10;

    private $_paginasTotales = 1;

    private $_seccion = 'noticias'; // Seccion Default
    private $_secciones = array('noticias','videos'); // Secciones que tomo como validas desde 1 hasta n

    /* Metodos */

    public function __construct($total = 1, $paginaActual = 1, $limitePorPagina = 10, $seccion = NULL) {

        $this->_setTotal($total); // Total de elementos a listar

        $this->_setPaginaActual($paginaActual);
        $this->_setLimitePorPagina($limitePorPagina); // Limite de elementos a listar por página

        $this->_setSeccion($seccion); // Sección inicial

        $this->_procesar();

    }

    private function _setPaginaActual($pagina) {

        if(!empty($pagina) && is_numeric($pagina) && $pagina > 0) $this->_paginaActual = $pagina;

    }

    private function _setLimitePorPagina($limite) {

        if(!empty($limite) && is_numeric($limite) && $limite > 0) $this->_limitePorPagina = $limite;

    }

    private function _setTotal($total) {

        if(!empty($total) && is_numeric($total) && $total > 0) $this->_total = $total;

    }

    private function _setSeccion($seccion) {

        if(in_array($seccion,$this->_secciones)) $this->_seccion = $seccion;

    }

    private function _procesar() {

        $this->_paginasTotales = ceil( $this->_total / $this->_limitePorPagina); // Redondeo para arriba, si tengo 1.5 paginas, tengo 2 para mostrar el .5 restante

        if($this->_paginaActual > $this->_paginasTotales) $this->_paginaActual = 1;

    }

    public function getLimit($ordenAscendente = true) {

        if($ordenAscendente == true) {

            if($this->_total - ($this->_paginaActual * $this->_limitePorPagina) < 0) 
                return '0';

            return ($this->_total - ($this->_paginaActual * $this->_limitePorPagina));

        }else
            return (($this->_paginaActual - 1) * $this->_limitePorPagina);

    }

    public function procesarHTML($sufijo = '', $sig_ant = true, $prim_ult = true) {

        $paginadorHTML = NULL;

        if($this->_paginasTotales > 0) {

            $paginadorHTML = '                    <nav id="paginador">';
            $paginadorHTML .= '                        <ol>';

            if($this->_paginasTotales > 2 && $this->_paginaActual > 1 && $prim_ult == true) $paginadorHTML .= '                            <li><a href="/'.$sufijo.$this->_seccion.'/1/#pagina">&lt;&lt; Primera</a></li>';
            if($this->_paginasTotales > 1 && $this->_paginaActual > 1 && $sig_ant == true) $paginadorHTML .= '                            <li><a href="/'.$sufijo.$this->_seccion.'/'.($this->_paginaActual - 1).'/#pagina">&lt;&lt;</a></li>';

            for($i = 1; $i <= $this->_paginasTotales; $i++)
                $paginadorHTML .= '                            <li><a href="/'.$sufijo.$this->_seccion.'/'.$i.'/#pagina"'.($this->_paginaActual == $i ? ' class="seleccionado"' : '').'>'.$i.'</a></li>';

            if($this->_paginasTotales > 1 && $this->_paginaActual < $this->_paginasTotales && $sig_ant == true) $paginadorHTML .= '                            <li><a href="/'.$sufijo.$this->_seccion.'/'.($this->_paginaActual + 1).'/#pagina">&gt;&gt;</a></li>';
            if($this->_paginasTotales > 2 && $this->_paginaActual < $this->_paginasTotales && $prim_ult == true) $paginadorHTML .= '                            <li><a href="/'.$sufijo.$this->_seccion.'/'.$this->_paginasTotales.'/#pagina">Ultima &gt;&gt;</a></li>';

            $paginadorHTML .= '                        </ol>';
            $paginadorHTML .= '                    </nav>';

        }

        return $paginadorHTML;

    }

}

?>
matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
1

Just like that:

<?php

$currentPage = 5;
$lastPage = 9;
$firstPage = 1;
$between = 3;

function output($current, $limit, $between, $isAfter = false)
{
  $i = $isAfter ? $current + 1 : $current - $between;
  $max = $i + $between;

  for ($i; $i < $max; $i++)
    echo "<a href='#'>Page {$i}</a>";
}

if ($currentPage > $firstPage)
  output($currentPage, $lastPage, $between);

echo "Page {$currentPage}!";

if ($currentPage < $lastPage)
  output($currentPage, $lastPage, $between, true);
keupsonite
  • 399
  • 3
  • 15