TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-11-2008, 02:34 PM   #1 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default Pagination class help..

Hey!
I suppose you've read my pagination class (Pagination class), if not, please read it

Anyways, how can I insert an option to have a span in it?
So it only shows the amount of links within the span.

Let's say I set the span to 2, it will only display 2 links on each side of the current page.

First .. 4 5 6 7 8 .. Last
First .. 5 6 7 8 9 .. Last
First .. 32 33 34

__________________
Tanax is offline  
Reply With Quote
Old 02-11-2008, 03:29 PM   #2 (permalink)
The Contributor
 
Gibou's Avatar
 
Join Date: Nov 2007
Location: France, near Paris
Posts: 53
Thanks: 6
Gibou is on a distinguished road
Default

First, you should initialize $currentSpan to 1.
Then, modify your method getPages()
PHP Code:

public function getPages($results) {
            
    
$this->totalResults count($results);
    
$totalPages $this->totalResults $this->totalPerPage;
    
$this->totalPages ceil($totalPages);
            
    
$x 1;
    
$array = array();
          
    while(
$x <= $this->totalPages) {
                
        if(
$x == || $x == $this->totalPages || abs($x-$this->currentPage) == $this->currentSpan) {
            
$array[] = $x;
        }
        else if(
$x == || $x == $this->totalPages// take care here. If $totalPages is too small, it might cause some errors in display.
            
$array[] = "..";
    }
            
    return 
$array;
          

Surely not very optimized but it may works, I think (not tested).
If the current page is the 6th on 10, after calling this method, you will have an array like: 1|..|4|5|6|7|8|..|10
When you display, you will take care to don't make links around ".."

Hoping it will help you a little :)
__________________
Wedus project's Website
Send a message via MSN to Gibou
Gibou is offline  
Reply With Quote
Old 02-11-2008, 07:16 PM   #3 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Uhm, actually I don't really understand your script :O
I mean.. the if-statements, I don't understand
__________________
Tanax is offline  
Reply With Quote
Old 02-11-2008, 08:11 PM   #4 (permalink)
The Contributor
 
Gibou's Avatar
 
Join Date: Nov 2007
Location: France, near Paris
Posts: 53
Thanks: 6
Gibou is on a distinguished road
Default

If I have well understood your class, the getPages() method returns an array with the pages to display.

In this case, the array must return

First .. 4 5 6 7 8 .. Last

So, must return this array (if we have 10 pages) : 1|..|4|5|6|7|8|..|10

the if statement verfies that you store the good pages. Indeed, if you have 10 pages and if the current page is the 6th, you must store:

* the first one: if(x==1
* the last one: $x == $this->totalPages
* and both which are around the current page: abs($x-$this->currentPage) <= $this->currentSpan

Let's see more clearly the third statement:

if currentSpan = 2: the absolute value of the difference between the current page and the value of the loop must be inferior or equal to 2

this is the detail of your while($x <= $this->totalPages) loop:

$x = 1: respects the first statement (x=1) -> we take it !
$x = 2: do not respects any statement -> do not take it
BUT
respects the if(x==2) so we store a ".."
$x = 3: do not respects any statement -> do not take it
$x = 4: respects the third statement (the absolute value of 4-6 is 2 -> the current span) -> we take it
$x = 5: respects the third statement (the absolute value of 5-6 is 1 -> inferior to the current span) -> we take it
$x = 6: respects the third statement (the absolute value of 6-6 is 0 -> inferior to the current span) -> we take it
$x = 7: respects the third statement (the absolute value of 7-6 is 1 -> inferior to the current span) -> we take it
$x = 8: respects the third statement (the absolute value of 8-6 is 2 -> the current span) -> we take it
$x = 9: do not respect any statement -> do not take it
BUT
respects the if(x == $this->totalPages-1) -> we take it
$x = 10: respect the second statement ($x == $this->totalPages) -> we take it

I've made some errors in the previous version. It is more that:

PHP Code:
public function getPages($results) {
            
    
$this->totalResults count($results);
    
$totalPages $this->totalResults $this->totalPerPage;
    
$this->totalPages ceil($totalPages);
            
    
$x 1;
    
$array = array();
          
    while(
$x <= $this->totalPages) {
                
        if(
$x == || $x == $this->totalPages || abs($x-$this->currentPage) <= $this->currentSpan) {
            
$array[] = $x;
        }
        else if(
$x == || $x == ($this->totalPages 1)) // take care here. If $totalPages is too small, it might cause some errors in display.
            
$array[] = "..";
    }
            
    return 
$array;
          

It's more clean ?
__________________
Wedus project's Website
Send a message via MSN to Gibou
Gibou is offline  
Reply With Quote
The Following User Says Thank You to Gibou For This Useful Post:
Tanax (02-11-2008)
Old 02-11-2008, 08:20 PM   #5 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Thanks! Now I understand :D But what happens if I'm on page 1, and it's only 3 pages?

I mean, you mentioned in the code "could cause errors if $totalPages is too small" ?? Why is that?
__________________
Tanax is offline  
Reply With Quote
Old 02-11-2008, 08:35 PM   #6 (permalink)
The Contributor
 
Gibou's Avatar
 
Join Date: Nov 2007
Location: France, near Paris
Posts: 53
Thanks: 6
Gibou is on a distinguished road
Default

If you have 3 pages, and the current is the first:

the returned array will be: 1|..|3

but we should want: 1|2|3

To change this, we can change for that:

There are 1 or 2 conditions to add but with a variable span, it's a little bit complex and I'm really sorry but I haven't the time to search in details this evening :(
__________________
Wedus project's Website
Send a message via MSN to Gibou
Gibou is offline  
Reply With Quote
Old 02-11-2008, 08:38 PM   #7 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
Anyways, how can I insert an option to have a span in it?
So it only shows the amount of links within the span.
You don't want to modify the class to do this. Essentially, you're changing the HTML; you're not dealing with anything internal to the class.

Quote:
Originally Posted by Tanax View Post
Let's say I set the span to 2, it will only display 2 links on each side of the current page.

First .. 4 5 6 7 8 .. Last
First .. 5 6 7 8 9 .. Last
First .. 32 33 34
An example of this might be:
PHP Code:
// Start pagination display
// Remember: $page = array(current_page_number, total_pages)
if ($page[0] > 1)
{
    
printf('<a href="?p=%d">First</a> .. '1);
}

$pagination_span_width 2;
foreach (
range($page[0] - $pagination_span_width$page[0] + $pagination_span_width) as $pageNumber)
{
    if (
$pageNumber OR $pageNumber $page[1]) continue;
    
    
$tpl = ($pageNumber == $page[0])
         ? 
'<strong>%1$d</strong>'
         
'<a href="?p=%1$d">%1$d</a>';
    
printf($tpl$pageNumber);
}

if (
$page[0] < $page[1])
{
    
printf(' .. <a href="?p=%d">Last</a>'$page[1]);

Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Tanax (02-12-2008)
Old 02-11-2008, 08:41 PM   #8 (permalink)
The Contributor
 
Gibou's Avatar
 
Join Date: Nov 2007
Location: France, near Paris
Posts: 53
Thanks: 6
Gibou is on a distinguished road
Default

Yes, indeed, it's a better way than using the currentSpan attribute.

PS: I didn't know the "continue" in a loop, interesting :)

PS2 for Tanax, i just see it: <strong> mustn't be used to bold a string just because its visual effect is the same than <b> :) <b> is not deprecated in xhtml. And if you did know it, there's no reason to make a <strong> around a page number.
__________________
Wedus project's Website
Send a message via MSN to Gibou
Gibou is offline  
Reply With Quote
Old 02-12-2008, 03:41 PM   #9 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

@Salathe- Thanks! Your method seems simpler.. However, there were some complications..

I want the "first" page only to appear when the page 1 is outside the span.

So if I'm on page 1 of 5, and the span is set to 2, it should display 1 2 3 .. Last.
When going to page 2, it should display 1 2 3 4 Last (<-- note that the dots are skipped here..).

page 3 it should display all links.
Page 4, it should display First 2 3 4 5 (<-- dots are not shown here because below 2, there's 1, and thus only need to display a First link)
And page 5 it should display First .. 3 4 5(<-- dots are shown here, because below 3, there's first the number 2, and that's why the 2 dots are replacing all numbers between the shown 3, and the first link)

How the dot system works is that I want it to only appear if the last or first link is more than 1 number below/above the shown span.

See screenshot for a view how it looks right now..

@Gibou- Thanks! Yes I know the difference.. but since I normally use xhtml(I write <br /> instead of <br> for example), I write <strong> instead of <b>. Same with <em> and <i> ...
Attached Thumbnails
pagination-class-help-page1.jpg  pagination-class-help-page3.jpg  
__________________
Tanax is offline  
Reply With Quote
Old 02-12-2008, 04:09 PM   #10 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

All that you're needing are very simple conditional checks on the value of the current page (and in relation to the number of values in the 'span'). I'm reluctant to keep giving out code snippets without you putting in some thought and work for yourself.

Give it a shot and if things get sticky, we can help to clarify any issues that crop up. From your description in the last post, you've got a clear idea of that it is that you want so just translate the written English description into PHP.
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Tanax (02-12-2008)
Old 02-12-2008, 04:36 PM   #11 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Thanks, I solved it !!
A little bit of thinking, and a little bit of math

Thanks ^_^
__________________
Tanax is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:10 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design