Pagination of site content

Hello, can you advise or maybe there is already a script ready to divide the content of the site into pages?


Answer 1, authority 100%

Check these links:

In general, you can see for yourself on the Internet, hundreds of examples. Search for “pagination” or “pagination”.


Answer 2

<?
/*
 *       news( )
 *        
 *  -       
 *  .     $_GET['page']
 *   ,       
 *     
*/
function getFirstPage() {
    //  ...
    $res = mysql_query('SELECT * FROM news ORDER BY date DESC LIMIT 10');
    $news = array(); //  
    while($row = mysql_fetch_assoc($res)) {
        $news[] = $row;  // 
    }
    $links = getPaginationLinks();
    $result = array('news'=>$news, 'links'=>$links);
    return $result;
}
function getNews() {
    if($_GET['page'] && $_GET['page'] >= 1) {
        //     page 1
        if($_GET['page'] == 1) {
            return getFirstPage();
        }
        else {
            $limit = ($_GET['page']*10-10) . ', 10';
            $res = mysql_query('SELECT * FROM news ORDER BY date DESC LIMIT ' . $limit);
            $news = array();
            while($row = mysql_fetch_assoc($res)) {
                  $news[] = $row;
            }
            // ,       ...
            if(empty($news)) {
                 die(); //die    ,     404....
            }
            $links = getPaginationLinks(); //    
            //      ....
            $result = array('news'=>$news, 'links'=>$links);
            //           
            //    
            return $result;
        }
    }
    else {
         //  $_GET['page'] ,     ...
         return getFirstPage();
    }
}
function getPaginationLinks() {
    //     ...
    $res = mysql_query('SELECT COUNT(*) AS count FROM news');
    $count = mysql_fetch_assoc($res);
    $count = $count['count'];
    //     ...
    $links = array();
        //      10    
        // ..     1,2,3,4,5,6,7,8,9,10 -  
        //  11,12,13,14,15,16,17,18,19,20 -   ..
        //         ...
        $group = floor($_GET['page'] / 10); //floor    
        //          ...
        if($group*10+10 <= $count) {
            $max = $group*10+10; 
        }
        else {
        //       $group*10+10,    
            $max = floor($count / 10);
        }
        //      ...
        for($i=$group*10; $i<=$max; $i++) {
             $links[] = $i + 1;
        }
        return $links; //...
}
?>
    ,         ..          ,     ...
    ,         ,      ... C JS, :
<!--         paginationLinks...-->
<script type="text/javascript">
onload = function() {
    getCurrentPaginationLink();
}
function getCurrentLink() {
    links = document.getElementsByClassName("paginationLinks");
    for(var i=0; i<links.length; i++) {
         //  document.location.href   url 
         //       ....
         if(links[i] == document.location.href) {
             //     -  
             //  ,      ...
             links[i].style.backgroundColor = "red";
             return;
         }
    }
}
</script>

Check it out 🙂