美文网首页
PHP -- 数据库11 -- 分页函数封装

PHP -- 数据库11 -- 分页函数封装

作者: 潘肚饿兵哥哥 | 来源:发表于2019-09-25 22:25 被阅读0次

\color{rgba(254, 67, 101, .8)}{页码分页显示全部代码:}

<?php
    require_once('db.php');
    $sql = "select count(*) from goods";//数据总数
    $totalRows = get_one($sql);//总行数,get_one,自己封装的函数:查询一个数据 $totalRows值为31
    
    $pageStr = '';

    $listRows = 5;//每页显示5行数据
    $totalPages = ceil($totalRows/$listRows);//全部页数
    $nowPage = isset($_GET['p']) && intval($_GET['p']) > 0 ? intval($_GET['p']):1;
    //获取当前页  
    //isset()函数用于检测变量是否已设置并且非NULL。
    //intval取整,如果用户不是点击下一页,而是自己在搜索框中输入负数或者字母,直接跳转到首页
    //但是如果输入的数字超过总页数的话,这行代码不能跳转到首页,所以在下面判断:



    //如果当前页码大于总页数,就跳转到尾页$totalPages,如果不大于,就留在本页
    //$nowPage = $nowPage > $totalPages?$totalPages:$nowPage;

    $nowPage = min($nowPage, $totalPages);
    //min()函数返回一个数组中的最小值,或者几个指定值中的最小值。
    //这一种和上面$nowPage = $nowPage > $totalPages?$totalPages:$nowPage;都能实现
    //通过上面的三目运算和这里的两种方法中的一种,可以保证用户无论是输入过大过小的数字还是字母都不会出错




    //$prevPage = $nextPage = $nowPage;下面的两个if判断,也可以不写else
    //但是要先初始化(写)这一句,如果 上一页 = 下一页 = 当前页,就留在当前页(不作任何操作) 

    //判断,如果是尾页,点击上一页就留在当前页面
    //当$nowPage > 1  $nowPage < $totalPages时,首页、上一页、下一页、尾页全部输出,因为全部满足条件
    if($nowPage > 1){
        $prevPage = $nowPage-1;
        $pageStr .= '<a href="?p=1">首页</a>';// .=将右边参数附加到左边的参数之后
        $pageStr .= '<a href="?p='.$prevPage.'">上一页</a>';
        //将$pageStr在下方页面中输出
        //如果到达尾页,将只显示首页和上一页按钮

    }else{
        $prevPage = $nowPage;
    }

    /*
    
        页码分页显示效果设计:
          1 [2] 3 4
        1 2 [3] 4 5   ==>  3-2 3-1 [3] 3+1 3+2
        2 3 [4] 5 6   ==>  4-2 4-1 [4] 4+1 4+2
        4 5 [6] 7 
    */ 

    //页码分页显示效果设计:前半部分
    for($i=2; $i>0; $i--){
        $page = $nowPage - $i;
        if($page > 0){
            $pageStr .= '<a href="?p='.$page.'">'.$page.'</a>';
        }
        
    }

    $pageStr .= "<a class='current'>{$nowPage}</a>";


    //页码分页显示效果设计:后半部分
    for($i=1; $i<=2; $i++){
        $page = $nowPage + $i;
        if($page <= $totalPages){
            $pageStr .= '<a href="?p='.$page.'">'.$page.'</a>';
        }
    }



    //判断,如果是第一页,点击下一页就留在当前页面
    if($nowPage < $totalPages){
        $nextPage = $nowPage+1;//下一页
        $pageStr .= '<a href="?p='.$nextPage.'">下一页</a>';
        $pageStr .= '<a href="?p='.$totalPages.'">尾页</a>';

    }else{
        $nextPage = $nowPage;
    }

    
    $firstRow = ($nowPage-1)*$listRows;//起始行从0开始
    
    $sql = "select goods_id, goods_name, shop_price, goods_number from goods limit $firstRow,$listRows";
    //这里用函数设置起始行和每页显示数量,如果直接给数字就写死了
    $list = get_all($sql);
?>






<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>商品列表</title>
    <style>
        .page a{
            text-decoration: none;
            color: #333;
            display: inline-block;
            height: 24px;
            line-height: 24px;
             padding: 0 5px; /* 撑开边距 */
             font-family: "微软雅黑";
             font-size: 12px;
             border: 1px solid orange;
            margin-right: 5px;
            background: skyblue;
        }
        .page a.current, /* 给当前页设置颜色 高亮显示当前页码*/
        .page a:hover{
            color: orange;
            background: white;
        }
    </style>
