php - while 循环内部 if 语句困惑

标签 php mysql

在过去的四天里,这对我来说一直是一场持续的斗争。我真的希望能够解决这个问题,而不必在这里问,因为它有很多代码,但恐怕我已经尝试过并试图减少问题,就像我在追我的尾部一样。我已经删除了尽可能多的代码 - 我删除的代码可能超出了我应该轻松了解正在发生的情况的范围,因此请随时要求澄清。

概要:

我正在从 Google 日历中提取日历源并填充我自己的日历。我的日历添加了功能,其中我有一个“时间线事件”数据库,这些事件类似于常规事件发生一定天数 ( DaysFromEvent ) 后发生的待办事项。

提要被这个 cron 拉入,并在谷歌日历上搜索任何新事件并将它们添加到数据库中。 (这部分似乎正常工作)。 cron 还会查看 ModifiedInGoogle字段以查看事件是否已被修改,如果是,则更新数据库中的事件。这似乎也有效。关键在于,如果主事件被修改,那么我还需要在数据库中查找并提取与主事件关联的任何时间线事件,并根据StartTime + DaysFromEvent更改其开始时间。

我想我几乎有这个脚本可以做到这一点,但我的 while 似乎遇到了问题if 内部的循环声明。

当我通过脚本运行修改后的事件时,我只会得到回显:echo "Event ID: ".$tempEventID. ", Promotional Time Line: ".$tempTimelineID.", Parent Event ID: ".$tempParentEventID."<br>";

我应该从 while 循环中得到 am echo,但我没有。我对冗长的文字表示歉意,更对后面的长代码表示歉意。我是这方面的新手,所以请温柔一点。 :)

我准备放弃并雇人来帮我完成这个工作,因为我是音乐家而不是程序员!

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";

关于php - while 循环内部 if 语句困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6476037/

相关文章:

javascript - 如何在正则表达式中检测带标点符号的汉字?

python - 使用大型列表优化 Django 查询集调用?

php - 如何编写选择查询以在 php MySQL 中使用外键和连接从 5 个表中获取数据?

mysql - 如果数据存在则设置一个值的 SQL 查询

mysql - 如何搜索带有替代字符的字符串?

php - foreach中的foreach条件过滤

javascript - 将数组从 HTML 表单传递到 PHP,然后传递到 WordPress

php - 将图像解析为数据[任何语言]

php - Foreach php变量值创建while循环

mysql - 指定的字符串不符合电子邮件地址所需的格式。有什么解决办法吗?