home

Pagination

Making class for Pagination using php

Pagination class

I am often use class for DOM and HTML5

Pager

for a table view of the database in several tables with tables that have a certain size

Main goal is in class Pagination which is a one-page creation board
To class Pagination we pass parameters that are then printed by methods a by choice Prev and Next and most important, tabs of pages



THIS CLASS IS A COPYRIGHT WORK AND IS FREE FOR USE, MODIFICATION, ADDITION TO ALL PHP FAVORITES IS CLASS IS A COPYRIGHT WORK AND IS FREE FOR USE, MODIFICATION, ADDITION TO ALL PHP FAVORITES
by Miroslav Mitic 06.10.2017.
email : php@wind.in.rs
NOTE: In this example is used Bootstrap.
This page is rewriten 2023 to use W3.CSS.
You can use whatever css you like :)

This table is from some of my project. It use database with jobs done table (when is some jobs done and which spare parts is spent).

Pager for a table view of the database in several tables with tables that have a certain size
Main goal is in class Pagination which is a one-page creation board
To class Pagination we pass parameters that are then printed by methods a by choice Prev i Next and most important, tabs of pages

class Pagination
{
// ----- connection parameters --------------
private $servername;
private $username ;
private $password ;
private $dbname ;
// --- --- main variables -----------
private $custom_query; // main query an his recordset is in use in script
private $records; // recordset count
private $pages; // page numbers
private $limit = 30; // limits redords per page (is default)
public $selected =1; // default , first page
public $page = 1; // current page
private $fields = array(); // redorst for display
function __construct ( $_parametri, $_p, $_custom_query,$_limit)
{
$this->set_DB_params($_parametri);
$this->set_fields($_p);
$this->set_Limit ($_limit);
$this->set_custom_query($_custom_query);
$this-> set_records();
$this-> set_pages();
}
//-----------------------------
// SET s functions
//-----------------------------

function set_DB_params($parametri)
{
/* Doesn t depend from user data */
$this->servername = $parametri[0];
$this->username = $parametri[1];
$this->password = $parametri[2];
$this->dbname = $parametri[3];
}
// ---------------------------------------------
function set_fields($_fields)
{
for($i =0; $i $this->fields[$i] = $_fields[$i];
}
// ---------------------------------------------
function set_Limit ($data)
{
$this->limit = $data;
}
// ---------------------------------------------
function set_custom_query($data)
{
$this->custom_query = $data;
}
// ---------------------------------------------
function set_records()
{
try {
$conn = new PDO("mysql:host=$servername;dbname=$this->dbname", $this->username, $this->password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// echo "Connection success
";
$total = $conn->query($this->custom_query);
$this->records = $total->rowCount();
}
catch (Exception $e)
{
echo " EROOR cant find number of rows: " . $e->getMessage();
}
}
function set_pages()
{
$this->pages = (int) ( $this->records / $this->limit ) ;
$mark_on_last_page = $this->records - $this->pages* $this->limit;
if($mark_on_last_page>0) {$this->pages++;}
}
// --------------
// GET`s
// ---------------

private function getControlData()
{
/* Test only function */

echo "< h3 >This controls is - test only < /h3 > ";
echo " Query " . $this->custom_query . "<BR>";
echo " DB name " . $this->dbname . "<BR>";
echo " Limit " . $this->limit . "<BR>";
echo " Records sum " . $this->records . "<BR>";
echo " Page numbers " . $this->pages . "<BR>";
echo "Fields for display";
print_r($this->fields);
}
// -------------------
// print functoins
// -------------------

public function print_pages()
{
print '<ul class="pagination">';
for ($i =0 , $counter = 1; $i< $this->pages; $i++)
{
printf ( '%s%s%s%s%s', '<li> <a href="?page=' ,$counter, '" >' , $counter , '</a></li>');
$counter++;
}
print '</ul>';
}

// ----------------------
public function print_prew()
{
if(isset($_GET['page']) )
{ $this->page = $_GET['page']-1; }
else $this->page =1;
if($this->page <1) {$this->page = 1;}
echo "<a href= ?page=". $this->page . " >Previous</a>";
}

// ------------------------
public function print_next()
{
if(isset($_GET['page']) )
{ $this->page = $_GET['page']+1; }
else $this->page =1;
if($this->page <1) {$this->page = 1;}
echo "<a href= ?page=". $this->page . " > Next</ >";
if ($this->page > $this->pages) {$this->page = $this->pages;}
}

// ---------------------
function print_table()
{
$this-> selected = $_GET['page'];
try {
$conn = new PDO("mysql:host=$servername;dbname=$this->dbname", $this->username, $this->password);

// set the PDO error mode to exception uvek ovako
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$of = ($this->selected -1 ) * $this->limit;
$custom_query1 = $this->custom_query . "LIMIT " . $of . "," . $this->limit ;
print'<table class="table table-striped">';
// I prefer bootstrap, you cant change if you like
//$fields = array ('ID','PLACE' , 'TYPE' , 'PART_NAME', 'DATE', 'NOTE'); // fields Data base - table fields
echo "< tr >";
for($i =0;$i< count($this->fields) ; $i++)
{
echo '<th >' .$this->fields[$i]. '</ th >';
}
echo "< /tr >";
foreach( $conn->query($custom_query1) as $row)
{
echo "< tr >";
for($i =0; $i< count($this->fields) ; $i++)
{
echo '<td >' .$row[$i]. '</td >';
}
echo "< tr >";
}
print '</table>';
}
catch (Exception $e)
{
echo " table print error - Connection failed: " . $e->getMessage();
}
}
// -------------------
}
//class end
$_parametri = array('localhost','boss','password','databasename');
$_fields_za_prikaz = array('ID','PLACE' , 'TYPE' , 'PART_NAME', 'DATE', 'NOTE');
// records that we wont display
$custom_query = "SELECT * FROM interv ";
$prikaza_po_stranici = 30;
$Stranicenje = new Pagination ( $_parametri, $_fields_za_prikaz, $custom_query, $prikaza_po_stranici);
// $Stranicenje ->getControlData(); // samo za tesitranje
?>



<div class="container">
<h2>Pager</h2>
<p> Pagination klasa - TEST:


<ul class="pager">
<< li class="previous">
<?php
$Stranicenje-> print_prew();
?>
</li>
<?php
$Stranicenje-> print_pages();
?>
<li class="next">
<?php
$Stranicenje-> print_next();
?>
</li>
<br><?php
$Stranicenje->print_table();
?>
</div >
< /div >
 pvc:  2374 


Put some comments (if you want)


Most relevant comments

×

Privacy Policy

Comments

This site does not record user comments. and dont use the thread access IP address. The website does not collect or record the personal data of visitors

Cookies

This web site does not use cookies.

The connection to othes sites

Articles on this site contain links to other sites, as well as links to web applications author of the site. The author of the site is not responsible for the content and changes to these sites

Analitika

This site use Google Analytics
Disclamers
The author of the site is not responsible for any damage caused by use site. The visitor of this website accepts the terms of use as they are.

×

Terms of use

This web loacrion wind.in.rs is private intelectual property by Miroslav Mitić mechanikal engineer from Belgrade / Serbia : the site author).
By using this website, you agree to the following terms:

  • The website editor makes every effort to keep the published data accurate and up-to-date, but cannot take responsibility for any errors made during the entry.
  • The author does not guarantee the accuracy or completeness of the data, so users of the site use the content of the website at their own risk. The author may change this page at any time without notice.
  • The copyright on this website and in connection with the content of this website is protected under the terms of the Creative Commons Attribution 3.0 Serbia license.
  • All texts and illustrations on the website are informative and do not have to be the subject of any obligation of the website owner towards the website visitors. The materials published on this site are provided "as is" without warranty of any kind.
You can make links to this site from other sites, but also those:
  • They must not falsely represent a relationship with the author,
  • Do not use the author's logo and logo and other visual standards of the author without permission
  • They do not contain anything that could be interpreted as distasteful, offensive or controversial.
  • This website contains links to a large number of other websites, but the author is in no way responsible for the content and accuracy of the information on these websites.
 By accessing this page, you confirm that you agree to the terms of use