This has been an ongoing struggle for me for the last four days. I was really hoping to work this out enough to not have to ask here, because it's a lot of code, but I'm afraid I have tried and tried to reduce the problem down, and it's just like I'm chasing my tail. I have eliminated as much code as I think I can-I may have eliminated more than I should have to easily see what is going on, so feel free to ask for clarification.
The synopses:
I am pull开发者_如何学Cing a calendar feed from Google calendar and populating my own calendar. My calendar has added functionality where I have a database of "timeline events" that are like to-do items that happen a certain number of days (DaysFromEvent
) from the regular event.
The feed gets pulled in by this cron and searches for any new events on the google calendar and adds them to the database. (This part seems to be working as it should). The cron also looks at the ModifiedInGoogle
field to see if the event has been modified and if so it updates the event in the database. That also seems to be working. The crux is that if the main event is modified, then I also need to look in the database and pull any timeline event that is associated with the main event and change its start time based on StartTime + DaysFromEvent
I think I pretty much have this script doing that, but I seem to be having trouble with my while
loop that is inside of the if
statement.
When I run a modified event through the script, I only get echoed up to the point of: echo "Event ID: ".$tempEventID. ", Promotional Time Line: ".$tempTimelineID.", Parent Event ID: ".$tempParentEventID."<br>";
I should be getting am echo from the while loop, but I'm not. My apologies for the long words, and even more apologies for the long code to follow. I'm a newbie at this, so please be gentle. :)
I'm about ready to give up and hire someone to finish this for me because I'm a musician not a programmer!
if((int)$feed->totalResults>0) { //checking if at least one event is there in this date range
foreach ($feed as $event) { //iterating through all events and pulling all the info to assign the variables below.
$UserID= "42";
$eventDatabase = "events";
//Setting startDate and startTime and endDate and endTime
$StartDate = $event->when[0]->startTime;
$date = strtotime($StartDate);
$StartDate = date('Y-m-d H:i:s',$date);
$StartArray = explode(' ', $StartDate);
$StartTime = $StartArray[1];
$EndDate = $event->when[0]->endTime;
$date = strtotime($EndDate);
$EndDate = date('Y-m-d H:i:s',$date);
$EndArray = explode(' ', $EndDate);
$EndTime = $EndArray[1];
$ModifiedInGoogle = $event->updated->text;
$GoogleID = $event->id;
$EventName = stripslashes($event->title);
$PromotionalTimeLine = "0";
$ParentEventID = "NULL";
$DaysFromEvent = "";
//We are seeing if the Event is already in the database by looking for the googleID
$query = "SELECT * FROM ".$eventDatabase. " WHERE GoogleID='$GoogleID'";
$result = mysql_query($query);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
//This loop UPDATES events if ModifiedInGoogle string is different than what is already in the database
if($ModifiedInGoogle != $row['ModifiedInGoogle']){
//Variables for modifying the timeline start time
$ModifiedEventStartTime = $row['StartTime'];
$tempParentEventID = $tempEventID = $row['EventID'];
$tempTimelineID = $row['PromotionalTimeLine'];
//THIS ECHOS AS I EXPECT IT SHOULD
echo "Event ID: ".$tempEventID. ", Promotional Time Line: ".$tempTimelineID.", Parent Event ID: ".$tempParentEventID."<br>";
//Updates main event when modified in google:
mysql_query("UPDATE ".$eventDatabase." SET
EventName = '$EventName',
StartDate = '$StartDate',
StartTime = '$StartTime',
EndTime = '$EndTime',
EndDate = '$EndDate',
Description = '$Description',
AllDay = '$AllDay',
CreatedOn = '$CreatedOn',
GoogleID = '$GoogleID',
ModifiedInGoogle = '$ModifiedInGoogle'
WHERE GoogleID='$GoogleID' ");
//Query to select the timeline events that have the same ParentEvenID-this is where everything seems to start falling apart...
$query = "SELECT * FROM events WHERE ParentEventID='.$tempParentEventID.' AND GoogleID IS NULL";
$tempresult = mysql_query($query);
while($row = mysql_fetch_array($tempresult, MYSQL_ASSOC)){
$tempEventID = $row['EventID'];
$tempDaysFromEvent = $row['DaysFromEvent'];
$tempStartDate = $row['StartDate'];
//THIS LINE IS NOT ECHOING
echo "EventID: ".$tempEventID.", Start Date: ".$tempStartDate.", Days From Event: ".$tempDaysFromEvent.", Parent Event ID: ".$row['ParentEventID']."<br>";
//IF STARTDATE IS DIFFERENT FROM HOW IT USED TO BE, UPDATE IT.
list($year, $month, $day) = explode("-", $tempStartDate);
$tempStartDate = $tempEndDate = date("Y-m-d", mktime (0,0,0,$month,$day+$tempDaysFromEvent,$year));
echo "TempStart Date:".$tempStartDate."<br>";
//Query to update the startdate of the events
mysql_query("UPDATE".$eventDatabase." SET
StartDate = '$tempStartDate'
WHERE EventID = $tempEventID
");
} //Closes While loop
} //This closes the update if modified if statement
//This loop adds NEW EVENTS
if (!mysql_num_rows($result)) {
//Insert NEW EVENTS
}
} //ends main event loop
} else {
echo "No event found";
}
Within the line
$query = "SELECT * FROM events WHERE ParentEventID='.$tempParentEventID.' AND GoogleID IS NULL";
You have included single quotes instead of doubles. Did you mean to have
$query = "SELECT * FROM events WHERE ParentEventID=".$tempParentEventID." AND GoogleID IS NULL";
or maybe without the dots (if you want to keep the delimiters)
$query = "SELECT * FROM events WHERE ParentEventID='$tempParentEventID' AND GoogleID IS NULL";
Well the first thing that jumps out at me is the while loop: while($row = mysql_fetch_array($tempresult, MYSQL_ASSOC))
in order for the while loop to continue the argument must be "true". I'm not too familiar with php and some language accept any integer larger than 0 to not be false, but that may be a source of error.
It says:
while($row = mysql_fetch_array($tempresult, MYSQL_ASSOC)){
Shouldn't it be:
while($row == mysql_fetch_array($tempresult, MYSQL_ASSOC)){
If that's not the intent, then there seems to be something fishy with that, because you won't get a proper value from an action, you'll need a query of some kind.
精彩评论