Blogger Information
Blog 38
fans 0
comment 1
visits 30390
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
分页函数,封装常用操作
1
Original
574 people have browsed it

实例

<!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>Document</title>
    <style>
        table,th,td{
            border:1px solid black;
        }
        table{
            border-collapse: collapse;
            width: 70%;
            margin: 30px auto;
            text-align: center;
        }
        h3{
            text-align: center;
        }
        h3 a{
            text-decoration: none;
        }
    </style>
</head>
<body>
<?php
$db = mysqli_connect('127.0.0.1','root','root');
mysqli_select_db($db,'php');
$num = 5;
$page = isset($_GET['p'])? $_GET['p']:1;
$offset = ($page-1) * $num;
$sql = "SELECT * FROM staff LIMIT {$offset},{$num}";
$res = mysqli_query($db,$sql);

$rows = mysqli_fetch_all($res,MYSQLI_ASSOC);
//计算总页数
$number = mysqli_query($db,'SELECT COUNT(*) FROM staff');
list ($total) = mysqli_fetch_row($number);//当前记录总数在$total
$pages = ceil($total / $num);
//如果当前页数为0,强制为1

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

?>
<table>
    <caption><h2>员工信息表</h2></caption>
    <tr>
        <th>ID</th>
        <th>姓名</th>
        <th>性别</th>
        <th>年龄</th>
        <th>工资</th>
    </tr>
    <?php
    foreach ($rows as $row):
        ?>
        <tr>
            <td><?php echo $row['staff_id']?></td>
            <td><?php echo $row['name']?></td>
            <td><?php echo $row['sex']?></td>
            <td><?php echo $row['age']?></td>
            <td><?php echo $row['salay']?></td>
        </tr>
    <?php
    endforeach;
    ?>
</table>
<h3>
    <a href="http://127.0.0.1/515/demo5.php?p=1">首页</a>
    <a href="http://127.0.0.1/515/demo5.php?p=<?php echo $page;?>">上一页</a>

    <?php
    for ($i=1;$i<=$pages;$i++):;
        ?>
        <a href="http://127.0.0.1/515/demo5.php?p=<?php echo $i?>"><?php echo $i?></a>

    <?php
    endfor;
    ?>

    <a href="http://127.0.0.1/515/demo5.php?p=<?php echo $page;?>">下一页</a>
    <a href="http://127.0.0.1/515/demo5.php?p=<?php echo $pages; ?>">尾页</a>
</h3>
</body>
</html>

运行实例 »

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


Correction status:Uncorrected

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
  • 1
    2018-03-16 00:39:40
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!