home

Pagination

Pravljenje tabova uz pomoć php a

Pagination class

php / DOM / HTML5

Pager

Kako jednostavno prikazati result set baze podataka podeljen na manje delove

Glavni cilj je klasa Pagination koja je osnova za kreiranje podeljnog prikaza na jednoj stranici
Pagination klasi prosleđujemo parametre koji se zatim prikazuju i dele na parčiće željene veličine. Na primer ako je rezultat upita baze podataka 120 podatak, a mi želimo da ih podelimo na 10 podataka po stranici, naš pagination će imati 12 tabova. Za kretanje kroz prikaze koristimo dugme Prethodno i Sledeci i najvažnije: kartice stranica


OVAJ KOD JE BESPLATAN JE ZA KORIŠĆENJE, IZMENE, DOPUNE
Miroslav Mitić 06.10.2017.
email : php@vind.in.rs
NAPOMENA: U ovom primeru se koristi Bootstrap.
Ova stranica je ponovo napisana 2023. da koristi W3.CSS.
Možete koristiti koji god css želite :)


Pagination za tabelarni prikaz baze podataka u nekoliko tabela sa tabelama koje imaju određenu veličinu
Glavni cilj je klasa Paginacija koja je osnova za kreiranje prikaza na jednoj stranici
Klasi Paginacija prosleđujemo parametre koji se zatim štampaju metodama a po izboru Prethodno i Sledeće i najvažnije, kartice stranica

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)
{
/* Doesn t depend from user data */
$this->servername = $parametri[0];
$this->username = $parametri[1];
$this->password = $parametri[2];
$this->dbname = $parametri[3];
}
// ---------------------------------------------
function set_polja($_polja)
{
for($i =0; $i $this->polja[$i] = $_polja[$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();
}
catch (Exception $e)
{
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:  892 


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