Klasa nie jest taka świetna jak tak klasa nospora ale kiedys moze mu dorownam (IMG:
http://forum.php.pl/style_emoticons/default/winksmiley.jpg) zauwazylem ze ludzie pisza o problemie z mod rewrite i roznymi linkami, tak wiec pomyslalem ze do skryptu ktory aktualnie pisze dodam klase z pagerem o walsnie takiej mozliwosci (IMG:
http://forum.php.pl/style_emoticons/default/winksmiley.jpg)
<?php
/**
* Klasa pagera
*
* @author Krzysztof 'Balon' Jagiełło < balonyo@gmail.com >
*
* ( pager::text['separ'] - separator odzdzielajacy linki do stron )
* ( pager::url - sposob wyswietlania urli )
*
*/
class pager
{
var $ileMax = 10; # ileMax na strone
var $actPage; # aktualna strona
var $ilePages; # ile stron (wynik pager::ileStron)
var $pagerMax = 3;
var $text = array( 'PREV' => '<< Poprzednia ::', 'NEXT' => ':: Następna >>', 'T_PREV' => 'Poprzednia strona', 'T_NEXT' => 'Następna strona', 'SEPAR' => ' ', 'ZILU' => 'z % stron' ); var $url = '<a href="linki.php?p=%">%</a>';
var $link;
/**
* Konstruktor klasy
*
* @param integer $actPage aktualny numer strony
*/
function pager( $actPage, $num, $limit = 10 )
{
$this->actPage = $actPage;
$this->ilePages = $this->ileStron( $num );
$this->ileMax = $limit;
}
/**
* Pager
*
*/
function showPager()
{
$this->prev();
$this->addSep();
for( $i = ( $this->actPage / $this->ileMax ) - $this->pagerMax; $i <= ( $this->actPage / $this->ileMax ) + $this->pagerMax; $i++ )
{
if( $i >= 0 && $i < $this->ilePages )
{
$this->page( $i );
$this->addSep();
}
}
$this->addzIlu( $this->ilePages );
$this->next();
return $this->link;
}
/**
* Poprzednia strona
*
*/
{
if( ( $this->actPage / $this->ileMax ) == 0 )
{
$this->link = $this->text['PREV'];
}
else
{
$this->link = $this->url( $this->actPage / $this->ileMax, '', 'minus' );
}
}
/**
* Nastepna strona
*
*/
{
if( $this->actPage < ( ( $this->ilePages * $this->ileMax ) - $this->ileMax ) )
{
$this->link .= $this->url( $this->actPage / $this->ileMax, '', 'plus' );
}
else
{
$this->link .= $this->text['NEXT'];
}
}
/**
* wyswietlanie linku do strony
*
* @param integer $i numer strony
*/
function page( $i )
{
if( $this->actPage == $i * $this->ileMax )
{
$this->link .= $this->url( $i , 't' );
}
else
{
$this->link .= $this->url( $i );
}
}
/**
* Obliczanie ilosci stron
*
* @param integer $num liczba wpisow(mysql_num_rows)
* @return $ilePages
*/
function ileStron( $num )
{
return ceil( $num / $this->ileMax ); }
/**
* Tworzenie urli, do $this->link podajemy juz <a href="(...)
* a miejsce na numer strony oznaczamy jako %
*
* @param integer $i
* @param string $bold
* @return $link
*/
function url( $i, $bold = null, $mod = null )
{
if( $mod == null )
{
if( $bold == 't' )
{
$link = str_replace( '>%<', '><b>'.( $i + 1 ).'</b><', $this->url ); }
else
{
$link = str_replace( '>%<', '>'.( $i + 1 ).'<', $this->url ); }
}
else
{
if( $mod == 'plus' )
{
$link = str_replace( '>%<', ' title="'.$this->text['T_NEXT'].'">'.$this->text['NEXT'].'<', $this->url ); $link = str_replace( '%', ( $i * $this->ileMax ) + $this->ileMax, $link ); }
else if( $mod == 'minus' )
{
$link = str_replace( '>%<', ' title="'.$this->text['T_PREV'].'">'.$this->text['PREV'].'<', $this->url ); $link = str_replace( '%', ( $i * $this->ileMax ) - $this->ileMax, $link ); }
}
return $link;
}
/**
* Dodawanie separatora
*
*/
function addSep()
{
$this->link .= $this->text['SEPAR'];
}
/**
* " (...) ze 100 "
*/
function addzIlu( $i )
{
$this->link .= str_replace( '%', $i, $this->text['ZILU'] ); $this->addSep();
}
}
?>
Przyklad uzycia
<?php
{
$p = $_GET['p'];
}
else
{
$p = 0;
}
$sql->sql_query( 'SELECT id FROM LINKS WHERE zatw = "tak"' );
$num = $sql->sql_num_rows();
$pager = new pager( $p, $num );
echo $pager->showPager(); ?>
pozdrawiam
ZEZWALAM NA WSZELKI UZYTEK wiem ze klasa porazkowa, ale to byla klasa na poczatku mojej przygody z php. teraz napisze cos lepszego (IMG:
http://forum.php.pl/style_emoticons/default/winksmiley.jpg)
Ten post edytował Balon 21.08.2006, 18:58:31