Здравейте! Тъй като работех по един проект, ми се наложи да си направя скрипт за странициране. Реших да го лицензирам под MIT, за да може всеки, който иска да го използва без граници :). Можете да видите кода в PasteBin като кликнете тук, или просто да го копирате от тук:

<?php
/* Pagination script v0.1
 * Author: Yasen Georgiev
 * Email: avbincco@gmail.com
 * Web site: http://ygeorgiev.com/
 * License: MIT - http://ygeorgiev.com/licenses/MIT-LICENSE
*/

$commentsPerPage = 5; // Items per page
$currentPage = (int)$_GET['p']; // Your pagination's GET parameter
$getNumRows = mysql_num_rows(mysql_query('SELECT `column` FROM `table`')); // Set your Query to get count of the results in table
$totalPages = $getNumRows/$commentsPerPage;
$intvalPages = intval($totalPages);
$lastPage = ceil($getNumRows / $commentsPerPage);
$paginationPath = 'page.php?id='.(int)$_GET['id'].'&p='; //Set your URL path

if($totalPages-$intvalPages>0) {
    $totalPages = $intvalPages+1;
}

if($currentPage <= 1 || $currentPage>$totalPages) {
    $start = 0;
    $end = $commentsPerPage;
    $currentPage = "1";
} else {
    $start = ($currentPage-1)*$commentsPerPage+1;
    $end = $currentPage*$commentsPerPage;
}
if($lastPage>1) {
    if($currentPage > 1) {
        $pagesNum .= '<a href="'.$paginationPath.($currentPage-1).'">« Previous page</a> ';
    }
    if($lastPage <= 10) {
        for ($i = 1; $i <= $lastPage; $i++) {
            $pagesNum .= '<a href="'.$paginationPath.$i.'">'.$i.'</a> | ';
        }
    } else {
        if($currentPage <= 5) {
            for($i = 1; $i <= 10; $i++) {
                $pagesNum .= ' | <a href="'.$paginationPath.$i.'">'.$i.'</a> ';
            }
            $pagesNum .= ' | ... | <a href="'.$paginationPath.$lastPage.'">'.$lastPage.'</a> | ';
        } elseif($currentPage > 5 && $currentPage < ($lastPage-5)) {
            $pagesNum .= ' | <a href="'.$paginationPath.'1">1</a> | ... | ';
            for($i = $currentPage-3; $i <= $currentPage+3; $i++) {
                $pagesNum .= '<a href="'.$paginationPath.$i.'">'.$i.'</a> | ';
            }
            $pagesNum .= ' ... | <a href="'.$paginationPath.$lastPage.'">'.$lastPage.'</a> | ';
        } elseif($currentPage >= ($lastPage-5)) {
            $pagesNum .= ' | <a href="'.$paginationPath.'1">1</a> | ... | ';
            if($currentPage < ($lastPage-2)) {
                for($i = $currentPage-3; $i <= $currentPage + 3; $i++) {
                    $pagesNum .= '<a href="'.$paginationPath.$i.'">'.$i.'</a> | ';
                }
            } else {
                for($i = $lastPage - 5; $i <= $lastPage; $i++) {
                    $pagesNum .= '<a href="'.$paginationPath.$i.'">'.$i.'</a> | ';
                }
            }
        }
    }
    if($currentPage < $lastPage) {
        $pagesNum .= ' <a href="'.$paginationPath.($currentPage+1).'">Next page »</a>';
    }
    $nomeration = str_replace('<a href="'.$paginationPath.$currentPage.'">'.$currentPage.'</a>','<b>'.$currentPage.'</b>',$pagesNum);
}

echo $nomeration; //Show the pagination
?>
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on LinkedIn0Share on Reddit0Email this to someone