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).
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