列出表中包含日期信息的行

时间:2012-07-27 10:28:58

标签: php mysql

我正在为教师管理学生建立一个简单的缺席脚本。

我需要在5个表中填充相同的学生列表。本周每天(星期一至星期五)的一张表。

我目前有以下代码

列出学生SQL查询:

SELECT s.student_id, s.student_firstname, s.student_lastname, 
 a.student_absence_startdate, a.student_absence_enddate, a.student_absence_type
FROM students s 
LEFT JOIN studentabsence a ON a.student_id = s.student_id
WHERE a.student_absence_startdate 
 IS NULL OR 
  '2012-06-20' 
   BETWEEN 
a.student_absence_startdate AND a.student_absence_enddate

此查询选择所有学生并加入另一个表格,该表格是一个包含学生缺席信息的链接表。

我将代码填充5次:

<?php
$data = array();
    while ($row = mysql_fetch_array($list_students)) {
    $data[] = $row;
    }
?>

<table>
  <?php
  foreach($data as $row){
    $class = "";
    if ($row['student_absence_type'] == 2) {$class = " style='background:#f00;'";}
    else if ($row['student_absence_type'] == 3) {$class = " style='background:#8A2BE2;'";}

        echo "<tr><td$class>";
        echo "<a class='ajaxlink' href='#' rel='pages/ajaxAbsence.php?student_id=".$row['student_id']."'>".$row['student_firstname']." ".$row['student_lastname']."</a>";
        echo "</td></tr>\n";
   }
   ?>
</table> <!-- Repeat -->

我的问题是如何管理日期功能。

正如您在我的SQL查询中看到的,我已经硬编码了日期。我需要它来自动选择学生列出的桌子的日期。

例如:

table > Monday (2012-07-23)
    the user John Doe has absence information in this span, based on the above query. Let's change the background of <td>
table > Tuesday (2012-07-24)
    the user John Doe has no absence information in this span. Just list him.
etc...

我不希望你写我的代码。我只是好奇如何思考。我对编程很新。

1 个答案:

答案 0 :(得分:1)

我在你的想法中发现的最明显的错误是你不需要这五个表。你最终会重复和搞乱数据。

你需要不同的表格,而不是我读过的。

<强>学生

ID,姓名等。

<强>类

class_id,name,

教师

Id_teacher,名称等

TeachersInClass (所以你现在可以找哪个老师给出一个特定的课程)

id,id_teacher,id_class

缺勤

id,id_student,id_class,date

这为您提供了一个更强大的数据库,您可以通过多种关联来玩游戏,例如,一年中教师最多缺席......等等。

编辑:忘了回答你的“真实”问题。

您可以使用PHP DateTime类来操纵日期。它易于使用,并且php.net有很多很好的例子可以帮助你开始提升