从数据库中获取数据并按日期排序,然后按HTML表中的用户排序

时间:2019-08-16 04:48:55

标签: php

我有一些记录要按日期排序,然后按服务器端或客户端上的用户排序。

这是我的代码。

    if (isset($_POST['work_report']) && $_POST['work_report'] == 'SAVE') {
        $from_date = $_POST['from_date'];
        $to_date   = $_POST['to_date'];
        $users      = isset($_POST['users']) ? $_POST['users'] : array();
        if (empty($users)) {
            header("location: work_dairy_report");
        } else {
            $reports = array();
            foreach ($users as $key => $user) {
                if ($user == 0) {
                    $work_report = "SELECT * FROM " . WORK_REPORT_TABLE . " WHERE work_date BETWEEN '$from_date' AND '$to_date'";
                } else {
                    $work_report = "SELECT * FROM " . WORK_REPORT_TABLE . " WHERE work_date BETWEEN '$from_date' AND '$to_date' AND staff_id = $user";
                }
                $result_work_report = $db->execute_query($work_report);
                while ($report = mysqli_fetch_assoc($result_work_report)) {
                    $reports[] = $report;
                }
            }
        }
    }

任何解决方案表示赞赏!

1 个答案:

答案 0 :(得分:0)

最简单的选择似乎是将ORDER BY work_date, staff_id添加到SELECT语句中,如下所示:

"SELECT * FROM " . WORK_REPORT_TABLE . " WHERE work_date BETWEEN '$from_date' AND '$to_date' ORDER BY work_date";

"SELECT * FROM " . WORK_REPORT_TABLE . " WHERE work_date BETWEEN '$from_date' AND '$to_date' AND staff_id = $user ORDER BY work_date, staff_id";

有关ORDER BY使用情况here的更多信息