</head>
<body>
<h3 align="center">商品列表</h3>
       <hr size="10" color="gray" />
       <table align="center" width="100%" border="1" cellspacing="0" cellspadding="3"><!-- cellspacing单元格间距 cellpadding单元格边距 -->
       
       <tr>
           <th>ID</th>
           <th>名称</th>
           <th>本店价格</th>
           <th>库存</th>
       </tr>

        <?php
            foreach($list as $v){       
        ?>

        <tr align="center">
            <td><?php echo $v['goods_id'];?></td>
            <td><?php echo $v['goods_name'];?></td>
            <td><?php echo $v['shop_price'];?></td>
            <td><?php echo $v['goods_number'];?></td>
        </tr>

        <?php
            }
        ?>
        <tr height="36">
            <td colspan="4" align="right">
                <div class="page"><?php echo $pageStr; ?></div>
            </td>
        </tr>
       </table>
</body>
</html>
image.png

\color{rgba(254, 67, 101, .8)}{分页函数封装:}

 //自定义函数库
    function page($totalRows, $listRows=5){//封装分页函数
        $pageStr = '';

        $totalPages = ceil($totalRows/$listRows);//全部页数
        
        if($totalPages <= 1){
            //如果数据一页就够显示,就不再输出页码和首页、尾页
            
            return ['firstRow' => 0, 'listRows' => $listRows, 'pageStr' => $pageStr];
        }

        $nowPage = isset($_GET['p']) && intval($_GET['p']) > 0 ? intval($_GET['p']):1;
        //获取当前页  
        //isset()函数用于检测变量是否已设置并且非NULL。
        //intval取整,如果用户不是点击下一页,而是自己在搜索框中输入负数或者字母,直接跳转到首页
        //但是如果输入的数字超过总页数的话,这行代码不能跳转到首页,所以在下面判断:



        //如果当前页码大于总页数,就跳转到尾页$totalPages,如果不大于,就留在本页
        //$nowPage = $nowPage > $totalPages?$totalPages:$nowPage;

        $nowPage = min($nowPage, $totalPages);
        //min()函数返回一个数组中的最小值,或者几个指定值中的最小值。
        //这一种和上面$nowPage = $nowPage > $totalPages?$totalPages:$nowPage;都能实现
        //通过上面的三目运算和这里的两种方法中的一种,可以保证用户无论是输入过大过小的数字还是字母都不会出错




        //$prevPage = $nextPage = $nowPage;下面的两个if判断,也可以不写else
        //但是要先初始化(写)这一句,如果 上一页 = 下一页 = 当前页,就留在当前页(不作任何操作) 

        //判断,如果是尾页,点击上一页就留在当前页面
        //当$nowPage > 1  $nowPage < $totalPages时,首页、上一页、下一页、尾页全部输出,因为全部满足条件
        if($nowPage > 1){
            $prevPage = $nowPage-1;
            $pageStr .= '<a href="?p=1">首页</a>';// .=将右边参数附加到左边的参数之后
            $pageStr .= '<a href="?p='.$prevPage.'">上一页</a>';
            //将$pageStr在下方页面中输出
            //如果到达尾页,将只显示首页和上一页按钮

        }else{
            $prevPage = $nowPage;
        }

        /*
        
            页码分页显示效果设计:
            1 [2] 3 4
            1 2 [3] 4 5   ==>  3-2 3-1 [3] 3+1 3+2
            2 3 [4] 5 6   ==>  4-2 4-1 [4] 4+1 4+2
            4 5 [6] 7 
        */ 

        //页码分页显示效果设计:前半部分
        for($i=2; $i>0; $i--){
            $page = $nowPage - $i;
            if($page > 0){
                $pageStr .= '<a href="?p='.$page.'">'.$page.'</a>';
            }
            
        }

        $pageStr .= "<a class='current'>{$nowPage}</a>";


        //页码分页显示效果设计:后半部分
        for($i=1; $i<=2; $i++){
            $page = $nowPage + $i;
            if($page <= $totalPages){
                $pageStr .= '<a href="?p='.$page.'">'.$page.'</a>';
            }
        }



        //判断,如果是第一页,点击下一页就留在当前页面
        if($nowPage < $totalPages){
            $nextPage = $nowPage+1;//下一页
            $pageStr .= '<a href="?p='.$nextPage.'">下一页</a>';
            $pageStr .= '<a href="?p='.$totalPages.'">尾页</a>';

        }else{
            $nextPage = $nowPage;
        }

        
        $firstRow = ($nowPage-1)*$listRows;//起始行从0开始
        return ['firstRow' => $firstRow, 'listRows' => $listRows, 'pageStr' => $pageStr];//return返回多个值就用数组返回
    }

