PHP没有退出循环

时间:2015-10-15 05:00:35

标签: php loops break

我试图根据时间从php返回一些数据。即如果当前时间大于发布时间,那么只有帖子数据应该发送给用户,否则他应该被告知其预定...问题是即使条件满足它仍然执行循环,并给我的成功' result.any想法如何克服这个?

$sql = "Select posts.post_title,posts.author_name,posts.publish_date,posts.post_content,comments.name,comments.comment,comments.time_posted "
        . "from posts left join comments "
        . "on posts.id=comments.post_id "
        . "where posts.id=$data->id "
        . "LIMIT 5";
    $result = mysql_query($sql) or trigger_error(mysql_error() . $sql);
    $count = mysql_num_rows($result);
    $index = 0;
    if ($count >= 1) {
        $temp = array();
        while ($row = mysql_fetch_assoc($result)) {

            if (strtotime($data->now) > strtotime($row['publish_date'])) {
                if ($index == 0) {
                    $results[$index]['post_title'] = $row['post_title'];
                    $results[$index]['author_name'] = $row['author_name'];
                    $results[$index]['publish_date'] = $row['publish_date'];
                    $results[$index]['post_content'] = $row['post_content'];

                    $temp[$index]['name'] = $row['name'];
                    $temp[$index]['comment'] = $row['comment'];
                    $temp[$index]['time_posted'] = $row['time_posted'];
                } else {
                    $temp[$index]['name'] = $row['name'];
                    $temp[$index]['comment'] = $row['comment'];
                    $temp[$index]['time_posted'] = $row['time_posted'];
                }
                $index++;
            } else {
                $response['status'] = 'Scheduled';
                $response['message'] = 'Data present';
                break;
            }


        }
        $results[0]['comments'] = $temp;
        $response['status'] = 'Success';
        $response['message'] = 'Data present';
        $response['results'] = $results;

    } else {
        $response['status'] = '404';
        $response['message'] = 'Post does not exist';
    }
    echo json_encode($response);

2 个答案:

答案 0 :(得分:0)

所以我设法通过使用标志变量

来解决它
$flag = true;
    if ($count >= 1) {
        $temp = array();
        while ($row = mysql_fetch_assoc($result)) {

            if ((strtotime($data->now) > strtotime($row['publish_date'])) == true) {
                if ($index == 0) {
                    $results[$index]['post_title'] = $row['post_title'];
                    $results[$index]['author_name'] = $row['author_name'];
                    $results[$index]['publish_date'] = $row['publish_date'];
                    $results[$index]['post_content'] = $row['post_content'];

                    $temp[$index]['name'] = $row['name'];
                    $temp[$index]['comment'] = $row['comment'];
                    $temp[$index]['time_posted'] = $row['time_posted'];
                } else {

                    $temp[$index]['name'] = $row['name'];
                    $temp[$index]['comment'] = $row['comment'];
                    $temp[$index]['time_posted'] = $row['time_posted'];
                }
                $index++;
            } else {
                $flag = false;
            }
        }
        if ($flag == false) {
            $response['status'] = 'Scheduled';
            $response['message'] = 'Data present';
        } else {
            $results[0]['comments'] = $temp;
            $response['status'] = 'Success';
            $response['message'] = 'Data present';
            $response['results'] = $results;
        }

答案 1 :(得分:0)

无论您的状况是否得到满足,您始终将状态初始化为成功($ response [' status'] =' Success&#39 ;;)。 您需要将此行移动到代码块,该代码块在满足条件时执行。