Nápady na rozšíření : phpRS Fórum
Napadlo vás zajímavé rozšíření? Myslíte si, že phpRS systému chybí nějaká důležitá funkce? Pokud ano, tak neváhejte a napište nám!
phpRS - redakční a informační systém
Out of blue patch: Strankovani na indexu
Zaslán uživatelem/kou: ptacek.pavel (IP adresa zaznamenána)
Datum: 2008-12-10, 19:40

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.

Re: Out of blue patch: Strankovani na indexu
Zaslán uživatelem/kou: ptacek.pavel (IP adresa zaznamenána)
Datum: 2008-12-10, 19:52

Mozna, abych dovysvetlil... Vytiskne to strankovani podle toho, na jake strance jste.

Za predpokladu ze mame 6 stranek, strankovani bude postupne vypadat takto: (podle aktualni stranky)

1: 1-10 | 11-20 | 21-30 | ... | 61-70 | další
2: předchozí | 1-10 | 11-20 | 21-30 | ... | 61-70 | další
3: předchozí | 1-10 | 11-20 | 21-30 | ... | 61-70 | další
4: předchozí | 1-10 | ... | 21-30 | 31-40 | 41-50 | ... | 61-70 | další
5: předchozí | 1-10 | ... | 41-50 | 51-60 | 61-70 | další
6: předchozí | 1-10 | ... | 41-50 | 51-60 | 61-70

... jinak koukam ze v nove verzi uz to je. V tom pripade stoji za zminku pouze system zobrazeni strankovani .. :)



Celkem upraveno 1×. Poslední úprava ptacek.pavel v 10.12.2008 19:59.



Lituji, ale pouze registrovaní uživatelé mohou zasílat příspěvky do této sekce.
This forum powered by Phorum and designed by STaNBoSS.