sql >> データベース >  >> RDS >> Mysql

MySQLデータベースからの定期的なイベントを含むPHPカレンダー

    誰かが同様のことをしたい場合は、私が作成してテストしたコードを過去5時間以内に投稿します。

    2つのテーブルを3つに変換し、すべての定期的なオプションを3番目のテーブルに移動することにしました。

    'churchcal_events' -ここに含めていないイベント情報の追加フィールドがあります

    +----+-----------+------------+------------+------------+-----------+
    | id | name      | date       | starttime  | endtime    | recurring |
    +----+-----------+------------+------------+------------+-----------+
    | 1  | Event 1   | 2013-04-01 | 10:30:00   | 12:00:00   | 0         |
    | 2  | Event 2   |            | 14:00:00   | 15:00:00   | 1         |
    | 3  | Event 3   |            | 09:00:00   |            | 1         |
    | 4  | Event 4   |            | 19:00:00   | 21:00:00   | 1         |
    +----+-----------+------------+------------+------------+-----------+
    

    'churchcal_assign' -イベントを適切なカレンダーにルーティングします(Webサイト全体にさまざまな部門のカレンダーがあるため)

    +----------+-------------+
    | event_id | calendar_id | 
    +----------+-------------+
    | 1        | 1           |
    | 2        | 1           |
    | 3        | 1           |
    | 4        | 1           |
    +----------+-------------+
    

    'churchcal_recur_events' -各イベントの再発のルールは次のとおりです

    +----------+-----------+------------+----------------+------------+-------------+-----------+
    | event_id | frequency | recur_type | recur_day_num  | recur_day  | recur_start | recur_end |
    +----------+-----------+------------+----------------+------------+-------------+-----------+
    | 2        | 1         | Week       | NULL           | Sunday     | NULL        | NULL      |
    | 3        | 2         | Week       | NULL           | Wednesday  | 2013-04-01  | NULL      |
    | 4        | 2         | Month      | third          | Friday     | 2013-04-01  | NULL      |
    +----------+-----------+------------+----------------+------------+-------------+-----------+
    

    新しいコード(PHP) -これは1日ループです

        //query all events
        $get_events = db_query("SELECT * FROM {churchcal_events ce, churchcal_recur_events cre}
        WHERE (MONTH(ce.date) = :month AND YEAR(ce.date) = :year AND DAY(ce.date) = :day) OR
        (ce.id = cre.event_id AND ce.recurring = :recur AND cre.recur_day = :calendar_day)
        ORDER BY starttime",
        array(
          ':month' => $month,
          ':year' => $year,
          ':day' => $list_day,
          ':recur' => '1',
          ':calendar_day' => date('l', strtotime($month . '/' . $list_day . '/' . $year)),
        ));
    
    
    
        foreach($get_events as $event) {
    
          //see if events belong to this calendar
          $calendar_assign = db_query("SELECT * FROM {churchcal_assign} WHERE event_id = :event_id AND calendar_id = :cal_id",
          array(
            ':event_id' => $event->id,
            ':cal_id' => $cal_id,
          ));
    
          if($calendar_assign->rowCount() > 0) {
    
            //one-time events
            if(($event->recurring != '1') && ($single_event_id != $event->id)) {
              $calendar .= calendar_event($event->id, $event->name, $event->starttime);
              $single_event_id = $event->id;
              //$calendar .= $single_event_id;
            }
    
              //monthly recurring events
              if(($event->recur_type == 'Month') && ($event->recurring == '1')
              //day is on or before end date
              && (($event->recur_end >= date('Y-m-d', strtotime($month . '/' . $list_day . '/' . $year))) || ($event->recur_end == '0000-00-00') || ($event->recur_end == NULL))
              //day is on or after start date
              && (($event->recur_start <= date('Y-m-d', strtotime($month . '/' . $list_day . '/' . $year))) || ($event->recur_start == '0000-00-00') || ($event->recur_start == NULL))
              //day is equal to date of recurrence, i.e. first friday, third monday, etc
              && ($year . date('m', strtotime($year . '/' . $month . '/' . $list_day)) . $list_day == date('Ymj', strtotime($event->recur_day_num . ' ' . $event->recur_day . ' of ' . date('F', strtotime($month . '/' . $list_day . '/' . $year)) . ' ' . $year)))
              //day is in agreement with month frequency, i.e. every month, every other, etc. from start date
              && ($month % $event->frequency == date('m', strtotime($event->recur_start)) % $event->frequency)) {
                $calendar .= calendar_event($event->id, $event->name, $event->starttime);
              }
    
    
              //weekly recurring events
              if(($event->recur_type == 'Week') && ($event->recurring == '1')
              //day is on or before end date
              && (($event->recur_end >= date('Y-m-d', strtotime($month . '/' . $list_day . '/' . $year))) || ($event->recur_end == '0000-00-00') || ($event->recur_end == NULL))
              //day is on or after start date
              && (($event->recur_start <= date('Y-m-d', strtotime($month . '/' . $list_day . '/' . $year))) || ($event->recur_start == '0000-00-00') || ($event->recur_start == NULL))
              //day is in agreement with week frequency, i.e. every week, every other, etc. from start date
              && (date('W', strtotime($month . '/' . $list_day . '/' . $year)) % $event->frequency == date('W', strtotime($event->recur_start)) % $event->frequency)) {
    
                $calendar .= calendar_event($event->id, $event->name, $event->starttime);
    
              }
    
          }
    
        }
    

    私のようにDrupalモジュールでこれを作成していない場合は、「db_query()」をPHPのデフォルトの「mysql_query()」のような独自のデータベースクエリ関数に置き換えることができます。




    1. SQLServerのAlwaysOn機能の使用方法

    2. Exists1またはExists*を使用したサブクエリ

    3. c#を使用してSQL Serverテーブルの変更を監視するにはどうすればよいですか?

    4. Windowsにmysql-python(新しいバージョン)をインストールできません