php - 将数组与函数中的日期进行匹配

标签 php mysql

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

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

我在数据库中有一个表,用于记录在特定日期对类进行的任何更改,称为异常。

两个数组的结构如下:

$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

在时间表上显示类(class)的函数如下:

 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 连接不起作用。

如果有人能帮助我,我将不胜感激!

最佳答案

在每个 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
}

关于php - 将数组与函数中的日期进行匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22122520/

相关文章:

php - 即使连接数据为空,Mysql如何返回行

c# - Left Join Mysql 查询结果错误

python - Django for循环显示数据库中的所有对象

php - 如何从字段 auto_increment 填充字段外键

mysql - Knex 不返回插入 ID

php - 使用 DOMDocument 加载包含命名空间的 HTML

php - SQL 注入(inject)保护 - 单引号

php - 循环不重复表列中的值

php - php通过其值之一合并两个或多个ArrayIterators

php - 数据库发布到另一个数据库