Blogger Information
Blog 56
fans 3
comment 1
visits 50684
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
mysql分页——2018年4月29日
沈斌的博客
Original
757 people have browsed it

php+mysql实现分页,前一页后一页,尾页首页,以及中间页,被点击的页码会高亮显示。

高亮的原理是判断当前页$page 与 被选择页$i是否相同。

lib/func_page.php

实例

<?php
/医院
 *
 */
if (!function_exists('func_page')){
    /医院
     * @param $db
     * @param $table
     * @param int $page
     * @param int $num
     * @return array
     */
    function func_page($db,$table,$page=1,$num=3) {
        $offset=((int)$page-1)*$num;
        $sql="SELECT * FROM {$table} LIMIT {$offset},{$num}";
        $res=mysqli_query($db,$sql);
        $rows=mysqli_fetch_all($res,MYSQLI_ASSOC);

        $number=mysqli_query($db,"SELECT COUNT(*) FROM {$table}");
        list($total)=mysqli_fetch_row($number);
        //总页数
        $pages=ceil($total/$num);
        return ['rows'=>$rows,'pages'=>$pages];
    }
}

运行实例 »

点击 "运行实例" 按钮查看在线实例

page2.php

实例

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>page</title>
    <style>
        table,th,td {
            border:1px solid black;
        }

        table th {
            background-color: lightblue;
        }

        table {
            border-collapse: collapse;
            width: 70%;
            text-align: center;
            margin: 30px auto;
        }

        h3 {
            text-align: center;
        }

        h3 a {
            text-decoration: none;
            margin-left: 20px;
            border: 1px solid black;

            display: inline-block;
            background-color: lightgreen;
        }

        h3 a:hover,.active {
            background-color: orange;
            color: white;
        }
        form {
            display: inline;
        }
    </style>
</head>
<body>
<?php
require 'lib/func_page.php';

//connect db
$db= mysqli_connect('localhost','root','root','user');
//Get current page
$page=isset($_GET['page']) ? $_GET['page']:1;
//每页显示3条记录
$num=3;
$table='staf';

$data=func_page($db,$table,$page,$num);
$rows=$data['rows'];
$pages=$data['pages'];

$page=($page == 0) ? 1:$page;
$page=($page>$pages) ? $pages:$page;

$link=$_SERVER['PHP_SELF'].'?page=';
?>

<table>
    <caption><h2>员工信息表</h2></caption>
    <tr>
        <th>name</th>
        <th>sex</th>
        <th>age</th>
        <th>salary</th>
    </tr>

    <?php foreach ($rows as $row): ?>
        <tr>
            <td><?php echo $row['name']; ?></td>
            <td><?php echo $row['sex']; ?></td>
            <td><?php echo $row['age']; ?></td>
            <td><?php echo $row['salary']; ?></td>
        </tr>
    <?php endforeach;?>
</table>

<!--翻页显示    -->
<h3>
    <?php if ($page != 1): ?>

        <a href="<?php echo $link.'1'; ?>">首页</a>
        <a href="<?php print $link.($page-1); ?>">上一页</a>
    <?php endif; ?>
    <!--中间页码-->
    <?php for ($i=1;$i<=$pages;$i++):?>
        <a class="<?php if($page==$i){echo 'active';} ?>" href="<?php print $link.$i ?>"><?php echo $i ?></a>
    <?php endfor; ?>

    <?php if ($page != $pages): ?>

        <a href="<?php print $link.($page+1); ?>">下一页</a>
        <a href="<?php echo $link.$pages; ?>">尾页</a>
    <?php endif; ?>


    <!--快速跳转-->
    <form action="" method="get">
        第
        <select name="page" id="page">
            <?php for($i=1;$i<=$pages;$i++): ?>
                <option value="<?php echo $i ?>" <?php if($page == $i){echo 'selected';}?>>
                    <?php echo $i; ?>
                </option>
            <?php endfor; ?>
        </select>
        页
        <button>跳转</button>
    </form>
</h3>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

employee.png


Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post