Jump to content

Recommended Posts

Hey guys, I am trying to create an ajax pagination class. Right now I have it displaying all the pages. I want to make it so I can display something like

 

1...8 9 10 11 12 13...28 (when you're in the middle)

1 2 3 4 5 6...28 (when you're in the beginning)

1... 23 24 25 26 27 28 (when you're at the end)

 

Just like its setup on these forums. The number of numbers in the middle, in this case 6, would be defined as well. Can someone point me in the right direction? Here is how I instantiate the class:

 

paginate.php - get's called via ajax

<?php
if (is_numeric($_REQUEST['page']))
{
require_once("paginator.class.php");
$p = new Paginator();
$p->currentPage		= $_REQUEST['page'];
$p->itemsTotal 		= 168; 	// mysql_num_rows value eventually
$p->itemsPerPage 	= 10; 	// Static number
$p->paginate();
?>
<div class="pagination"><?php echo $p->displayPages(); ?></div>
<?php
}
?>

 

paginator.class.php

<?php
class Paginator
{
var $itemsPerPage;
var $itemsTotal;
var $numPages;
var $currentPage;
var $output;

function Paginator()
{
}

function paginate()
{
	$this->numPages = ceil($this->itemsTotal /  $this->itemsPerPage);

	for ($i = 1; $i <= $this->numPages; $i++)
	{
		if ($i == $this->currentPage)
		{
			$this->output .= "<span class=\"paginate current\" onclick=\"goPage(this)\" data-page=\"" . $i . "\">" . $i . "</span> ";
		}
		else
		{
			$this->output .= "<span class=\"paginate\" onclick=\"goPage(this)\" data-page=\"" . $i . "\">" . $i . "</span> ";
		}
	}
}

function displayPages()
{
	return $this->output;
}
}
?>

 

Any help would be appreciated, thanks!

Link to comment
https://forums.phpfreaks.com/topic/229624-help-with-pagination-class/
Share on other sites

try

<?php
class Paginator{
//...
function my_p_number($total_pages, $current, $before = 2, $after = 3){
    $out = array(1);
    $out = array_merge($out, range(min($total_pages-$before-$after,max(1, $current - $before)), max($before+$after+1,min($total_pages, $current + $after))));
    $out[] = $total_pages;
    $out = array_unique($out);
    sort($out);
    return $out;
}
function paginate(){
    $this->numPages = ceil($this->itemsTotal /  $this->itemsPerPage);
    $p = Paginator::my_p_number($this->numPages, $this->currentPage);
    $t = 0;
    foreach ($p as $i){
        if($i - $t > 1) $this->output .= ' ... ';
        if ($i == $this->currentPage){
            $this->output .= "<span class=\"paginate current\" onclick=\"goPage(this)\" data-page=\"" . $i . "\">" . $i . "</span> ";
        }else{
            $this->output .= "<span class=\"paginate\" onclick=\"goPage(this)\" data-page=\"" . $i . "\">" . $i . "</span> ";
       }
       $t=$i;
   }
}
}
?> 

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.