Jeżeli ilość rekordów jest większa od "numeru" ostatniego wyświetlonego rekordu to należy przejść do strony, gdzie zmienna będąca współczynnikiem wielokrotności wyników wyświetalnych na jednej stronie (czyli w tym przypadku 15) będzie o 1 większa od wartości zmiennej na stronie aktualnej. Przykład:
[php]<?php
if (is_numeric($_GET['no'])) {
$no = $_GET['no']; }
else {
$no = 1; }
$limit = $no * 15;
$limit0 = $limit - 15;
$wynik_ilosc = mysql_query("select id from tabela order by kolumna");
$ilosc = mysql_num_rows($wynik_ilosc);
$wynik = mysql_query("select * from tabela order by kolumna desc limit ".$limit0.",".$limit."");
if ($limit > $ilosc)
$limit = $ilosc;
for ($i=$limit0; $i <$limit; $i++)
{
// wyswietlanie danych
}
$next = $no + 1;
if ($ilosc > $limit)
echo '<a href="?no='.$next.'">Następne ›</a>';
?>[/php]
(nie testowałem, mogą być jakieś błędy)
[PHP] Zliczanie wpisow na stronie
Re: [PHP] Zliczanie wpisow na stronie
Kiedys napisalem sobie prosta klase do stronicowania wynikow z mysqla, moze sie przydac:
[php]<?php
class pager
{
/* przewijanie */
var $_home = '|<';
var $_prev = '<';
var $_next = '>';
var $_end = '>|';
/*classa linkow*/
var $_class = '';
/* wynikow na strone*/
var $_recordonpage = '10';
var $separator = '';
/* inne :] */
var $page_id = '1';
var $row_number;
var $page_number;
/* linki */
var $home_link;
var $prev_link;
var $links;
var $next_link;
var $end_link;
/*konstruktor*/
function pager($row_number, $page_id, $on_page='')
{
if($on_page!=='') $this->setRecordOnPage($on_page);
if(($row_number % $this->_recordonpage)!==0) {
$this->page_number = ($row_number / $this->_recordonpage) - (($row_number % $this->_recordonpage) / $this->_recordonpage) + 1; }
else {
$this->page_number = $row_number / $this->_recordonpage; }
$this->row_number = $row_number;
if($page_id > 0) $this->page_id = $page_id;
}
/*ustalanie wygladu przewijania*/
function setNav($home, $prev, $next, $end)
{
if($home!=='') $this->_home=$home;
if($prev!=='') $this->_prev=$prev;
if($next!=='') $this->_next=$next;
if($end!=='') $this->_end=$end;
}
/*clasa dla linkow*/
function setClass($class)
{
if($class!=='') $this->_class=$class;
}
/*ilosc wynikow na strone*/
function setRecordOnPage($on_page)
{
if($on_page!=='') $this->_recordonpage = $on_page;
}
/*ustalanie id stron*/
function nav()
{
$this->home_id = 1;
$this->next_id = $this->page_id+1;
$this->prev_id = $this->page_id-1;
$this->end_id = $this->page_number;
}
/*tworzenie klas dla linkow*/
function render_class()
{
if($this->_class!=='') {
$this->_class = ' class="'.$this->_class.'"'; }
else{
$this->_class='';}
}
/*wyglad linkow :]*/
function render_links($page_id, $name)
{
$link = '<span'.$this->_class.'><a href="?page_id='.$page_id.'">'.$name.'</a></span>'.$this->separator;
return $link;
}
/*home*/
function home()
{
if($this->page_id > 1) {
$this->home_link = $this->render_links($this->home_id, $this->_home); }
}
/*prev*/
function prev()
{
if($this->prev_id > 0) {
$this->prev_link = $this->render_links($this->prev_id, $this->_prev); }
}
/*generowanie linkow*/
function links()
{
/* gdy id jest rowne 1 */
if($this->page_id == '1') {
$page_id = 1;
while($page_id <= 3) {
if($page_id==$this->page_id) {
$this->links .= '<span'.$this->_class.'><strong>'.$page_id.'</strong></span>'.$this->separator; }
elseif($page_id <= $this->end_id) {
$this->links .= $this->render_links($page_id, $page_id); }
$page_id++;
}
if($page_id < $this->end_id) $this->links .= '...'.$this->separator;
return true;
}
/* gdy id jest rowne ostatniej stronie */
if($this->page_id == $this->end_id) {
$page_id = $this->end_id - 2;
if($page_id > 1) $this->links .= '...'.$this->separator;
$page_id_end = $this->end_id;
while($page_id <= $page_id_end) {
if($page_id==$this->page_id) {
$this->links .= '<span'.$this->_class.'><strong>'.$page_id.'</strong></span>'.$this->separator; }
elseif($page_id > 0){
$this->links .= $this->render_links($page_id, $page_id); }
$page_id++;
}
return true;
}
/*w innym wypadku :] */
else {
if($this->page_id >= 4) $this->links .= '...'.$this->separator;
$page_id = $this->page_id - 2;
$page_id_end = $this->page_id + 2;
while($page_id <= $page_id_end) {
if($page_id==$this->page_id) {
$this->links .= '<span'.$this->_class.'><strong>'.$page_id.'</strong></span>'.$this->separator; }
elseif($page_id > 0 && $page_id <= $this->page_number) {
$this->links .= $this->render_links($page_id, $page_id); }
$page_id++;
}
if($this->page_id <= ($this->page_number - 3)) $this->links .= '...'.$this->separator;
return true;
}
}
/*next*/
function next()
{
if($this->next_id <= $this->page_number) {
$this->next_link = $this->render_links($this->next_id, $this->_next); }
}
/*end*/
function end()
{
if($this->page_id < $this->end_id) {
$this->end_link = $this->render_links($this->end_id, $this->_end); }
}
/*tworzenie pagera*/
function render()
{
echo '<div class="pager">';
$this->render_class();
$this->nav();
$this->home();
echo $this->home_link;
$this->prev();
echo $this->prev_link;
$this->links();
echo $this->links;
$this->next();
echo $this->next_link;
$this->end();
echo $this->end_link;
echo '</div>';
}
/*limit*/
function limit()
{
$limit_min = ($this->_recordonpage * ($this->page_id - 1));
return $limit_min;
}
/*wyswietlanie ilosci wynikow na strone*/
function onPage()
{
return $this->_recordonpage;
}
}
/* Przyklad uzycia */
$row_number = $db -> row_number('katalog'); //pobranie ilosci wierszy w tabeli, tutaj zmienic
$pager=new pager($row_number, $_GET['page_id']); //stworzenie pagera
$pager -> setClass('nawigacja'); //nadanie nazwy klasy dla pagera
$query='SELECT * FROM katalog LIMIT '.$pager -> limit().', '.$pager -> onPage(); //zapytanie dal ktorego przeznaczony jest pager
$result = $db -> query($query); //tutaj zmienic
while($row = mysql_fetch_array($result))
{
echo $row['title']; //tutaj daj co chcesz, czyli wyswielanie wynikow z mysqla
echo '<br />';
}
$pager -> render(); // wyswietlenie nawygacji pagera
?>[/php]
Przyklad uzycia jest oparty na klasie do obslugi bazy danych wiec trzeba pozmieniac na "swoje"
Wyswietla ladna nawigacje, mozna ustalic ilosc wynikow na strone, klase dla linkow w nawigacji, sam jej wyglad.
[php]<?php
class pager
{
/* przewijanie */
var $_home = '|<';
var $_prev = '<';
var $_next = '>';
var $_end = '>|';
/*classa linkow*/
var $_class = '';
/* wynikow na strone*/
var $_recordonpage = '10';
var $separator = '';
/* inne :] */
var $page_id = '1';
var $row_number;
var $page_number;
/* linki */
var $home_link;
var $prev_link;
var $links;
var $next_link;
var $end_link;
/*konstruktor*/
function pager($row_number, $page_id, $on_page='')
{
if($on_page!=='') $this->setRecordOnPage($on_page);
if(($row_number % $this->_recordonpage)!==0) {
$this->page_number = ($row_number / $this->_recordonpage) - (($row_number % $this->_recordonpage) / $this->_recordonpage) + 1; }
else {
$this->page_number = $row_number / $this->_recordonpage; }
$this->row_number = $row_number;
if($page_id > 0) $this->page_id = $page_id;
}
/*ustalanie wygladu przewijania*/
function setNav($home, $prev, $next, $end)
{
if($home!=='') $this->_home=$home;
if($prev!=='') $this->_prev=$prev;
if($next!=='') $this->_next=$next;
if($end!=='') $this->_end=$end;
}
/*clasa dla linkow*/
function setClass($class)
{
if($class!=='') $this->_class=$class;
}
/*ilosc wynikow na strone*/
function setRecordOnPage($on_page)
{
if($on_page!=='') $this->_recordonpage = $on_page;
}
/*ustalanie id stron*/
function nav()
{
$this->home_id = 1;
$this->next_id = $this->page_id+1;
$this->prev_id = $this->page_id-1;
$this->end_id = $this->page_number;
}
/*tworzenie klas dla linkow*/
function render_class()
{
if($this->_class!=='') {
$this->_class = ' class="'.$this->_class.'"'; }
else{
$this->_class='';}
}
/*wyglad linkow :]*/
function render_links($page_id, $name)
{
$link = '<span'.$this->_class.'><a href="?page_id='.$page_id.'">'.$name.'</a></span>'.$this->separator;
return $link;
}
/*home*/
function home()
{
if($this->page_id > 1) {
$this->home_link = $this->render_links($this->home_id, $this->_home); }
}
/*prev*/
function prev()
{
if($this->prev_id > 0) {
$this->prev_link = $this->render_links($this->prev_id, $this->_prev); }
}
/*generowanie linkow*/
function links()
{
/* gdy id jest rowne 1 */
if($this->page_id == '1') {
$page_id = 1;
while($page_id <= 3) {
if($page_id==$this->page_id) {
$this->links .= '<span'.$this->_class.'><strong>'.$page_id.'</strong></span>'.$this->separator; }
elseif($page_id <= $this->end_id) {
$this->links .= $this->render_links($page_id, $page_id); }
$page_id++;
}
if($page_id < $this->end_id) $this->links .= '...'.$this->separator;
return true;
}
/* gdy id jest rowne ostatniej stronie */
if($this->page_id == $this->end_id) {
$page_id = $this->end_id - 2;
if($page_id > 1) $this->links .= '...'.$this->separator;
$page_id_end = $this->end_id;
while($page_id <= $page_id_end) {
if($page_id==$this->page_id) {
$this->links .= '<span'.$this->_class.'><strong>'.$page_id.'</strong></span>'.$this->separator; }
elseif($page_id > 0){
$this->links .= $this->render_links($page_id, $page_id); }
$page_id++;
}
return true;
}
/*w innym wypadku :] */
else {
if($this->page_id >= 4) $this->links .= '...'.$this->separator;
$page_id = $this->page_id - 2;
$page_id_end = $this->page_id + 2;
while($page_id <= $page_id_end) {
if($page_id==$this->page_id) {
$this->links .= '<span'.$this->_class.'><strong>'.$page_id.'</strong></span>'.$this->separator; }
elseif($page_id > 0 && $page_id <= $this->page_number) {
$this->links .= $this->render_links($page_id, $page_id); }
$page_id++;
}
if($this->page_id <= ($this->page_number - 3)) $this->links .= '...'.$this->separator;
return true;
}
}
/*next*/
function next()
{
if($this->next_id <= $this->page_number) {
$this->next_link = $this->render_links($this->next_id, $this->_next); }
}
/*end*/
function end()
{
if($this->page_id < $this->end_id) {
$this->end_link = $this->render_links($this->end_id, $this->_end); }
}
/*tworzenie pagera*/
function render()
{
echo '<div class="pager">';
$this->render_class();
$this->nav();
$this->home();
echo $this->home_link;
$this->prev();
echo $this->prev_link;
$this->links();
echo $this->links;
$this->next();
echo $this->next_link;
$this->end();
echo $this->end_link;
echo '</div>';
}
/*limit*/
function limit()
{
$limit_min = ($this->_recordonpage * ($this->page_id - 1));
return $limit_min;
}
/*wyswietlanie ilosci wynikow na strone*/
function onPage()
{
return $this->_recordonpage;
}
}
/* Przyklad uzycia */
$row_number = $db -> row_number('katalog'); //pobranie ilosci wierszy w tabeli, tutaj zmienic
$pager=new pager($row_number, $_GET['page_id']); //stworzenie pagera
$pager -> setClass('nawigacja'); //nadanie nazwy klasy dla pagera
$query='SELECT * FROM katalog LIMIT '.$pager -> limit().', '.$pager -> onPage(); //zapytanie dal ktorego przeznaczony jest pager
$result = $db -> query($query); //tutaj zmienic
while($row = mysql_fetch_array($result))
{
echo $row['title']; //tutaj daj co chcesz, czyli wyswielanie wynikow z mysqla
echo '<br />';
}
$pager -> render(); // wyswietlenie nawygacji pagera
?>[/php]
Przyklad uzycia jest oparty na klasie do obslugi bazy danych wiec trzeba pozmieniac na "swoje"
Re: [PHP] Zliczanie wpisow na stronie
ja mam nieco krocej:Balas pisze:Kiedys napisalem sobie prosta klase do stronicowania wynikow z mysqla, moze sie przydac:
[php]<?php
function points($page,$ilee,$limit,$link,$styl,$max=10,$aft=5){
/*
$page - aktualna strona
$ilee - calkowita ilosc rekordow
$limit - ile na jedna strone
$max - maksymalna liczba cyferek
$aft - po nacisnieciu ktorej liczby ma nastapic przesuwanie punktow, cyferek
$link - adres punktow
$styl punktow
*/
if ($limit==0){$ile=0;}else{
$ile=ceil($ilee/$limit);}
//echo $ile;
if ($ile==1 || $ile==0){
return "";
}else{
if (empty($page)){$page=1;}
if ($page>$ile){$page=1;}
$a="<div class='".$styl."'>\n";
if ($max>=$ile){
$m=$ile;
}else{
$m=$max+$page-$aft+1;
if ($m>$ile){$m=$ile;}
if ($m<$max){$m=$max;}
}
if ($page!=1){
$bef=$page-1;
$a.=sprintf($link,$bef,"poprzednia strona","<");
}
if ($page>=$aft && $ile>$max){
$z=$page-$aft+2;
if ($z>$ile-$max+1){$z=$ile-$max+1;}
} else{
$z=1;
}
if ($page>=$aft && $ile>$max){$a.="... ";}
while ($z<=$m){
if ($page==$z){
$a.="<span style='background-color:black;color:white'>".$z++."</span>\n ";
}else{
$a.=sprintf($link,$z,$z.". strona",$z++);
}
}
if ($m!=$ile){$a.="...";}
if ($page!=$m){
$next=$page+1;
$a.=sprintf($link,$next,"następna strona",">");
}
$a.="</div>";
return $a;
}
}
?>[/php]
przyklad:
[php]<?php
/*
przyklad zastosowania funkcji
nr strony pobierany jest przez $strona w adresie (GET)
calkowita liczba stron rowna 100
na jednej stronie wyswietla sie 10 pozycji
nastepny jest to szablon linku danego numeratora hdzie pierwsze % to nr podstrony drugie to opis linka a trzecie tesk linka
"points" to clasa css opisujaca wyglad diva
*/
echo points($_GET['strona'],100,10,"<a href='index.php?page=%d' title='%s'>%s</a>\n ","points");
?>[/php]
Re: [PHP] Zliczanie wpisow na stronie
no jest zmienną przekazywaną metodą GET czyli w adresie url.
Co do opcji poprzedniej strony należałoby wprowadzić następującą modyfikację:
[php]<?php
if ($ilosc > $limit0 && $limit0 != 0)
echo '<a href="?no='.$prev.'">‹ Poprzednie</a>';
if ($ilosc > $limit)
echo '<a href="?no='.$next.'">Następne ›</a>';
?>[/php]
Co do opcji poprzedniej strony należałoby wprowadzić następującą modyfikację:
[php]<?php
if ($ilosc > $limit0 && $limit0 != 0)
echo '<a href="?no='.$prev.'">‹ Poprzednie</a>';
if ($ilosc > $limit)
echo '<a href="?no='.$next.'">Następne ›</a>';
?>[/php]
