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 i Next and most important, tabs of pages
THIS 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: I preferer Bootstrap and it is implement on this example. You can edit as you like
class Pagination
{
// ----- connection parameters --------------
private $servername;
private $username ;
private $password ;
private $dbname ;
// --- --- main variables -----------
private $upit; // main query an his recordset is in use in script
private $ukupno_slogova; // recordset count
private $br_strana; // page numbers
private $limit = 30; // limits redords per page (is default)
public $izabrani =1; // default , first page
public $page = 1; // current page
private $polja = array(); // redorst for display
function __construct ( $_parametri, $_p, $_upit,$_limit)
{
$this->set_DB_params($_parametri);
$this->set_polja($_p);
$this->set_Limit ($_limit);
$this->set_Upit($_upit);
$this-> set_Ukupno_Slogova();
$this-> set_br_strana();
}
//-----------------------------
// SET s functions
//-----------------------------
function set_DB_params($parametri)
{
/* Ovo ne zavisi od korisnickog unosa */
$this->servername = $parametri[0];
$this->username = $parametri[1];
$this->password = $parametri[2];
$this->dbname = $parametri[3];
}
// ---------------------------------------------
function set_polja($_polja)
{
for($i =0; $i
}
// ---------------------------------------------
function set_Limit ($data)
{
$this->limit = $data;
}
// ---------------------------------------------
function set_Upit($data)
{
$this->upit = $data;
}
// ---------------------------------------------
function set_Ukupno_Slogova()
{
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 "Konekcija uspela
";
$ukupno = $conn->query($this->upit);
$this->ukupno_slogova = $ukupno->rowCount();
}
{
echo " EROOR cant find number of rows: " . $e->getMessage();
}
}
function set_br_strana()
{
$this->br_strana = (int) ( $this->ukupno_slogova / $this->limit ) ;
$br_na_zadnjoj_strani = $this->ukupno_slogova - $this->br_strana* $this->limit;
if($br_na_zadnjoj_strani>0) {$this->br_strana++;}
}
// --------------
// GET`s
// ---------------
private function getControlData()
{
/* Ova funkcija je samo za testiranje klase u toku programiranja */
echo "< h3 >This controls is - test only < /h3 > ";
echo " Query " . $this->upit . "<BR>";
echo " DB name " . $this->dbname . "<BR>";
echo " Limit " . $this->limit . "<BR>";
echo " Records sum " . $this->ukupno_slogova . "<BR>";
echo " Page numbers " . $this->br_strana . "<BR>";
echo "Fields for display";
print_r($this->polja);
}
// -------------------
// print functoins
// -------------------
public function print_pages()
{
print '<ul class="pagination">';
for ($i =0 , $counter = 1; $i< $this->br_strana; $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 . " >prethodni</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 . " > sledeci</ >";
if ($this->page > $this->br_strana) {$this->page = $this->br_strana;}
}
// ---------------------
function stampa_tabelu()
{
$this-> izabrani = $_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->izabrani -1 ) * $this->limit;
$upit1 = $this->upit . "LIMIT " . $of . "," . $this->limit ;
print'<table class="table table-striped">';
// I prefer bootstrap, you cant change if you like
//$polja = array ('ID','MESTO' , 'TIP' , 'NAZIV_DELA', 'DATUM', 'NAPOMENE'); // polja tabele baze podataka
echo "< tr >";
for($i =0;$i< count($this->polja) ; $i++)
{
echo '<th >' .$this->polja[$i]. '</ th >';
}
echo "< /tr >";
foreach( $conn->query($upit1) as $row)
{
echo "< tr >";
for($i =0; $i< count($this->polja) ; $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');
$_polja_za_prikaz = array('ID','MESTO' , 'TIP' , 'NAZIV_DELA', 'DATUM', 'NAPOMENE');
// records that we wont display
$upit = "SELECT * FROM interv ";
$prikaza_po_stranici = 30;
$Stranicenje = new Pagination ( $_parametri, $_polja_za_prikaz, $upit, $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->stampa_tabelu();
?>
</div >
< /div >
pvc: 869