PHP 作业3.4 分页

要求


自定义每页展示几条数据,实现分页功能:首页、上一页、下一页、尾页。


效果图


在这里插入图片描述


index.php代码


<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="utf-8">
    <title></title>
    <style>
        body {
            background-color: #dedede;
        }

        .main {

            width: 1058px;
            margin: 50px auto;
        }

        h1 {
            margin: 50px auto;
            text-align: center;
        }

        form {

            margin-bottom: 20px;
        }

        a {
            text-decoration: none;
            color: #000;
        }

        a:hover {
            color: deeppink;
        }

        .edit {
            color: #0e84b5;
        }

        .del {
            color: #9A0000;
        }

        table {
            border-collapse: collapse;

        }

        th {
            background-color: #00bcff;
            padding: 20px 40px;
        }

        tr {
            text-align: center;
        }

        td {
            padding: 20px 40px;
        }

        .fenye {
            margin-top: 20px;
        }

        .fenye a {

            margin-right: 20px;
        }
    </style>
</head>

<body>

    <?php
    //连接数据库
    $link = mysqli_connect("localhost", "root", "root");
    if (!$link) {
        echo die("数据库链接失败") . mysqli_error();
    } else {
        //选择数据库
        mysqli_query($link, "use zhuang");
        /* ——————搜索—————— */
        //输入数值初始化为空
        $sql_where = "";
        //判断是否存在输入的数值
        if (isset($_GET['txt'])) {
            //变量等于输入数值
            $where = $_GET['txt'];
            //用户输入的搜索信息不能直接使用,可能存在导致SQL语句执行失败,需要使用函数进行转义
            $where = mysqli_real_escape_string($link, $where);
            //数据库模糊搜索输入数值
            $sql_where = "where `e_id` like '%$where%' or `e_name` like '%$where%' or `e_dept` like '%$where%' or `date_of_birth` like '%$where%' or `date_of_entry` like '%$where%'";
        }

        /* ——————排序—————— */
        // 初始化排序语句,用来组合排序的order子句
        $sql_order = "";
        //允许排序的字段
        $field = array('e_dept', 'date_of_entry');
        //判断$_GET['order']是否存在,如果存在则将其赋值给$order
        if (isset($_GET['order'])) {
            $order = $_GET['order'];
        } else {
            // 如果不存在则把空字符串赋值给$order
            $order = '';
        }

        if (isset($_GET['sort'])) {
            $sort = $_GET['sort'];
        } else {
            $sort = '';
        }


        //判断$order是否存在于合法字段列表$fields中
        if (in_array($order, $field)) {
            $sql_where = $_GET['where'];
            //判断$_GET['sort']是否存在并且值是否为'desc'
            if ($sort == 'desc') {
                //条件成立,组合order子句   order by 字段 desc
                $sql_order = "order by $order desc";
                //更新$sort为'asc'
                $sort = 'asc';
            } else {
                //条件不成立,组合order子句   order by 字段 asc
                $sql_order = "order by $order asc";
                //更新$sort为'desc'
                $sort = 'desc';
            }
        }

        /* ——————分页—————— */
        //定义每页显示几条数据
        $page_size = 3;
        //获取数据总数
        $sql_res = mysqli_query($link, "select count(*) from emp_info");
        $arr_count = mysqli_fetch_row($sql_res);
        $count = $arr_count[0];
        //最后一页
        $max_page = ceil($count / $page_size);
        //第一页
        $page = isset($_GET['page']) ? $_GET['page'] : 1;
        //上一页
        $prev_page = $page - 1;
        $prev_page = $prev_page < 1 ? 1 : $prev_page;
        //下一页
        $next_page = $page + 1;
        $next_page = $next_page > $max_page ? $max_page : $next_page;

        $Offset = ($page - 1) *  $page_size;
        $sql_limit = "limit $Offset, $page_size";

        //选择表
        $sql = "select * from emp_info    $sql_where  $sql_order $sql_limit";
        $res = mysqli_query($link, $sql);
        //定义员工数组,用以保存员工信息
        $arr = array();
        //遍历结果集,获取每位员工的详细数据
        while ($row = mysqli_fetch_row($res)) {
            $arr[] = $row;
        }
    }
    ?>
    <div class="main">
        <h1>展示员工信息</h1>
        <form action="index.php" method="get">
            <input type="text" name="txt">
            <input type="submit" value="搜索">
        </form>
        <table border="1" style="border-collapse: collapse;">
            <tr>
                <th>ID</th>
                <th>姓名</th>
                <th><a href="index.php?order=e_dept&sort=<?php echo ($order == 'e_dept') ? $sort : 'desc'; ?>&where=<?php echo isset($sql_where) ? $sql_where : ' '; ?>">所属部门</a></th>
                <th>出生日期</th>
                <th><a href="index.php?order=date_of_entry&sort=<?php echo ($order == 'date_of_entry') ? $sort : 'desc'; ?>&where=<?php echo isset($sql_where) ? $sql_where : ' '; ?>">入职日期</a></th>
                <th>相关操作</th>
            </tr>

            <?php foreach ($arr as  $v) : ?>
                <tr>
                    <?php foreach ($v as  $a) : ?>

                        <td><?php echo $a; ?> </td>

                    <?php endforeach; ?>

                    <td>
                        <a href="#" onclick="alert('Error:编辑失败');"><span class="edit"></span>编辑</a>
                        &nbsp; &nbsp; &nbsp;
                        <a href="#" onclick="alert('Error:删除失败');"><span class="del"></span>删除</a>
                    </td>
                </tr>

            <?php endforeach; ?>

        </table>
        <div class="fenye">
            <a href="index.php?page=1">首页</a>
            <a href="index.php?page=<?php echo $prev_page; ?>">上一页</a>
            <a href="index.php?page=<?php echo $next_page; ?>">下一页</a>
            <a href="index.php?page=<?php echo $max_page; ?>">尾页</a>
        </div>

    </div>
</body>

</html>

data-z.sql代码


create database  `zhuang`;
use   `zhuang`;
CREATE TABLE `emp_info` (
  `e_id` int unsigned PRIMARY KEY AUTO_INCREMENT,
  `e_name` varchar(20) NOT NULL,
  `e_dept` varchar(20) NOT NULL,
  `date_of_birth` timestamp NOT NULL,
  `date_of_entry` timestamp NOT NULL
)CHARSET=utf8;

insert into `emp_info` values
(1, '张三', '市场部', '2008-4-3 13:33:00', '2014-9-22 17:53:00'),
(2, '李四', '开发部', '2008-4-3 13:33:00', '2013-10-24 17:53:00'),
(3, '王五', '媒体部', '2008-4-3 13:33:00', '2015-4-21 13:33:00'),
(4, '赵六', '销售部', '2008-4-3 13:33:00', '2015-3-20 17:54:00'),
(5, '小兰', '人事部', '1989-5-4 17:33:00', '2015-4-1 17:35:00'),
(6, '小新', '媒体部', '1993-9-18 17:36:00', '2015-2-28 17:36:00'),
(7, '小白', '市场部', '1991-10-17 17:37:00', '2014-8-16 17:37:00'),
(8, '小智', '运维部', '1987-6-20 17:37:00', '2015-1-10 17:38:00');

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值