Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL updating recurring events
    primarykey
    data
    text
    <p>I've been struggling with this for a while and would love some fresh ideas. </p> <p>I have an events calendar that has recurring events. I've run into some problems when I try to update recurring events. </p> <p>I have a simple form where I ask the user for a day of the week and a start date. </p> <p>I want to get the next day of the week after the start date (no problem here) and update the exiting event that week to the new starting day(still no problem). Then I want to increment all the other events after that to be 7 days after the event before it. (&lt;--problem)</p> <pre><code>&lt;?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-&gt;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-&gt;bindParam(':start', $start); $stmt-&gt;bindParam(':end', $end); $stmt-&gt;bindParam(':parent_id', $parent_id); $stmt-&gt;execute(); } </code></pre> <p>Now I need to loop through every event (with the same parent_id) after this one and increment it by 7 days (or whatever my repeat_freq is). I've tried using </p> <pre><code>UPDATE events SET start = :start + INTERVAL 7 DAY, end = :end + INTERVAL 7 DAY </code></pre> <p>but this just updates all the events to 7 days after the new start date and not from 7 days from the last event. If I could put this in a loop without having to reference the specific event id and just go sequentially, that'd be great. I'm open to any solutions though. Thanks for reading.</p> <p><strong>UPDATE FOR SCHEMA</strong></p> <p>My parent_events table has</p> <pre><code>parent_id (auto increment) | start_date | repeats | repeat_freq | </code></pre> <p>My events table has the following </p> <pre><code>event_id (auto increment) | parent_id | start | end | </code></pre> <p>When a new recurring event is created I insert a row into the parent_events table. I get the new parent_id using lastInsertId() and use it when I insert all of the recurring instances (of that parent event) into the events table. Basically, the events table holds all of the instances of a specific (parent) event. </p> <p><strong>UPDATE FOR WORKING CODE</strong></p> <pre><code>$stmt = $dbh-&gt;prepare(" SELECT event_id FROM events WHERE parent_id = :parent_id AND WEEK(start) &gt;=WEEK(:start)"); $stmt-&gt;bindParam(':start', $start_date); $stmt-&gt;bindParam(':parent_id', $parent_id); $stmt-&gt;execute(); $updateArray = array(); $last_class = 0; while ($row = $stmt-&gt;fetch(PDO::FETCH_ASSOC)) { if ($last_event != $row['event_id']) { $last_event = $row['event_id']; $updateArray[$last_event] = array('event_id' =&gt; $row['event_id']); } } foreach($updateArray as $update){ $event_id = $update['event_id']; $stmt = $dbh-&gt;prepare(" UPDATE events SET title = start = :start, end=:end WHERE event_id = :event_id"); $stmt-&gt;bindParam(':start', $start); $stmt-&gt;bindParam(':end', $end); $stmt-&gt;bindParam(':event_id', $event_id); $stmt-&gt;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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload