Jump to content

[SOLVED] shift numbers with paging


jmcc

Recommended Posts

Hi

 

How would i be able to display 10 numbers 1 2 3 4 5 6 7 8 9 10

and when I click next  then 2 3 4 5 6 7 8 9 10 11 and click next then 3 4 5 6 7 8 9 10 11 12

 

are displayed.

 

i am using a for loop to echo te index 1. up to 10 but have no idea how i would od the shifting thing.

 

here what I have so far:

 

<?php>

    for ( $i =0; $i < 10 ; $i++){ ?>

        <a href="<?php> printf("%s?pageNum_seller=%d%s", $currentPage, max(0, $pageNum_seller = $i), $queryString_seller); ?>"> <?php echo $i +1?></a>

        <?php> } ?>

 

my next button increments $pageNum_seller

Thanks

Link to comment
Share on other sites

This is a complex problem and there's a few solutions depending on what you want to accomplish.

 

What you should immediately realize is that what pages get displayed needs to depend on what page you're on, and how many you have. But it's not that straight forward.

 

Users need to go back as much as they need to go forward. So displaying 2-11 when you're on page 2 isn't useful. I can't get back to page 1!

 

Instead, you need to define the range of pages that will be shown. So, for example, you might show the next 9 pages and the previous 9 pages.

 

Then you have to validate that those pages exist. Then you have to decide what you want to do with "left over" space.

 

Case in point, should page 1 only show the next 9 pages where as page 10 will show pages 1-20? Or should page 1 recognize that the 9 previous pages aren't being used, and switch them to next pages instead. So that page 1 also shows pages 1-20 (or at least 1-19) instead of just showing pages 1-10.

 

If you don't recycle left over room, by the time a user gets to page 10 they can see twice as many pages as they could from page 1 (including page 1). Creates a bit of a weird feel when clicking on page 2 suddenly shows page 11, clicking page 3 suddenly shows page 12 etc. Makes the user wonder why pages 11 and 12 weren't just shown in the first place.

Link to comment
Share on other sites

Just an adjust to the page links:

 

This assumes you have 30 items per page

 

$page = 1;
$tpage = ceil( $total_pics / 30 );
$startfrom = ( $page - 1 ) * 30;
$start_num = $startfrom + 1;
    $end_num = $startfrom + $total_pics;
    $prev = $page - 1;
    $next = $page + 1;
    $adjust = 9;
    
    if ( 1 < $page )
    {
        $page_links_pics .= "<a class=pagination_prev href='".$prev."'><< Prev</a><a class=pagination_next href='1'>1</a>....";
    }
    $k = $page - $adjust;
    for ( $page ; $k <= $page + $adjust; ++$k )
    {
        if (( 0 < $k ) && ( $k <= $tpage ) )
        {
            
            if(($page)==($k)){
			$page_links_pics.="<span class=\"pagination_active\"> ".$k." </span>";
			} else {
               $page_links_pics .= "<a class=pagination href='".$k."'>{$k}</a>";
                    		}
     	}
    }
    if ( $page < $tpage )
    {
    
        $page_links_pics .= ".....<a class=pagination_next href='".$tpage."'>".$tpage."</a><a class=pagination_next href='".$next."'>Next >></a>";
        
    }

 

Link to comment
Share on other sites

what about if I take the page number add  five to it and loop till i hit that var then also do it by -5.

 

Concept  below:

 

$pageNumBig+=$pageNum+5;

$pageNumSmall-=$pageNum-5;

 

for (i=$pageNum ; i=$pageNumBig, i++){

 

echo i;

}

for (i=pageNum ; i=$pageNumSmall, i--){

 

if (Pagenum > 0) echo $i;

}

 

Donno just a shot in the dark

 

Link to comment
Share on other sites

That'd work, although the += and -= shouldn't be necessary.

 

Also, this is the type of thing that may look a little weird. IE. if I start on page 1 I can see page 5, but once I get to page 5, I can not only see page 1, but I can see page 9 as well. Kinda makes you wonder why you couldn't see page 9 from page 1.

 

But, that's just an issue of convenience.

Link to comment
Share on other sites

Here's a function I came up with real quick:

<?php

$totalPages = 30;	// total pages
$currPage = 17;		// current page, from GET or whatever
$maxDisplay = 14;	// how many to show at once

function Paginate($totalPages, $currPage, $maxDisplay = 10) {

// Check to see if the current page var is OK
if($currPage>$totalPages) {
	$currPage = $totalPages;
}
if($currPage<=0) {
	$currPage = 1;
}
// Just to have an even number on either side...
if($maxDisplay%2!=0) {
	$maxDisplay--;
}
// Are there more pages than we would display?
if($totalPages < $maxDisplay) {
	return range(1,$totalPages);
}
// Would the first element be a negative number or page #0?
if($currPage-($maxDisplay/2) <= 0) {
	$range = range(1,$currPage);
	$range = array_merge($range, range($currPage+1, $currPage+$maxDisplay-count($range)+1));
	return $range;
}
// Would the last element be past the total number of pages?
if($currPage+($maxDisplay/2) > $totalPages) {
	$range = range($totalPages, $currPage);
	$range = array_merge($range, range($currPage-1, $currPage-$maxDisplay+count($range)-1));
	return array_reverse($range);
}
// Otherwise, just add half the max display on either side
return range($currPage-($maxDisplay/2), $currPage+($maxDisplay/2));
}

/*
// Testing:
print_r(Paginate(30, -15, 10));
print_r(Paginate(30, 18, 10));
print_r(Paginate(30, 27, 10));
*/

// Run through the results:
foreach(Paginate($totalPages, $currPage, $maxDisplay) as $pageNumber) {
if($pageNumber == $currPage) {
	// just show what page we're on:
	echo '<u>'.$currPage.'</u>';
} else {
	// normally show a link or whatever... but for simplicity we'll just show the number
	echo $pageNumber;
}

// add a space between the numbers
echo ' ';
}

?>

 

By placing it in a function it allows you to reuse the code with different styles, etc. The function above returns an array of the page numbers, and you choose what to do with them. Is there a shorter way? Probably.

 

Oh and a quick look at CV's tutorial, I think it covers it in a different way: http://www.phpfreaks.com/tutorial/basic-pagination

Link to comment
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.