PHP 作业3.2 排序

导读:本篇文章讲解 PHP 作业3.2 排序,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

要求


所属部门和入职日期实现升降排序

效果图


在这里插入图片描述

index.php代码


<!DOCTYPE html>
<html>

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

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

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

        a:hover {
            color: deeppink;
        }

        .edit {
            color: #0e84b5;
        }

        .del {
            color: #9A0000;
        }

        table {
            border-collapse: collapse;
            margin: 0 auto;
        }

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

        tr {
            text-align: center;
        }

        td {
            padding: 20px 40px;
        }
    </style>
</head>

<body>

    <?php
    //连接数据库
    $link = mysqli_connect("localhost", "root", "root");
    if (!$link) {
        echo die("数据库链接失败") . mysqli_error();
    } else {
        //选择数据库
        mysqli_query($link, "use zhuang");

        // 初始化排序语句,用来组合排序的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)) {
            //判断$_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';
            }
        }


        //选择表
        $sql = "select * from emp_info   $sql_order";
        $res = mysqli_query($link, $sql);
        //定义员工数组,用以保存员工信息
        $arr = array();
        //遍历结果集,获取每位员工的详细数据
        while ($row = mysqli_fetch_row($res)) {
            $arr[] = $row;
        }
    }
    ?>

    <h1>展示员工信息</h1>

    <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'; ?>">所属部门</a></th>
            <th>出生日期</th>
            <th><a href="index.php?order=date_of_entry&sort=<?php echo ($order == 'date_of_entry') ? $sort : 'desc'; ?>">入职日期</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>

</body>

</html>

创建data.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');

增加emp_info.php代码



INSERT INTO `emp_info` VALUES
   (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');

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/83738.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!