0

我的脚本工作正常,但只显示 1 个结果。我需要添加什么才能从我的数据库中获取接下来的十个结果。我已经提供了一小部分我认为最重要的代码。

AJAX

 <script type="text/javascript">
$(function() 
{
$('.load_more').live("click",function() 
{
var ID = $(this).attr("id");
if(ID)
{
$("#load"+ID).html('Loading...');

$.ajax({
type: "POST",
url: "include/load_more_home_posts.php",
cache: false, 
dataType: "json",
data: { streamitem_id: ID},
cache: false,
success: function(response){
$("#articles").prepend("<div id='divider-"+response['streamitem_id']+"'><div class='userinfo'><a href='/profile.php?username="+response['username']+"'><img class='stream_profileimage' style='border:none;padding:0px;display:inline;' border=\"0\" src=\"imgs/cropped"+response['id']+".jpg\" onerror='this.src=\"img/no_profile_img.jpeg\"' width=\"40\" height=\"40\" ></a><div class'delete' style='cursor:pointer;position:relative;top:0px;float:right;padding-right:5px;' onclick=\"delete_('"+response['streamitem_id']+"');\">X</div><a href='/profile.php?username="+response['username']+"'>"+response['first']+" "+ response['middle']+" "+response['last']+"</a><span class='subtleLink'> said</span><br/><a class='subtleLink' style='font-weight:normal;'>"+response['streamitem_timestamp']+"</a><hr>"+response['streamitem_content']+"<div style='height:20px;' class='post_contextoptions'><div id='streamcomment'><a style='cursor:pointer;' id='commenttoggle_"+response['streamitem_id']+"' onclick=\"toggle_comments('comment_holder_"+response['streamitem_id']+"');clearTimeout(streamloop);swapcommentlabel(this.id);\">Write a comment...</a></div><div id='streamlike'><a title='Like "+response['first']+" "+ response['middle']+" "+response['last']+"s status' id='likecontext_"+response['streamitem_id']+"' style='cursor:pointer;' onClick=\"likestatus("+response['streamitem_id']+",this.id);\"><div style='width:50px;' id='likesprint"+response['streamitem_id']+"'>Like</a></div><div style='width:50px;' id='likesprint"+response['streamitem_id']+"'><a title='See who likes "+response['first']+" "+ response['middle']+" "+response['last']+"s status' href='include/likes.php?streamitem_id="+response['streamitem_id']+"' /></a></div></div></form></div><div id='streamdislike'><a id='dislikecontext_"+response['streamitem_id']+"' style='cursor:pointer;' onClick=\"dislikestatus("+response['streamitem_id']+",this.id);\"><div style='width:70px;' id='dislikesprint"+response['streamitem_id']+"'>Dislike</a></div><div style='width:70px;' id='dislikesprint"+response['streamitem_id']+"'></div></div></form><div class='stream_comment_holder' style='display:none;' id='comment_holder_"+response['streamitem_id']+"'><div id='comment_list_"+response['streamitem_id']+"'></div><div class='stream_comment_inputarea'><form id='mycommentform' method='POST'  class='form_statusinput'>\
<input type='hidden'  name='streamidcontent' id='streamidcontent' value='"+response['streamitem_id']+"'>\
<input type='input' name='commentingcontents' id='commentingcontents' placeholder='Say something' autocomplete='off'>\
<input type='submit' id='button' value='Feed'><br/></div></div>").show();
// remove the previous load more link
$("#load"+ID).remove();
}
});
}
return false;
});
});
</script>

LOAD_MORE_HOME_POSTS

    <?php
session_start();
include("rawfeeds_load.php");