\color{rgba(254, 67, 101, .8)}{调用封装好的函数库:}

<?php
    require_once('db.php');
    require_once('function.php');

    $sql = "select count(*) from goods";//数据总数
    $totalRows = get_one($sql);//总行数,get_one,自己封装的函数:查询一个数据 $totalRows值为31

    
    $page = page($totalRows);//调用函数中才会起作用
    
    $sql = "select goods_id, goods_name, shop_price, goods_number from goods limit {$page['firstRow']}, {$page['listRows']}";
    //这里用函数设置起始行和每页显示数量,如果直接给数字就写死了
    $list = get_all($sql);
?>






<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>商品列表</title>
    <style>
        .page a{
            text-decoration: none;
            color: #333;
            display: inline-block;
            height: 24px;
            line-height: 24px;
             padding: 0 5px; /* 撑开边距 */
             font-family: "微软雅黑";
             font-size: 12px;
             border: 1px solid orange;
            margin-right: 5px;
            background: skyblue;
        }
        .page a.current, /* 给当前页设置颜色 高亮显示当前页码*/
        .page a:hover{
            color: orange;
            background: white;
        }
    </style>
</head>
<body>
<h3 align="center">商品列表</h3>
       <hr size="10" color="gray" />
       <table align="center" width="100%" border="1" cellspacing="0" cellspadding="3"><!-- cellspacing单元格间距 cellpadding单元格边距 -->
       
       <tr>
           <th>ID</th>
           <th>名称</th>
           <th>本店价格</th>
           <th>库存</th>
       </tr>

        <?php
            foreach($list as $v){       
        ?>

        <tr align="center">
            <td><?php echo $v['goods_id'];?></td>
            <td><?php echo $v['goods_name'];?></td>
            <td><?php echo $v['shop_price'];?></td>
            <td><?php echo $v['goods_number'];?></td>
        </tr>

        <?php
            }
        ?>
        <tr height="36">
            <td colspan="4" align="right">
                <div class="page"><?php echo  $page['pageStr']; ?></div>
            </td>
        </tr>
       </table>
</body>
</html>


\color{rgba(254, 67, 101, .8)}{前面封装的函数样式写死了,可以写的更灵活:}
\color{rgba(254, 67, 101, .8)}{里面的文字可以随意更改,顺序也可以随意调换:}

<?php

//自定义函数库
//之前的函数把样式写死了,可以写的更灵活


