0

我有这段代码来显示按用户分组的数据库查询:

$report_dates = $users->get_dates($startdate, $enddate);
foreach ($report_dates as $report_date_group) {
    $report_group = $users->get_jobs($userid, $report_date_group['job_date']);
    if ($report_group) {
        foreach ($report_group as $report_group_row) {
            $user_name  = $report_group_row['user_name'] . " " . $report_group_row['user_last'];
            $job_up_time  = '';
            $job_date     = datef($report_group_row['job_date'], "m/d/Y");
            $job_start    = datef($report_group_row['job_start'], "g:i a");
            $job_end      = datef($report_group_row['job_end'], "g:i a");
            $job_time     = decimals($report_group_row['job_time']);
            $job_up_time  = ($job_time < 2) ? 2 : $job_time;
            $job_rate     = $report_group_row['user_rate'];
            $job_total    = $job_time * $job_rate;
            $job_up_total = $job_up_time * $job_rate;
            $user_total_fee += $job_total;
            $user_date_fee += $job_total;
            $user_total_up_fee += $job_up_total;
            $user_date_up_fee += $job_up_total;
            $user_total_time += $job_time;
            $date_total_time += $job_time;
            $date_total_up_time += $job_up_time;
            $user_total_up_time += $job_up_time;
            $rowdata .= "<tr>
                        <td>" . $report_group_row['job_num'] . "</td>
                        <td>" . $job_date . "</td>
                        <td>" . $job_start . "</td>
                        <td>" . $job_end . "</td>
                        <td>" . number_format($job_time, 2, '.', '') . "</td>
                        <td>" . cash($job_total) . "</td>
                        <td>" . ($job_time < 2 ? number_format($job_up_time, 2, '.', '') : number_format($job_up_time, 2, '.', '')) . "</td>
                        <td>" . ($job_time < 2 ? cash($job_up_total) : cash($job_up_total)) . "</td>
                    </tr>";
            $records++;
        }
        $rowdata .= "<tr>
                        <td colspan=5>Date total</td>
                        <td>" . number_format($date_total_time, 2, '.', '') . "</td>
                        <td>" . cash($user_date_fee) . "</td>
                        <td>" . number_format($date_total_up_time, 2, '.', '') . "</td>
                        <td>" . cash($user_date_up_fee) . "</td>
                    </tr>";
        }
    }
?>
    <h4>Job report for <?php echo $user_name; ?></h4>
    <table>
        <thead>
            <tr>
                <th>#</th>
                <th>Job#</th>
                <th>Job Date</th>
                <th>Start</th>
                <th>End</th>
                <th>Time</th>
                <th>Total</th>
                <th>Up-time</th>
                <th>Total Up</th>
            </tr>
        </thead>
        <tbody>
<?php
        echo $rowdata;
?>
        </tbody>
        <tfoot>
            <tr>
                <td colspan=5>Total for <?php echo $user_name; ?></td>
                <td><?php echo number_format($user_total_time, 2, '.', ''); ?></td>
                <td><?php echo cash($user_total_fee); ?></td>
                <td><?php echo number_format($user_total_up_time, 2, '.', ''); ?></td>
                <td><?php echo cash($user_total_up_fee); ?></td>
            </tr>
        </tfoot>
    </table>

被调用的主要函数有:

public function get_dates($start, $end) {
    $query = $this->db->prepare("SELECT DISTINCT job_date 
                                   FROM jobs 
                                  WHERE job_date BETWEEN ? AND ? 
                                    AND entry = 1 
                               ORDER BY job_date ASC");
    $query->bindValue(1, $start);
    $query->bindValue(2, $end);
    try {
        $query->execute();
    } catch (PDOException $e) { die($e->getMessage()); }
    return $query->fetchAll();
}

public function get_jobs($user, $date) {
    $query = $this->db->prepare("SELECT job_num, 
                                        job_date, 
                                        job_start, 
                                        job_end, 
                                        TIMEDIFF(job_end, job_start) AS job_time,
                                        users.first_name             AS user_name, 
                                        users.last_name              AS user_last, 
                                        users.rate                   AS user_rate 
                                   FROM jobs 
                              LEFT JOIN users 
                                     ON jobs.user_id = users.id
                                  WHERE job_date = ? 
                                    AND user_id = ?
                                    AND entry = 1
                               ORDER BY job_date ASC, job_start ASC");
    $query->bindValue(1, $date);
    $query->bindValue(2, $user);
    try {
        $query->execute();
    } catch (PDOException $e) { die($e->getMessage()); }
    return $query->fetchAll();
}

此代码当前按需要工作,并以我需要的方式正确显示数据。

我的问题:有没有更有效的编码方式,这样我就不必进行两个不同的查询来显示我需要的数据?

谢谢!

4

0 回答 0