编辑:问题已解决。罪魁祸首是一个错误的单引号:P 显示内容的第一张图片:
第二张图片是没有内容显示的地方
几天前我已经问过这个问题,我的问题在那里解决了,但是突然又出现了问题,我似乎找不到解决办法。我想做的是加载滚动时将超过10个帖子的帖子,但是当我添加Ajax时却没有显示任何帖子。 这是代码:
index.php code:
<?php
include("includes/header.php");
include("includes/classes/User.php");
include("includes/classes/Post.php");
if(isset($_POST['post']))
{
$post=new Post($con,$userLoggedIn);
$post->submitPost($_POST['post_text'],'none');
}
?>
<div class="user_details column">
<a href="<?php echo $userLoggedIn; ?>" > <img src="<?php echo $user['profile_pic']; ?>"></a>
<div class="user_details_left_right">
<a href="<?php echo $userLoggedIn; ?>">
<?php
echo $user['first_name'] . " " . $user['last_name'];
?>
</a>
<br>
<?php
echo "My Posts: " . $user['num_post'] . "<br>";
echo "Likes: " . $user['num_likes'];
?>
</div>
</div>
<div class="main_column column">
<form class="post_form" action="index.php" method="POST">
<textarea name="post_text" id="post_text" placeholder="Write something here..."></textarea>
<input type="submit" name="post" id="post_button" value="Post">
<hr>
</form>
<!--<?php
$post=new Post($con,$userLoggedIn);
$post->loadPostsFriends();
?>-->
<div class="posts_area"></div>
<img id="loading" src="assets/images/icons/loading.gif">
</div>
<script>
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=$('.posts_area').find('.nextPage').val();
var noMorePosts=$('posts_area').find('.noMorePosts').val();
if((document.body.scrollHeight == document.body.scrollTop + window.innerHeight) && noMorePosts == 'false')
{
$('#loading').show();
alert("hello");
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()
});
</script>
</div>
</body>
</html>
Post.php code:
<?php
class Post
{
private $user_obj;
private $con;
public function __construct($con,$user)
{
$this->con = $con;
$this->user_obj = new User($con,$user);
}
//$user_obj=new User($con,$userLoggedIn)
public function submitPost($body,$user_to)
{
$body=strip_tags($body); //To remove any HTML tag
$body=mysqli_escape_string($this->con, $body);
$check_empty=preg_replace('/\s+/','',$body); //To delete spaces
if($check_empty != "")
{
//Date and time
$date_added=date("Y-m-d H:i:s");
//get user name
$added_by=$this->user_obj->getUsername();
//If user is watching his/her own profile, user_to is none
if($user_to == $added_by)
{
$user_to = "none";
}
//Insert post
$query = mysqli_query($this->con,"INSERT INTO posts VALUES('','$body','$added_by','$user_to','$date_added','no','no','0')");
$returned_id=mysqli_insert_id($this->con);
//Insert noti
//Update posts count
$num_posts=$this->user_obj->getNumPosts();
$num_posts++;
$update_query=mysqli_query($this->con,"UPDATE users SET num_post='$num_posts' WHERE username='$added_by'");
}
}
public 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'];
$data[] = $id;
$data[] = $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++;
}
$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'];
//Datetime
$date_time_now=date("Y-m-d H:i:s");
$start_date=new DateTime($date_time); //Post date & time
$end_date=new DateTime($date_time_now); //Current time
$interval=$start_date->diff($end_date); //Difference between above 2 dates
if($interval->y >= 1)
{
if($interval==1)
$time_message=$interval->y . "year ago"; //1 year ago
else
$time_message=$interval->y . "year 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:#BDBDBD;'>
<a href='$added_by'> $first_name $last_name </a> $user_to $time_message
</div>
<div id='post_body'>$body<br></div>
</div><hr>";
}
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!</p>";
}
echo $str;
}
}
?>
user.php code:
<?php
class User
{
private $user;
private $con;
public function __construct($con,$user)
{
$this->con = $con;
$user_details_query=mysqli_query($con,"SELECT * FROM users WHERE username='$user'");
$this->user = mysqli_fetch_array($user_details_query);
}
//$user_obj=new User($con,$userLoggedIn)
public function getUsername()
{
return $this->user['username'];
}
public function getNumPosts()
{
$username=$this->user['username'];
$query=mysqli_query($this->con,"SELECT num_post FROM users WHERE username='$username'");
$row=mysqli_fetch_array($query);
return $row['num_post'];
}
public function getFirstAndLastName()
{
$username=$this->user['username'];
$query=mysqli_query($this->con, "SELECT first_name, last_name FROM users WHERE username='$username'");
$row=mysqli_fetch_array($query);
return $row['first_name'] . " " . $row['last_name'];
}
public function isClosed()
{
$username=$this->user['username'];
$query=mysqli_query($this->con,"SELECT user_closed FROM users WHERE username='$username'");
$row=mysqli_fetch_array($query);
if($row['user_closed']=='yes')
return true;
else
return false;
}
public function isFriend($username_to_check)
{
$usernameComma="," . $username_to_check . ",";
if((strstr($this->user['friend_array'],$usernameComma)) || $username_to_check==$this->user['username'])
{
return true;
}
else
{
return false;
}
}
}
?>
ajaxloadpost.php code:
<?php
include("../../config/config.php");
include("../classes/User.php");
include("../classes/Post.php");
$limit=10; //Limit post to 10 each time
$posts=new Post($con,$_REQUEST['userLoggedIn']);
$posts->loadPostsFriends($_REQUEST,$limit);
?>