//分页函数封装
function page($totalRows, $listRows=5, $config=[]){//把要灵活使用的几个样式封成一个数组传过来
    $pageStr = '';

    $totalPages = ceil($totalRows/$listRows);//取最小整数

    if($totalPages <= 1){//在第一页时不显示分页
        return ['firstRow'=>0, 'listRows'=>$listRows, 'pageStr'=>$pageStr];//return后面的代码不会被执行,所以只要return就可以了,不要else
    }


    $header = !empty($config['header'])?$config['header']:'首页';
    $prev = !empty($config['prev'])?$config['prev']:'上一页';
    $next = !empty($config['next'])?$config['next']:'下一页';//使用文字显示下一页还是自定义设置下一页的样式
    $end = !empty($config['end'])?$config['end']:'尾页';


    $theme = !empty($config['theme'])?$config['theme']:'%header% %upPage% %linkPage% %downPage% %end%';//%是占位,最终生成的样式就是这样的,替换之后在调用这个函数之后,可以随意更换5个元素位置达到作为一个模板的作用

    $nowPage = isset($_GET['p']) && intval($_GET['P'])>0?intval($_GET['p']):1;//当前页码
    //$nowPage = $nowPage > $totalPages;?$totalPages:$nowPage;这种写法和下面的写法一样
    $nowPage = min($nowPage, $totalPages);//返回最小值
    $firstRow = ($nowPage-1)* $listRows;//起始行数

    $headerPage = $linkPage = $upPage = $downPage = $endPage = '';//赋初值,不赋值的话会报notice错误

    if($nowPage > 1)
    {
        $prevPage = $nowPage-1;
        $headerPage = '<a href="?p=1">'.$header.'</a>';
        $upPage = '<a href="?p='.$prevPage.'">'.$prev.'</a>';
    }


    $linkPage = '';

/* 
    
        页码分页显示效果设计:
        1 [2] 3 4
        1 2 [3] 4 5   ==>  3-2 3-1 [3] 3+1 3+2
        2 3 [4] 5 6   ==>  4-2 4-1 [4] 4+1 4+2
        4 5 [6] 7 

    */

    //页码分页显示效果设计:前半部分
    for($i=2; $i>0; $i--){
        $page = $nowPage - $i;
        if($page > 0){
            $pageStr .= '<a href="?p='.$Page.'">'.$page.'</a>';
        }
        
    }

    $linkPage .= "<a class='current'>{$nowPage}</a>";


    //页码分页显示效果设计:后半部分
    for($i=1; $i<=2; $i++){
        $page = $nowPage + $i;
        if($page <= $totalPages){
            $linkPage .= '<a href="?p='.$Page.'">'.$page.'</a>';
        }
    }


    if($nowPage < $totalPages)
    {
        $nextPage = $nowPage+1;//下一页
        $downPage = '<a href="?p='.$nextPage.'">'.$next.'</a>';
        $endPage = '<a href="?p='.$totalPages.'">'.$end.'</a>';
    }


    
    //'%header% %upPage% %linkPage% %downPage% %end%'
    $pageStr = str_replace(['%header%', '%upPage%', '%linkPage%', '%downPage%', '%end%'], [$headerPage, $upPage, $linkPage, $downPage, $endPage], $theme);
    //这样写可以将这5个元素随机设置显示顺序
    //将5个带有占位符的元素和5个变量一一对应起来,这样在调用这个函数的时候,他就是一个模板的作用5个元素的位置可以随便改而不会出错,


    return ['firstRow'=>$firstRow, 'listRows'=>$listRows, 'pageStr'=>$pageStr];
}
//以上代码是分页函数封装
?>

\color{rgba(254, 67, 101, .8)}{调用新的更灵活的函数:}


<?php
    require_once('study.php');
    require_once('function.php');//加载封装好的分页函数库
    $sql = "select count(*) from goods";
    $totalRows = get_one($sql);//获取总行数

    $config = ['next'=>'>>', 'prev'=>'<<', 'theme' = '%header% %upPage% %linkPage% %downPage% %end%'];//调用封装好的函数,给定要修改的样式参数,上一页下一页不用文字,用符号显示,不给这个参数的话,就使用默认参数
    //theme就是模板,这5个元素可以随意调换位置,直接改参数就可以



    $page = page($totalRows, 5, $config);//调用封装好的函数

    $sql = "select goods_id, goods_name, sop_price, goods_number from goods limit {$page['firstRow']}, {$page['listRows']}";//查询;每页显示条数
    $list = get_all($sql);
?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>商品列表</title>
    <style>
        .page a{
            text-decoration: none; 
            color: #fff;
            display: inline-block;
            height: 24px;
            line-height: 24px;
            padding: 0 5px;
            font-family: "微软雅黑";
            font-size: 12px;
            border: 1px solid #dfdfdf;
            margin-right: 5px; 
            background: #f5f5f5;
            }

        .page a.current, /* 给当前页设置颜色 高亮显示当前页码*/
        .page a:hover{
            color: orange;
            background: #00cc00;
        }
    </style>
</head>
    <body>
       <h3 align="center">商品列表</h3>
       <hr size="10" color="gray" />
       <table align="center" width="1200" border="1" cellspacing="0" cellpadding="3"><!-- cellspacing单元格间距 cellpadding单元格边距 -->
       
       <tr>
           <th>ID</th>
           <th>名称</th>
           <th>本店价格</th>
           <th>库存</th>
       </tr>

        <?php
            foreach($list as $v){       
        ?>

        <tr align="center">
            <td><?php echo $v['goods_id'];?></td>
            <td><?php echo $v['goods_name'];?></td>
            <td><?php echo $v['shop_price'];?></td>
            <td><?php echo $v['goods_number'];?></td>
        </tr>

        <?php
            }
        ?>

        <tr height="36">
            <td colspan="4" align="right">
                <div class="page"><?php echo $page['pageStr']]; ?></div>
            </td>
        </tr>  
       </table>
</body>
</html>


相关文章

网友评论

      本文标题:PHP -- 数据库11 -- 分页函数封装

      本文链接:https://www.haomeiwen.com/subject/taahyctx.html