Home  >  Article  >  Backend Development  >  PHP implementation of paging: text paging and number paging_PHP tutorial

PHP implementation of paging: text paging and number paging_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:15:58784browse

PHP implements paging: text paging and number paging

Source: http://www.ido321.com/1086.html

Recently, paging is used in projects. The paging function is a frequently used function, so it is encapsulated in the form of a function.

// 分页分装
/**
*   $pageType   分页类型 1是数字分页  2是文本分页
*  可以将$pageTotal,$page,$total等数据作为参数传递,或者在paging作为全局变量(推荐)
*/
function paging($pageType)
{
    global $pageTotal,$page,$total;
    if($pageType == 1)
    {
        echo '<div id="pagenum">';
            echo'<ul>';
                for($i=0; $i < $pageTotal; $i&#43;&#43;)
                { 
                    if($page == ($i&#43;1))
                    {
                        echo '<li>'.($i&#43;1).'</li>';
                    }
                    else
                    {
                        echo '<li>'.($i&#43;1).'</li>';
                    }
                }
            echo'</ul>';
        echo'</div>';

    }
    else if($pageType == 2)
    {
        echo '<div id="pagetext">';
            echo '<ul>';
                echo '<li>'.$page.'/'.$pageTotal.'页 | </li>';
                echo '<li>共有<strong>'.$total .'</strong>个会员 | </li>';
                    // 第一页
                    if($page == 1)
                    {
                        echo '<li>首页 | </li>';
                        echo '<li>上一页 | </li>';
                    }
                    else
                    {
                        // $_SERVER["SCRIPT_NAME"]获取当前的脚本名字,方便移植
                        // 也可以自定义常量,常量&#20540;和脚本文件名一致
                        echo '<li>首页 | </li>';
                        echo '<li>上一页 | </li>';
                    }
                    // 最后一页
                    if($page == $pageTotal)
                    {
                        echo '<li>下一页 | </li>';
                        echo '<li>尾页 | </li>';
                    }
                    else
                    {
                        echo '<li>下一页 | </li>';
                        echo '<li>尾页 | </li>';
                    }        
            echo '</ul>';
        echo '</div>';
    }
}

Parameter explanation:

$pageTotal is the total number of pages, $page is the current page, $total is the total number of data obtained from the database;

For simplicity, all parameters are encapsulated

// 分页参数分装
/**
*   $sql   可以 获取数据总数的一个sql语句
*  $size  每一页显示条数
*/
function pageParam($sql,$size)
{
    // 将所有涉及的参数设置全局变量
    // $pagestart  某一页从哪里开始
    // $total  总记录数   $page  某一页  $pageTotal  总页数
    global $pagestart,$pagesize,$total,$page,$pageTotal;
    $pagesize = $size;
    // 获取数据总数
    $total = mysql_num_rows(queryDB($sql));

    // 错误处理,先判断是否存在
    if(isset($_GET['page']))
    {
        // 具体某一页
        $page = $_GET['page'];
        // 判断是否为空(0是空)/小于0/是否是数字
        if(empty($page) || $page < 0 || !is_numeric($page))
        {
            $page = 1;
        }
        else
        {
            $page = intval($page);  //取整,防止小数出现
        }
        
    }
    else
    {
        // 初始化显示第1页
        $page = 1;
    }

    // 数据库清零
    if($total == 0)
    {
        // 设置为1
        $pageTotal = 1;
    }
    else
    {
        // 分页的总页数(进一取整处理)
        $pageTotal = ceil($total / $pagesize);
    }

    // 页数大于总页码$total
    if($page > $pageTotal)
    {
        $page = $pageTotal;
    }
    // 当页从某一条记录开始
    $pagestart = ($page - 1) * $pagesize;
}

Parameter explanation:

$pagestart is when the page starts from a certain record, $pagesize is the number of records displayed on each page

In use, call pageParam first, then call paging

/**
*  第一个  可以 获取数据总数的一个sql语句
*  第二个  每一页显示条数
*/
pageParam("select userid from user",2);
<?php 
    // 分页类型 1是数字分页  2是文本分页
      paging(2);
?>    

The calling position is selected according to the specific situation. The text is paginated as follows:

<?php 
       // 分页类型 1是数字分页  2是文本分页
        paging(1);
 ?>    

Numbers are paginated as follows:

The style can be adjusted by yourself.

Next article: A small example of displaying a map using Google Maps API

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/900030.htmlTechArticlePHP implementation of paging: text paging and number paging Source: http://www.ido321.com/1086.html Recently, paging has been used in projects. The paging function is a frequently used function, so...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn