PHP not exiting loop -
im trying return data php based on time. ie if current time greater publish time post data should sent user else should told that's scheduled.. problem if condition met still executes loop , gives 'success' result.any idea how overcome this?
$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 not exist'; } echo json_encode($response);
so managed solve using flag variable
$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; }
Comments
Post a Comment