我实际上正在使用php和mysql在社交媒体网站上工作,当我创建无限滚动新闻提要时,我在使用ajax创建新闻提要时遇到了麻烦,一切正常,但新闻提要永无止境(我只有在tabe中有12个帖子),它像循环一样反复显示所有内容。
index_page.php
with open('data.csv') as file:
start_urls = [line.strip() for line in file]
post.php(class)
<div class="post_area"></div>
<img id="loading" src="assets/images/icons/loading.gif">
</div>
<script>
var userloggedin = '<?php echo $userloggedin; ?>';
$(document).ready(function(){
$('#loading').show();
//original ajax request for loading first posts
$.ajax({
url: "includes/handlers/ajax_load_posts.php",
type: "POST",
data: "page=1&userloggedin=" + userloggedin,
cache:false,
success: function(data){
$('#loading').hide();
$('.post_area').html(data);
}
});
$(window).scroll(function(){
var height = $('.post_area').height(); //div containing posts
var scroll_top = $(this).scrollTop();
var page = $('.post_area').find('.nextPage').val();
var noMorePosts = $('.post_area').find('.noMorePosts').val();
if((document.body.scrollHeight = document.body.scrollTop + window.innerHeight) && noMorePosts == 'false'){
$('#loading').show();
var ajaxReq = $.ajax({
url: "includes/handlers/ajax_load_posts.php",
type: "POST",
data: "page=1&userloggedin=" + userloggedin,
cache:false,
success: function(response){
$('.post_area').find('.nextPage').remove(); //removes current .nextpage
$('.post_area').find('.noMorePosts').remove(); //removes current .noMorePosts
$('#loading').hide();
$('.post_area').append(response);
}
});
}//end of if
return false;
});// end of $(window).scroll(function());
});
</script>
ajax_load.php
public function loadPostsFriends($data, $limit){
$page = $data['page'];
$userloggedin = $this->user_obj->getUsername();
if($page == 1)
$start = 0;
else
$start = ($page - 1 ) * $limit;
$str=""; //string to return
$data_query = mysqli_query($this->con, "select * from posts where deleted ='no' order by id desc");
if(mysqli_num_rows($data_query) > 0){
$num_iterations = 0; // number of items checked (not necessarily posted)
$count = 1;
while($row = mysqli_fetch_array($data_query))
{
$id = $row['id'];
$body = $row['body'];
$added_by = $row['added_by'];
$date_time = $row['date_added'];
//prepare user_to string so it can be included even if not posted to a specific user
if($row['user_to'] = "none"){
$user_to = "";
}
else{
$user_to_obj = new User($con, $row['user_to']);
$user_to_name =$user_to_obj->getFirstAndLastName();
$user_to = "<a href='" . $row['user_to'] . "'>" .$user_to_name . "</a>";
}
// check if a user who posted has their account closed
$added_by_obj = new User($this->con, $added_by);
if($added_by_obj->isClosed()){
continue;
}
if($num_iterations++ < $start)
continue;
//once 10 posts is loaded, break
if($count > $limit){
break;
}
else{
$count++;
}
$user_details_query = mysqli_query($this->con, "select first_name, last_name, profile_pic from users where username='$added_by'");
$user_row = mysqli_fetch_array($user_details_query);
$first_name = $user_row['first_name'];
$last_name = $user_row['last_name'];
$profile_pic = $user_row['profile_pic'];
//Time Frame
$date_time_now = date("Y-m-d H:i:s");
$start_date = new DateTime($date_time); //time of post
$end_date = new DateTime($date_time_now); //current time
$interval = $start_date->diff($end_date); //difference between dates
if($interval->y >= 1)
{
if($interval == 1)
$time_message = $interval->y . " year ago"; //1 year ago
else
$time_message = $interval->y . " years ago"; // more than 1 year ago
}
else if($interval->m >= 1)
{
if($interval->d == 0)
$days = " ago";
else if($interval->d == 1)
$days = $interval->d . " day ago";
else
$days = $interval->d . " days ago";
if($interval->m == 1)
$time_message = $interval->m . " month" . $days;
else
$time_message = $interval->m . " months" . $days;
}
else if($interval->d >= 1)
{
if($interval->d == 1)
$time_message = "Yesterday";
else
$time_message = $interval->d . " days ago";
}
else if($interval->h >= 1)
{
if($interval->h == 1)
$time_message = $interval->h . " hour ago";
else
$time_message = $interval->h . " hours ago";
}
else if($interval->i >= 1)
{
if($interval->i == 1)
$time_message = $interval->i . " minute ago";
else
$time_message = $interval->i . " minutes ago";
}
else
{
if($interval->s < 30)
$time_message = "Just now";
else
$time_message = $interval->s . " seconds ago";
}
$str .= "<div class='status_post'>
<div class='post_profile_pic'>
<img src='$profile_pic' width='50'>
</div>
<div class='posted_by' style='color: #ACACAC;'>
<a href='$added_by' id='user_names_in_post'> $first_name $last_name </a> $user_to $time_message
</div>
<div id='post_body'>
$body
</div>
</div>
<hr>";
} // end of while
if($count > $limit){
$str .= "<input type='hidden' class='nextPage' value='" . ($page + 1) . "'>
<input type='hidden' class='noMorePosts' value='false'>";
}
else{
$str .= "<input type='hidden' class='noMorePosts' value='true'><p style='text-align: center;'> No more posts to show! </p>";
}
}// end of if
echo $str;
}//end of loadPostsFriends