Pager class needed

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Pager class needed

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Pager class needed

Post 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
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Pager class needed

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Pager class needed

Post 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.
wei
Forum Contributor
Posts: 140
Joined: Wed Jul 12, 2006 12:18 am

Re: Pager class needed

Post by wei »

alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Pager class needed

Post 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.
TottyAndBaty
Forum Newbie
Posts: 2
Joined: Tue Dec 16, 2008 10:13 pm
Location: CHINA

Re: Pager class needed

Post 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; 
}
 
 
 
 
Post Reply