简体   繁体   English

在表格中显示数据的最佳方法

[英]The best way to display data in a table

Getting a little bit outside my comfort zone and I'm hoping someone can explain the best way of achieving what I need. 稍微超出我的舒适范围,我希望有人能够解释实现我所需要的最佳方法。

This is a very simple staff attendance system. 这是一个非常简单的员工出勤系统。

The staff table includes the name of the staff member. 人员表包含该人员的姓名。

The attendance table includes the date and type of shift they worked (full/half/day-and-a-half) on that day. 出勤表包括他们当天工作的日期和班次类型(全天/半天/半天)。

What I now want to do is create a report as a table that shows all the staff's attendance between two dates (a pay period). 我现在想做的是创建一个报告,作为一个表,显示两个日期(工资期)之间所有员工的出勤情况。

I'm thinking the first column should be the staff names, and then subsequent columns would be headed with each date between the two dates (start_date and end_date) and I then want to populate the rest of the table with the shift that a member of staff worked on a particular date. 我想第一列应该是职员名称,然后后面的列将以两个日期(start_date和end_date)之间的每个日期为标题,然后我想用该成员的班次填充表的其余部分工作人员在特定的日期工作。

So, it would look something like this: 因此,它看起来像这样:

     Name     |  2013-05-26  |   2013-05-27   |   etc
     -------------------- ---------------------------
     John     |     full     |      half      |
     Mary     |     off      |      full      |
     Peter    |     half     |      full      |

The following query gives me all the data: 以下查询提供了所有数据:

SELECT a.id, first_name, last_name, attDate, staff_id, att, late
FROM staff as a LEFT OUTER JOIN staff_attendance AS b
  ON a.id = b.staff_id 
    AND b.`attDate` >= '2013-05-26' 
    AND  b.`attDate` <= '2013-06-27' 
ORDER BY last_name, attDate;

Which would basically be: 基本上是:

    id  |   first_name   | last_name |  attDate    | staff_id | att   |  late
    ------------------------------------------------------------------------
    1   |     John       |    smith  | 2013-05-26  |     1    | full  |    0
    1   |     John       |    smith  | 2013-05-27  |     1    | half  |    0
    2   |     Mary       |    Jones  | 2013-05-26  |     2    | off   |    0
    2   |     Mary       |    Jones  | 2013-05-27  |     2    | full  |    0
    3   |     Peter      |    Doe    | 2013-05-26  |     3    | half  |    0
    3   |     Peter      |    Doe    | 2013-05-26  |     3    | full  |    0

So as you can see, I have all the data, I just have no idea how to get it into the table. 如您所见,我拥有所有数据,但我不知道如何将其放入表中。

Any thoughts or suggestions very welcome. 任何想法或建议都非常欢迎。

Many thanks 非常感谢

Graeme 格莱美

Following advice from dev-null-dweller, I have the following code: 根据dev-null-dweller的建议,我有以下代码:

    $dbh = new PDO("mysql:host=$hostname;dbname=web14-fresh", $username, $password);
    $sql = "SELECT a.id, first_name, last_name, attDate, staff_id, att, late FROM staff as a LEFT OUTER JOIN staff_attendance AS b ON a.id = b.staff_id AND b.`attDate` >= '" .                   $startdate. "' AND  b.`attDate` <= '". $enddate . "' ORDER BY last_name, attDate;";
    $stmt = $dbh->query($sql);

    $dates = array();
    $users = array();

    while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
        $dates[] = $row['attDate'];
        if (!isset($users[$row['id']])) {
            $users[$row['id']] = array(
                'first_name' => $row['first_name'], 
                'last_name' => $row['last_name'],
                'attendance' => array()
            );
        }
        $users[$row['id']]['attendance'][$row['attDate']] = $row['att'];
    }

    $dates = array_unique($dates);
    sort($dates);

    // header
    echo '<table border=1><tr>';
    echo '<td>Name</td>';
    foreach($dates as $d){
        echo '<td>'.$d.'</td>';
    }
    echo '</tr>';

    //data
    foreach($users as $u) {
        echo '<tr><td>'.$u['first_name'].'</td>';
        foreach($dates as $d) {
            if (isset($u['attendance'][$d])) {
                echo '<td>'.$u['attendance'][$d].'</td>';
            } else {
                echo '<td>?</td>';
            }
        }
        //end row
        echo '</tr>';
    }
    echo '</table>';

