Jump to content

Pagination output


Go to solution Solved by requinix,

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
https://forums.phpfreaks.com/topic/303824-pagination-output/
Share on other sites

  • Solution
$page_nums = range($lower_limit, $upper_limit);
range() supports counting down, such as from 1 to 0. Add some logic in there to make sure that doesn't happen; I suggest returning an empty array if num_rows == 0.
Link to comment
https://forums.phpfreaks.com/topic/303824-pagination-output/#findComment-1545971
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.