Jump to content

Pagination output


Brian_15

Recommended Posts

Need help. This code will generate pagination pages if no result from db.

<?php

class Pagination
{
    protected $num_rows;
    protected $current_page;
    protected $ipp;
    protected $ipps = array(10, 25, 50, 75, 100);
    protected $max_links;
    
    public function __construct($number_of_rows, $current_page, $items_per_page, int $max_links = 9)
    {
        $this->num_rows = $number_of_rows;
        $this->current_page = $current_page;
        $this->ipp = $items_per_page;
        $this->max_links = $max_links;
    }
    
    public function itemsPerPage() 
    {
        return in_array($this->ipp, $this->ipps) ? $this->ipp : 10;
    }
    
    public function offset()
    {
         return ($this->currentPage() - 1) * $this->itemsPerPage();
    }
    
    public function currentPage()
    {
        if ($this->current_page >= $this->totalPages())
        {
            return $this->totalPages();
        }
        elseif ($this->current_page <= 1) 
        {
            return 1;
        }
        else
        {
            return $this->current_page;
        }
    }
    
    public function totalPages() 
    {
        return ceil($this->num_rows / $this->itemsPerPage());
    }
    
    /* ----------------- other code ---------------- */
    
    public function pages()
    {
        $range = floor(($this->max_links - 1) / 2);
        $lower_limit = max($this->currentPage() - $range, 1);
        $upper_limit = min($this->totalPages(), $this->currentPage() + $range);
        $page_nums = range($lower_limit, $upper_limit);
			
        $j = 0;
        foreach ($page_nums as $i) 
        {
            $page_links[$j] = $i;
            $j++;
        }
        return $page_links;
    }
}

$row_count_from_db = 0;
$pagination = new Pagination($row_count_from_db, $_GET['page'] ?? '', $_GET['ipp'] ?? '', 11);
var_dump($pagination->pages());

/* output

array(2) {
  [0]=>
  float(1)
  [1]=>
  float(0)
}

post-199680-0-82307100-1493397485_thumb.png

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.