将数组与函数中的日期匹配

时间:2014-03-02 00:55:37

标签: php mysql

我希望有人可以提供帮助,我有一个健身房的课程预订系统,显示每周的时间表。它可以很好地工作,但是几周之后,课程的某些细节会发生变化,可能是开始时间或讲师。

目前,这些类是从数据库中提取出来的,并根据星期几存储在一个数组中,然后一个函数比较日期并在正确的位置显示它们。

我在数据库中有一个表,用于在某个特定日期对类进行的任何更改。

两个阵列的结构如下:

$timetable  [Monday]  [0]  id,  class_name,  min_attendees, max_attendees, start_time, end_time, day, start_date, studio_name, firstname, surname

$exceptions  [1]  exc_class_id, exc_class_date, exc_name, exc_notes, exc_minattendees, exc_maxattendees, exc_price, exc_starttime, exc_endtime, exc_firstname, exc_surname, exc_studio_name

在时间表上显示类的功能如下:

 function day_switch ($weekday, $timetable, $fulldate) {

switch($weekday)
 {
    case 'Monday':   
      foreach($timetable['Monday'] as $details) {
        $date = date('Y-m-d',strtotime($weekday));   
        echo "<div class='class'>";
        echo "<a href='" . "templates/viewClass.php?id=" . $details['id'] . "&date=" . $fulldate . "'>" . $details['class_name'] . "</a>";
        echo '<br>';
        echo $details['firstname'] . ' ' . $details['surname'];
        echo '<br>';
        echo $details['start_time'] . ' ' . $details['end_time'];
        echo '<br>';
        echo date('Y-m-d',strtotime($weekday)); // Replace this with number booked, need to join bookings onto timetable
        echo "</div>";
        }
    break;

    case 'Tuesday':
      foreach($timetable['Tuesday'] as $details) {
        $date = date('Y-m-d',strtotime($weekday));  
        echo "<div class='class'>";
        echo "<a href='" . "templates/viewClass.php?id=" . $details['id'] . "&date=" . $fulldate . "'>" . $details['class_name'] . "</a>";
        echo '<br>';
        echo $details['firstname'] . ' ' . $details['surname'];
        echo '<br>';
        echo $details['start_time'] . ' ' . $details['end_time'];
        echo '<br>';
        echo date('Y-m-d',strtotime($weekday));
        echo "</div>";
        }
    break;

等...

我需要在其中加入一个查看异常数组并查看类ID是否匹配的检查,其次是当前日期与$ date匹配。

每个日期的例外中只有一条记录,但每个类ID可能有多个记录,因此SQL连接无法正常工作。

如果有人能提供帮助,我会非常感激!

1 个答案:

答案 0 :(得分:1)

在每个foreach循环中调用此函数。此函数调用应该是foreach循环中的第一行

我希望它有所帮助。

function checkException($details, $exceptions){
    foreach($exceptions as $exception){
        if(($exception['exc_class_id'] == $details['id']) && ($exception['exc_class_date'] == $details['start_date']))
            return array(
                'id' => $details['id'],
                'class_name' => $exception['exc_name'],
                'firstname' => $exception['exc_firstname'],
                'lastname' => $exception['exc_lastname'],
                'start_time' => $exception['exc_starttime'],
                'end_time' => $exception['exc_endtime']
                //Add whatever details you want to be changed here...
            );
    }
    return $details;
}

实施例

foreach($timetable['Monday'] as $details){
    $details = checkException($details, $exceptions);
    //Rest of the code here below
}