Zdravím,
používáme phpRS v2.3.5a & v2.3.5b . Upgrade na vyšší verzi není možný.
Doplnění funkce stránkování na index - tedy výpis článků. Zde je patch (funguje pouze pro 10 článků na indexu, kdyžtak to doupravte :-)
/* trclanek.php: */
class CClanek {
// Add:
var $articlesTotal;
// ---------------------------------------------------------------------------------------------------------------
// Add:
/**
* Displays paging based on total articles.
*
* In order this function work, we need to update several other functions.
*
* Just to realize what we need out of paging:
* 1 to 4 pages: We display all pages number
* 5 to N pages: We display first three if we are on them.
* We display last three, if we are on them.
* First and last page is always visible
* Current page is displayed along with previous and next page, separated by dots
*
* Because of that:
* First page is visible.
* Second page is visible, if we have four pages OR we are on first three
* Third page is visible, if we have four pages OR we are on first three
* Dots after third page are visible if we have more than four pages
*
* Fourth page is not hardcoded.
*
* Middle-counter with dots is visible, if we are not on first three and last three.
* Pages near counter are always visible because they will not interfear.
*
* Last three are visible, if we are on them AND we have more than four pages
* Dots before last three are visible all the time
*
* Last page is always visible.
*
* Try & see :)
*
* @author Pavel Ptacek <ptacek.pavel at gmail dot com>
* @return void
*/
function printPaging() {
// Get the paging variables
$_GET['from'] = empty($_GET['from']) ? 0 : intval($_GET['from']);
$currentPage = floor($_GET['from'] / 10) + 1;
$lastPage = ceil($this->articlesTotal / 10);
// Resuplly url
$uri = $_SERVER['REQUEST_URI'];
// Sub the &from tag
if(($pos = strpos($uri, 'from=')) !== false) {
$uri = substr($uri, 0, $pos - 1);
}
// Determine the ampersand in the uri
if(substr_count($uri, '?') > 0) {
$uri .= '&from=';
}
elseif(substr($uri, 0, -1) != '/' || substr_count($_SERVER['REQUEST_URL'], '.') > 0) {
$uri .= '?from=';
}
else {
$uri .= '/?from=';
}
// Everything we make as output string (it is array because of later coupling with |)
$output = array();
// First page is always visible along with active / inactive prev link
if($currentPage > 1) {
$output[] = '<a href="' . $uri . (($currentPage - 2) * 10) . '">předchozí</a>';
$output[] = '<a href="' . $uri . '0">1-10</a>';
}
else {
$output[] = '1-10';
}
// Second page is visible, if we have four pages OR we are on first three
if($lastPage == 4 || ($currentPage <= 3 && $lastPage > 1)) {
if($currentPage == 2) {
$output[] = '11-20';
}
else {
$output[] = '<a href="' . $uri . '10">11-20</a>';
}
}
// Third page is visible, if we have four pages, or we are on first three
if($lastPage == 4 || ($currentPage <= 3 && $lastPage >= 3)) {
if($currentPage == 3) {
$output[] = '21-30';
}
else {
$output[] = '<a href="' . $uri . '20">21-30</a>';
}
// Dots after third page is visible, if we have more than four pages
if($lastPage > 4 && !($lastPage == 5 && $currentPage == 3)) {
$output[] = '...';
}
}
// Middle counter along with dots and everything is visible, if we are not on first three or last three
if($currentPage > 3 && $currentPage <= ($lastPage - 2)) {
$output[] = '...';
$output[] = '<a href="' . $uri . (($currentPage - 2) * 10) . '">' . (($currentPage - 2) * 10 + 1) . '-' . (($currentPage - 2) * 10 + 10) . '</a>';
$output[] = (($currentPage - 1) * 10 + 1) . '-' . (($currentPage - 1) * 10 + 10);
$output[] = '<a href="' . $uri . ($currentPage * 10) . '">' . ($currentPage * 10 + 1) . '-' . ($currentPage * 10 + 10) . '</a>';
$output[] = '...';
}
// All last three pages along with dots are visible, if we are on them and we have more than 4 pages
if($lastPage > 4 && $currentPage > ($lastPage - 2)) {
if($currentPage != 3) {
$output[] = '...';
}
if($currentPage != 3) {
$from = ($lastPage - 2) * 10 + 1;
$to = ($lastPage - 2) * 10 + 10;
if($currentPage == ($lastPage - 2)) {
$output[] = $from . '-' . $to;
}
else {
$output[] = '<a href="' . $uri . ($from - 1) . '"> ' . $from . '-' . $to . '</a>';
}
}
$from = ($lastPage - 1) * 10 + 1;
$to = ($lastPage - 1) * 10 + 10;
if($currentPage == ($lastPage - 1)) {
$output[] = $from . '-' . $to;
}
else {
$output[] = '<a href="' . $uri . ($from - 1) . '"> ' . $from . '-' . $to . '</a>';
}
}
// And link to last page is always visible
$from = $lastPage * 10 + 1;
$to = $lastPage * 10 + 10;
if($currentPage == $lastPage && $lastPage > 4) {
$output[] = $from . '-' . $to;
}
elseif($currentPage != $lastPage && $lastPage > 4) {
$output[] = '<a href="' . $uri . ($from - 1) . '">' . $from . '-' . $to . '</a>';
$output[] = '<a href="' . $uri . ($currentPage * 10) . '">další</a>';
}
elseif($currentPage != $lastPage) {
$output[] = '<a href="' . $uri . ($currentPage * 10) . '">další</a>';
}
// And echo the output
echo implode(' | ', $output);
}
// ---------------------------------------------------------------------------------------------------------------
// Find:
function NactiClanky($mnozstvi = 0)
// Replace with:
function NactiClanky($mnozstvi = 0, $from = 0)
// ---------------------------------------------------------------------------------------------------------------
// In function NactiClanky find:
// dotaz
$this->dotazclanek=mysql_query("select ".$select." from ".$GLOBALS["rspredpona"]."clanky".$omezenidotazu." order by priority desc,datum desc limit 0,".$mnozstvi,$GLOBALS["dbspojeni"]);
$this->pocetclanku=mysql_num_rows($this->dotazclanek);
if ($this->pocetclanku>0):
// vse OK
$this->clanek=mysql_fetch_assoc($this->dotazclanek);
$this->aktpozice=0;
$this->aktivni=1; // trida je aktivni
else:
// chyba
$this->aktivni=0; // trida je v neaktivnim stavu
endif;
// and replace with:
// dotaz
$this->dotazclanek=mysql_query("select ".$select." from ".$GLOBALS["rspredpona"]."clanky".$omezenidotazu." order by priority desc,datum desc limit " . $from . ",".$mnozstvi,$GLOBALS["dbspojeni"]);
$this->pocetclanku=mysql_num_rows($this->dotazclanek);
if ($this->pocetclanku>0):
// vse OK
$this->clanek=mysql_fetch_assoc($this->dotazclanek);
$this->aktpozice=0;
$this->aktivni=1; // trida je aktivni
// And load total articles
$res = mysql_query("SELECT COUNT(idc) FROM " . $GLOBALS["rspredpona"] . "clanky " . $omezenidotazu);
$row = mysql_fetch_row($res);
$this->articlesTotal = $row[0];
else:
// chyba
$this->aktivni=0; // trida je v neaktivnim stavu
endif;
// ---------------------------------------------------------------------------------------------------------------
/* Index.php */
// In function HlavniBlok find:
$GLOBALS["clanek"] = new CClanek();
$GLOBALS["clanek"]->HlidatPlatnost(NactiConfigProm('hlidat_platnost',0));
$GLOBALS["clanek"]->NastavHlavStr(1); // aktivace indentifikatoru hl. str.
$GLOBALS["clanek"]->NactiClanky(NactiConfigProm('pocet_clanku',0));
$GLOBALS["clanek"]->NactiAutory();
$GLOBALS["clanek"]->NactiTemata();
// After that add:
echo '<p align="center" class="z">';
echo $GLOBALS["clanek"]->printPaging();
echo '</p>';
// In HlavniBlok the line:
$GLOBALS["clanek"]->NactiClanky(NactiConfigProm('pocet_clanku',0));
// Replace with:
$GLOBALS["clanek"]->NactiClanky(NactiConfigProm('pocet_clanku',0), $_GET['from']);
// yupi-a-jej
A tak, snad to nekomu pomuze. Jinak je to prijemny system strankovani ktery se da pouzit i na jinych projektech.
S pozdravem,
Pavel Ptacek
Celkem upraveno 3×. Poslední úprava ptacek.pavel v 15.04.2010 17:15.