MySQL更新重复发生的事件

时间:2012-09-04 06:07:05

标签: php mysql recurring-events

我一直在努力解决这个问题,并且会喜欢一些新鲜的想法。

我有一个包含重复活动的活动日历。当我尝试更新重复发生的事件时,我遇到了一些问题。

我有一个简单的表单,我要求用户提供一周中的某一天和开始日期。

我希望在开始日期之后的一周的第二天(这里没有问题)并将当周的退出事件更新到新的开始日(仍然没有问题)。然后我想在之后的事件发生后7天将所有其他事件增加。 (小于 - 问题)

<?php
session_start();
require_once '../../../config/db-config.php';
    $weekday = $_POST['weekday'];        // day of the week the event should take place on
    $start_date = $_POST['start-date'];      //new starting date of the event 
       if (date('N', strtotime($start_date)) == $weekday)
        {
        $start_date = $_POST['start-date'];
        }
       else
        {
         $start_date = date('Y-m-d', strtotime("next $weekday", strtotime($start_date)));   //gets the first day of the week after the starting date. First event should start on this day.
        }  
    $start_time = $_POST['start-time'];
    $end_time = $_POST['end-time'];

    $start = $start_date . " " . $start_time;  //'yyyy-dd-mm hh:mm:ii' format
    $end = $start_date . " " . $end_time;

    $repeats = $_POST['repeats'];             // 1 for repeats 0 for no repeats
    $repeat_freq = $_POST['repeat-freq'];    //7 for 7 days, 14 for 14 days etc
    $parent_id = $_POST['parent-id'];        //common parent_id to the event group

    $stmt = $dbh->prepare("
                        UPDATE events
                        SET  start = :start, end = :end
                        WHERE parent_id = :parent_id AND WEEK(start) =WEEK(:start)");     //This updates the existing event of that week to the new starting date

        $stmt->bindParam(':start', $start);
        $stmt->bindParam(':end', $end);
        $stmt->bindParam(':parent_id', $parent_id);
        $stmt->execute();

        }

现在我需要在这个事件之后遍历每个事件(使用相同的parent_id)并将其递增7天(或者无论我的repeat_freq是什么)。我尝试过使用

UPDATE events
SET  start = :start + INTERVAL 7 DAY, end = :end + INTERVAL 7 DAY

但这只会将所有事件更新为新开始日期后7天,而不是最后一次事件后7天。如果我可以把它放在一个循环中而不必引用特定的事件id而只是按顺序进行,那就太好了。我对任何解决方案持开放态度。谢谢你的阅读。

更新SCHEMA

我的parent_events表有

parent_id (auto increment) | start_date | repeats | repeat_freq |

我的事件表包含以下内容

event_id (auto increment) | parent_id | start | end |

创建新的周期性事件时,我在parent_events表中插入一行。我使用lastInsertId()获取新的parent_id,并在将所有重复实例(该父事件的事件)插入事件表时使用它。基本上,events表包含特定(父)事件的所有实例。

工作代码更新

$stmt = $dbh->prepare("
            SELECT event_id
            FROM events
            WHERE parent_id = :parent_id AND WEEK(start) >=WEEK(:start)");


        $stmt->bindParam(':start', $start_date);
        $stmt->bindParam(':parent_id', $parent_id);
        $stmt->execute();
        $updateArray = array();
        $last_class = 0;
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            if ($last_event != $row['event_id']) {
                $last_event  = $row['event_id'];
                    $updateArray[$last_event] = array('event_id' => $row['event_id']);
            }
        }


        foreach($updateArray as $update){
            $event_id = $update['event_id'];
            $stmt = $dbh->prepare("
                            UPDATE events
                            SET title = start = :start, end=:end
                            WHERE event_id = :event_id");


            $stmt->bindParam(':start', $start);
            $stmt->bindParam(':end', $end);
            $stmt->bindParam(':event_id', $event_id);
            $stmt->execute();

            $new_start = strtotime($start . '+' . $repeat_freq . 'DAYS'); 
            $new_end = strtotime($end . '+' . $repeat_freq . 'DAYS'); 
            $start = gmdate("Y-m-d H:i", $new_start);
            $end = gmdate("Y-m-d H:i", $new_end);
        }

1 个答案:

答案 0 :(得分:1)

我认为最简单的方法是通过PHP循环SET事件。

我将在下面写下重要的代码:

$sql='SELECT event_id AS id from events WHERE parent_id=:parent_id AND start=:start

运行此查询将获得一个包含所有需要更新的ID的数组

 $mysqli = new mysqli("localhost", "my_user", "my_password", "world");
 if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
 }
 $row_date=array();
 $row_date[0]=$start  //get start date
 foreach ($rows as $row)

  $sql_date="SELECT TIMESTAMPADD(week,1,$row_date[0])"; //increment start date by 1 week
  $date =  $mysqli->query($sql_date);
  $row_date = $result->fetch_array(MYSQLI_NUM); //Set the start date as Start date + 1 week to reflect in the next loop.

  $sql_update="UPDATE events SET start=$row_date[0] WHERE event_id=$row['id']";
  $mysqli->query($sql_update);
 end foreach

代码我需要一些调整,但我认为它有助于支出这个想法。