[英]While loop inside if statement confusion
在過去四天里,這一直是我的不懈努力。 我真的希望能夠解決這個問題,而不必在這里詢問,因為這需要很多代碼,但是恐怕我已經盡力減少這種問題了,就像我在追尾。 我已經盡我所能地消除了很多代碼-可能已經消除了比我應該輕松了解正在發生的事情多得多的代碼,請隨時提出澄清。
內容提要:
我正在從Google日歷中提取日歷供稿,並填充自己的日歷。 我的日歷增加了功能,在這里我有一個“時間軸事件”數據庫,就像發生在常規事件中經過一定天數( DaysFromEvent
)的DaysFromEvent
。
該Cron提取了供稿,並在Google日歷上搜索了所有新事件,並將它們添加到數據庫中。 (這部分似乎正在正常工作)。 cron還會查看ModifiedInGoogle
字段,以查看事件是否已修改,如果已修改,則它將更新數據庫中的事件。 這似乎也可行。 問題是如果修改了主事件,那么我還需要查看數據庫並提取與主事件相關的任何時間軸事件,並根據StartTime + DaysFromEvent
更改其開始時間
我想我幾乎是用這個腳本來做的,但是我似乎在if
語句內部的while
循環中遇到了麻煩。
當我通過腳本運行修改后的事件時,只會回顯到: echo "Event ID: ".$tempEventID. ", Promotional Time Line: ".$tempTimelineID.", Parent Event ID: ".$tempParentEventID."<br>";
echo "Event ID: ".$tempEventID. ", Promotional Time Line: ".$tempTimelineID.", Parent Event ID: ".$tempParentEventID."<br>";
我應該從while循環中得到回聲,但事實並非如此。 對於冗長的單詞,我深表歉意,對於長長的代碼,我也表示歉意。 我是這個新手,請保持溫柔。 :)
我准備放棄並雇用某人為我完成此任務,因為我是音樂家,而不是程序員!
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";
}
線內
$query = "SELECT * FROM events WHERE ParentEventID='.$tempParentEventID.' AND GoogleID IS NULL";
您包括單引號而不是雙引號。 你是說有
$query = "SELECT * FROM events WHERE ParentEventID=".$tempParentEventID." AND GoogleID IS NULL";
或可能沒有點(如果要保留定界符)
$query = "SELECT * FROM events WHERE ParentEventID='$tempParentEventID' AND GoogleID IS NULL";
好吧,跳到我身上的第一件事是while循環:while($ row = mysql_fetch_array($ tempresult,MYSQL_ASSOC))
為了使while循環繼續進行,參數必須為“ true”。 我對php不太熟悉,某些語言接受大於0的任何整數都不為假,但這可能是錯誤的來源。
它說:
while($row = mysql_fetch_array($tempresult, MYSQL_ASSOC)){
不應該是:
while($row == mysql_fetch_array($tempresult, MYSQL_ASSOC)){
如果這不是目的,那么似乎有些可疑,因為您無法從操作中獲得適當的值,因此需要某種查詢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.