This doesn't seem to be populating all the dates across the top and can't see why. 这似乎并没有填充顶部的所有日期,也看不出原因。 This is what I am getting. 这就是我得到的。

    Name        2013-06-26
    Katy    ?   full
    Lauren  ?   full
    Laura   ?   full
    Louise  ?   holiday
    Jade    ?   off

Not quite sure the date array is not populating correctly :-/ 不太确定日期数组是否正确填充:-/

Piece of code to illustrate my comment : 一段代码来说明我的评论

$dates = array();
$users = array();

while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
    $dates[] = $row['attDate'];
    if (!isset($users[$row['id']])) {
        $users[$row['id']] = array(
            'first_name' => $row['first_name'], 
            'last_name' => $row['last_name'],
            'attendance' => array()
        );
    }
    $users[$row['id']]['attendance'][$row['attDate']] = $row['att'];
}

$dates = array_unique($dates);
sort($dates);

// header
//echo 'name';
foreach($dates as $d){
    echo $d;
}

//data
foreach($users as $u) {
    //echo $u['first_name'];
    foreach($dates as $d) {
        if (isset($u['attendance'][$d])) {
            echo $u['attendance'][$d];
        } else {
            // for missing records?
        }
    }
    //end row
}

Here is the basic output loop I use for outputting SQL results. 这是我用于输出SQL结果的基本输出循环。 It can be extended from here with conditional formatting based on the $key value or what have you. 可以根据$key值或您所拥有的内容使用条件格式从此处扩展它。

You should also get friendly with the printf() and sprintf() functions, as they are super useful. 您还应该对printf()sprintf()函数友好,因为它们非常有用。

$rs; // assuming this is an indexed array of rows returned from your DB
// ie: $rs[0]['id']

$output = '<table>';

//print header row by iterating through the first result row and printing the keys
$output .= '<tr>';
foreach($rs[0] as $key => $value) {
  $output .= sprintf('<td>%s</td>', $key);
}
$output .= '</tr>';

//now the data rows
foreach($rs as $row) {
  $output .= '<tr>';
  foreach($row as $key => $value) {
    $output .= sprintf('<td>%s</td>', $value);
  }
  $output .= '</tr>';
}

$output .= '</table>';
echo $output;

Briefly: First query only dates to create table header. 简要地说:第一个查询仅日期以创建表头。 Next use your query to get data to create all rows in html table. 接下来使用查询获取数据以创建html表中的所有行。 And you have to remember first_name from previous result row. 而且您必须记住上一个结果行中的first_name If first_name is changed you start new row in html table. 如果first_name更改,则在html表中开始新行。

pseudocode (not complete): 伪代码(不完整):

$results = query("SELECT ... only dates");

// ... some html ... opening header row

foreach($results as $date) {
    echo "<td>", $date, "</td>"; 
}

$results = query("SELECT ... your query");
$last__first_name = "";

// ... some html ... closing header row and opening data rows

foreach($results as $row) {

    if( $row->first_name != $last__first_name ) {
        $last__first_name = $row->first_name;
        echo "</tr><tr>"; // create new row in html table
        echo "<td>", $row->first_name, "</td>";
    }

    echo "<td>", $row->att, "</td>"; // next column 
}

// ... some html ... closing table

You can echo the results in a HTML table. 您可以在HTML表中回显结果。 First, you create the headings: 首先,创建标题:

echo '<table>';
echo '<tr><td>Column 1</td><td>Column 2</td><td>Column 3</td>......</tr>';

Then, you execute the SQL query (the SELECT): 然后,您执行SQL查询(SELECT):

$result = mysql_query("SELECT * FROM table ... ...");

After this, you can iterate through the results, with "while": 之后,您可以使用“ while”遍历结果:

while ($row = mysql_fetch_array($result ) ) {
echo '<tr><td>'.$row['name_of_column_1_in_db'].'</td><td>'.$row['name_of_column_2_in_db'].'</td> .......... </tr>';
}

Then, close the table tag: 然后,关闭表格标签:

echo '</table>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM