PHP Pagination Class tutorial with next and previous

Today, We want to share with you php pagination class.In this post we will show you Pagination with jQuery Ajax PHP and MySQL, hear for PHP pagination class with Bootstrap 4 we will give you demo and example for implement.In this post, we will learn about pagination code in php with next and previous demo with an example.

PHP pagination class

simple PHP pagination class for displaying main database results in pages example.

index.php

<?php
class pagination
{
	public function __construct()
	{
	}
	public function calculate_pages($total_rows, $rows_per_page, $checknumberofpg)
	{
		$arr = array();

		$endpg = ceil($total_rows / $rows_per_page);

		$checknumberofpg = (int) $checknumberofpg;
		if ($checknumberofpg < 1)
		{
		   $checknumberofpg = 1;
		} 
		elseif ($checknumberofpg > $endpg)
		{
		   $checknumberofpg = $endpg;
		}
		$upto = ($checknumberofpg - 1) * $rows_per_page;
		$arr['limit'] = 'LIMIT '.$upto.',' .$rows_per_page;
		$arr['current'] = $checknumberofpg;
		if ($checknumberofpg == 1)
			$arr['previous'] = $checknumberofpg;
		else
			$arr['previous'] = $checknumberofpg - 1;
		if ($checknumberofpg == $endpg)
			$arr['next'] = $endpg;
		else
			$arr['next'] = $checknumberofpg + 1;
		$arr['last'] = $endpg;
		$arr['info'] = 'Page ('.$checknumberofpg.' of '.$endpg.')';
		$arr['pages'] = $this->get_surrounding_pages($checknumberofpg, $endpg, $arr['next']);
		return $arr;
	}
	function get_surrounding_pages($checknumberofpg, $endpg, $next)
	{
		$arr = array();
		$show = 5; // how many boxes
		// at first
		if ($checknumberofpg == 1)
		{
			// case of 1 page only
			if ($next == $checknumberofpg) return array(1);
			for ($i = 0; $i < $show; $i++)
			{
				if ($i == $endpg) break;
				array_push($arr, $i + 1);
			}
			return $arr;
		}
		// at last
		if ($checknumberofpg == $endpg)
		{
			$beginig = $endpg - $show;
			if ($beginig < 1) $beginig = 0;
			for ($i = $beginig; $i < $endpg; $i++)
			{
				array_push($arr, $i + 1);
			}
			return $arr;
		}
		// at middle
		$beginig = $checknumberofpg - $show;
		if ($beginig < 1) $beginig = 0;
		for ($i = $beginig; $i < $checknumberofpg; $i++)
		{
			array_push($arr, $i + 1);
		}
		for ($i = ($checknumberofpg + 1); $i < ($checknumberofpg + $show); $i++)
		{
			if ($i == ($endpg + 1)) break;
			array_push($arr, $i);
		}
		return $arr;
	}
}
?>

database connection

$paginator = new MyPagina($rows = 50, $connect = false);

Creating the MySQL result set

$paginator = new MyPagina($rows = 50, $connect = true);
$paginator->number_links = 5; 
$paginator->sql = "SELECT * FROM products ORDER BY id"; 
$result = $paginator->get_page_result(); 
 
while ($obj = mysql_fetch_object($result)) { 
    echo = $obj->id.' » '.$obj->title;
}

PHP pagination class examples

$paginator->forw = "forward";
$paginator->back = "backward";
echo $paginator->back_forward_link();

text symbols instead of text

echo $paginator->back_forward_link(true);

navigation with numbers

echo $paginator->navigation(" | ", "currentStyle", false, false, false, true);

display current range of records

echo $paginator->page_info("Result: %d - %d of %d records");

I hope you get an idea about pagination code in php with next and previous.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  Advanced Pagination with PHP, MySQL and jQuery Demo

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Remove Html tags from string PHP - PHP strip_tags() Function
  2. Read Also:  php foreach multidimensional array Example
  3. Read Also:  How to make PHP Pagination With Example and Demo?
  4. Read Also:  Print last query in laravel eloquent - Examples
  5. Read Also:  File Type validation using Javascript
  6. Read Also:  jQuery trigger function viewport Example
  7. Read Also:  PHP Pagination demo – Pagination in PHP with example script
  8. Read Also:  pagination code in php with next and previous demo
  9. Read Also:  pagination in php using ajax
  10. Read Also:  jquery get textarea value
CLOSEX