Page 1 of 1

Pager class needed

Posted: Mon Dec 15, 2008 8:11 pm
by alex.barylski
I was planning on using CodeIgnitor to hammer something out quickly but it uses the same namespaces as the other framework -- which is one reason globals are bad -- but since when did CodeIgnitor care about design? :P

Anyways...if anyone know of a project which is a simple class that I can include (without namespace collision problems) configure and execute to generate my links for pagination...I'd appreciate it.

I've searched the boards and Google and found little of interest.

I don't want it to rely on a database connection...it should be totally independent of anything just accept the total pages, etc and generate the HTML as required.

I know it's a common subject here on DN but I'm not looking for implementation ideas I'm just interested in a single class solution...no templates, or anything fancy.

CodeIgnitor would have been perfect had it not been for the tight integration into their own framework either by ignorance or intelligent business decision who knows. :)

Cheers,
Alex

Re: Pager class needed

Posted: Mon Dec 15, 2008 10:51 pm
by Christopher
PCSpectra wrote:I don't want it to rely on a database connection...it should be totally independent of anything just accept the total pages, etc and generate the HTML as required.

I know it's a common subject here on DN but I'm not looking for implementation ideas I'm just interested in a single class solution...no templates, or anything fancy.
I don't think there is a general design to the problem of pagination that is a single class solution. However, what you describe sound like about 20-30 lines of code -- no datasource, just total pages (and page size), the the HTML you like.

And then there is this today: viewtopic.php?p=504616#p504616

Re: Pager class needed

Posted: Mon Dec 15, 2008 11:05 pm
by alex.barylski
I don't think there is a general design to the problem of pagination that is a single class solution. However, what you describe sound like about 20-30 lines of code -- no datasource, just total pages (and page size), the
Never said there was...how you implement is depends on a many factors/variables...although the more loosely coupled it is from dependencies, certainly the more reusable it is.

Thankfully Joomla has a pager class...I just need it to work I could care less about the elegant design in this case. :P

Re: Pager class needed

Posted: Mon Dec 15, 2008 11:35 pm
by Chris Corbyn
There shouldn't really be any dependencies for a pager class if my thinking is correct. All it needs is numbers... it's just a calculator for offsets and ranges.

Code: Select all

class PagingModel {
  __construct($totalRecord, $recordsPerPage, $currentPage) {
  }
 
  getCurrentPage() {
  }
  
  getLastPage() {
  }
  
  getFirstPage() {
  }
  
  getStartOffset() {
  }
  
  getEndOffset() {
  }
}

Code: Select all

$pagingModel = new PagingModel(count($recordSet), 25, $request->get('pagenum'));
$pagedRecordSet = $recordSet->getRange($pagingModel->getStartOffset(), $pagingModel->getEndOffset());
This assumes that your record set is not materializing records before it needs to use them, but the general approach is always this way... the paging is numbers-only. It's easy to output the links based on what getCurrentPage(), getLastPage() and getFirstPage() return.

Re: Pager class needed

Posted: Tue Dec 16, 2008 12:05 am
by wei

Re: Pager class needed

Posted: Tue Dec 16, 2008 12:49 am
by alex.barylski
There shouldn't really be any dependencies for a pager class if my thinking is correct.
I would be inclined to agree, however I have seen many, many many pagers which:

1. Relied on a data source (database, csv, etc)
2. Relied on a specific client view medium (HTML, etc)

Each implementation has it's advantages/disadvantages, however I usually want something simple...and in this case didn't want to implement anything, no HTML or calculations, etc...I just wanted to call a simple class and invoke it's render() like method.

Re: Pager class needed

Posted: Tue Dec 16, 2008 10:20 pm
by TottyAndBaty

Code: Select all

 
//this is discuz'pager class
function multi($mpurl,$page = 10)
 { 
$multipage = ''; 
$mpurl .= strpos($mpurl, '?') ? '&' : '?'; 
$realpages = 1; 
if($this->infocount > $this->items) 
{ 
    $offset = 2; 
    $realpages = @ceil($this->infocount / $this->items); 
    $pages = $this->maxpages && $this->maxpages < $realpages ? $this->maxpages : $realpages; 
if($page > $pages) 
{ 
$from = 1; 
$to = $pages; 
} 
else 
{ 
    $from = $this->pageno - $offset; 
    $to = $from + $page - 1; 
    if($from < 1)
     { 
        $to = $this->pageno + 1 - $from; 
        $from = 1; 
        if($to - $from < $page) 
        { 
            $to = $page; 
        } 
    } 
    elseif($to > $pages) 
    { 
        $from = $pages - $page + 1; 
        $to = $pages; 
    } 
} 
$multipage = ($this->pageno - $offset > 1 && $pages > $page ? '<a href="'.$mpurl.'page=1" class="first">1 ...</a>' : ''). 
($this->pageno > 1 ? '<a href="'.$mpurl.'page='.($this->pageno - 1).'" class="p_redirect"><<</a>' : ''); 
for($i = $from; $i <= $to; $i++)
 { 
    $multipage .= $i == $this->pageno ? '<a class="p_curpage"><strong>'.$i.'</strong></a>' :'<a href="'.$mpurl.'page='.$i.'" class="p_num">'.$i.'</a>'; 
} 
$multipage .= ($this->pageno < $pages ? '<a href="'.$mpurl.'page='.($this->pageno + 1).'" class="p_redirect">>></a>' : ''). 
($to < $pages ? '<a href="'.$mpurl.'page='.$pages.'" class="last">... '.$realpages.'</a>' : ''). 
($pages > $page ? '<kbd><input type="text" name="custompage" size="3" onkeydown="if(event.keyCode==13) {window.location=\''.$mpurl.'page=\'+this.value; return false;}" /></kbd>' : ''); 
$multipage = $multipage ? '<div class="p_bar"><a class="p_total"> '.$this->infocount.' </a><a class="p_pages"> '.$this->pageno.'/'.$pages.' </a>'.$multipage.'</div>' : ''; 
} 
return $multipage; 
}