我正在尝试建立一个社交媒体网络站点作为一个主要项目,并且今天我尝试使用Ajax,但是实现后内容没有显示出来。我正在尝试加载限制为10个的帖子,但内容只是没有显示在索引上。
enter code herepublic function loadPostsFriends($data,$limit)
{
$page=$data['page'];
$userLoggedIn=$this->user_obj->getUsername();
if($page==1)
{
$start=0;
}
else
{
$start=($page-1) * $limit;
}
$str=""; //Str 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 result checking
$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 to include it even if not posted to an 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="wrote to <a href'" . $row['user_to'] . "'>" . $user_to_name . "</a>";
}
//check if userwho 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 loaded just break
if($count > $limit)
{
break;
}
else
{
$count++;
}
var userLoggedIn='<?php echo $userLoggedIn; ?>';
$(document).ready(function()
{
$('#loading').show();
//ajax request for loading 1st posts
$.ajax({
url: "includes/handlers/ajax_load_posts.php",
type: "POST",
data: "page=1&userLoggedIn=" + userLoggedIn,
cache:false,
success: function(data)
{
$("#loading").hide();
$('.posts_area').html(data);
}
});
$(window).scroll(function()
{
var height=$('.posts_area').height(); //div containing posts
var scroll_top=$(this).scrollTop();
var page=$('.post_area').find('.nextPage').val();
var noMorePosts=$('.posts_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=" + page + "&userLoggedIn=" + userLoggedIn,
cache:false,
success: function(response)
{
$('.posts_area').find('.nextPage').remove(); //removes current next page
$('.posts_area').find('.noMorePosts').remove();
$("#loading").hide();
$('.posts_area').append(response);
}
});
} //If end
return false;
}); //ending of $(window).scroll(function()
});
PS:添加了一些显示内容的模式代码,以更好地理解我的问题。
答案 0 :(得分:0)
好像您是新来的。欢迎。如果您没有完全发布代码,这会有些困难,因为其他人可能很难理解。因此,请下次完全发布。
使用Ajax显示数据的解决方案?
PHP代码:
var doActor = (_do)Marshal.GetDelegateForFunctionPointer(ptr, typeof(_do));
将数据保存到数据数组变量。然后使用 json_encode 以json格式返回。
JS代码:
$data = array();
while($row=mysqli_fetch_array($data_query)){
$id=$row['id'];
$body=$row['body'];
$data[] = $id;
$data[] = $body;
}
echo json_encode($data);
希望对您有帮助。
亲切的问候。