// get the article ID from ajax POST
if (isset($_POST['streamitem_id']) && $_POST['streamitem_id'] != "") {
$lastID = mysqli_real_escape_string($mysqli,$_POST['streamitem_id']);


 $following_string=mysqli_real_escape_string($mysqli,$_SESSION['id']);
 $sql_follows =  "SELECT * FROM friends WHERE user1_id=".$following_string." AND status=2 OR user2_id=".$following_string." AND status=2"; 
 $query_follows=mysqli_query($mysqli, $sql_follows) or die("Error finding friendships");


if($query_follows){
$friendlist="(".$_SESSION['id'].",";
$t=0;
while($follow=mysqli_fetch_array($query_follows))
{
if($follow['user1_id']==$_SESSION['id']){
if($t>0){
$friendlist=$friendlist.",";
}
$friendlist=$friendlist. $follow['user2_id'];
}else{
if($t>0){
$friendlist=$friendlist.",";
}
$friendlist=$friendlist. $follow['user1_id'];
}
$t=$t+1;
}
$friendlist=$friendlist.")";
}
//STREAMDATA
$badcall = "(".mysqli_real_escape_string($mysqli,$_SESSION['id']).",)";
if($friendlist==$badcall){
$friendlist="(".mysqli_real_escape_string($mysqli,$_SESSION['id']).")";
}
if(isset($_GET['limit'])){
$sqllimit = $_GET['limit'];
}else{
$sqllimit = 20;
}

$following_string = mysqli_real_escape_string($mysqli,$_SESSION['id']);
$call="SELECT * FROM streamdata WHERE streamitem_id < '$lastID' AND streamitem_target=".$following_string." OR streamitem_creator=".$following_string." OR streamitem_creator IN ".$friendlist." AND streamitem_target IN ".$friendlist." ORDER BY streamitem_id DESC LIMIT 10";
$chant= mysqli_query($mysqli, $call) or die(mysqli_error($mysqli));
$json = array();   
 while ($resultArr = mysqli_fetch_assoc($chant)) {
    $json[] = array(
        'streamitem_id' => $resultArr['streamitem_id'],
        'streamitem_content' => $resultArr['streamitem_content'],
        'streamitem_timestamp' => Agotime($resultArr['streamitem_timestamp'])
    );
}

$check = "SELECT comment_id, comment_datetime, comment_streamitem, comment_poster, comment_content FROM streamdata_comments WHERE comment_poster=".$following_string."  ORDER BY comment_datetime DESC";
$check1 = mysqli_query($mysqli,$check);
$resultArr = mysqli_fetch_assoc($check1);
$json['comment_id'] = $resultArr['comment_id'];
$json['comment_content'] = $resultArr['comment_content'];
$json['comment_poster'] = $resultArr['comment_poster'];
$json['comment_datetime'] = Agotime($resultArr['comment_datetime']);
$json['comment_streamitem'] = $resultArr['comment_streamitem'];



$check = "SELECT * FROM users WHERE id=".$following_string."";
$check1 = mysqli_query($mysqli,$check);
$resultArr = mysqli_fetch_assoc($check1);
$json['username'] = $resultArr['username'];
$json['id'] = $resultArr['id'];
$json['first'] = $resultArr['first'];
$json['middle'] = $resultArr['middle'];
$json['last'] = $resultArr['last'];


echo json_encode($json);

}
?>
4

2 回答 2

2

现在你没有遍历你的结果集,因此你只获取一行。尝试以下操作,它将您的$json数组拆分为三个数组(流、评论、用户),而这三个数组又是数组(每行一个)。请注意,您必须相应地更改您的 jQuery 代码。

/***** STREAMS *****/
$chant = mysqli_query($mysqli, $call) or die(mysqli_error($mysqli));
$json = array();
$json['streams'] = array();

while ($resultArr = mysqli_fetch_assoc($chant)) {
    $arr = array();
    $arr['streamitem_id'] = $resultArr['streamitem_id'];
    $arr['streamitem_content'] = $resultArr['streamitem_content'];
    $arr['streamitem_timestamp'] = Agotime($resultArr['streamitem_timestamp']);

    $json['streams'][] = $arr;
}

/***** COMMENTS *****/
$check = "SELECT comment_id, comment_datetime, comment_streamitem, comment_poster, comment_content FROM streamdata_comments WHERE comment_poster=".$following_string."  ORDER BY comment_datetime DESC";
$check1 = mysqli_query($mysqli,$check);
$json['comments'] = array();

while ($resultArr = mysqli_fetch_assoc($check1)) {
    $arr = array();
    $arr['comment_id'] = $resultArr['comment_id'];
    $arr['comment_content'] = $resultArr['comment_content'];
    $arr['comment_poster'] = $resultArr['comment_poster'];
    $arr['comment_datetime'] = Agotime($resultArr['comment_datetime']);
    $arr['comment_streamitem'] = $resultArr['comment_streamitem'];

    $json['comments'][] = $arr;
}

/***** USERS *****/

$check = "SELECT * FROM users WHERE id=".$following_string."";
$check1 = mysqli_query($mysqli,$check);
$json['users'] = array();

while ($resultArr = mysqli_fetch_assoc($check1)) {
    $arr = array();
    $arr['username'] = $resultArr['username'];
    $arr['id'] = $resultArr['id'];
    $arr['first'] = $resultArr['first'];
    $arr['middle'] = $resultArr['middle'];
    $arr['last'] = $resultArr['last'];

    $json['users'][] = $arr;
}


echo json_encode($json);

至于 jQuery,我将只向您展示您可以做什么的示例,因为我不知道您的 DOM 是什么样的,而且您的代码也不是那么简单。您应该能够更新您的代码而不会感到头疼。

success: function(response) {
    // Streams
    $.each(response.streams, function(i, stream) {
        alert(response.streams[i].streamitem_id);
    });

    // Comments
    $.each(response.comments, function(i, comment) {
        alert(response.comments[i].comment_id);
    });

    // Users
    $.each(response.users, function(i, user) {
        alert(response.users[i].id);
    });
}

编辑:在上面,直接使用stream,commentuser变量而不是查看位置 i 处的数组会更漂亮。

另外,我建议您将脚本拆分为几个关注范围更窄的脚本。

于 2012-09-02T17:09:35.693 回答
0

尝试这个:

$json = array();    
while($resultArr = mysqli_fetch_assoc($chant)) { 
    $json[] = array(
        'streamitem_id' => $resultArr['streamitem_id'],
        'streamitem_content' => $resultArr['streamitem_content'],
        'streamitem_timestamp' = Agotime($resultArr['streamitem_timestamp'])
    );
}
于 2012-09-02T17:09:50.277 回答