2012-09-02 58 views
0

我的腳本正在運行,但只顯示1個結果。我需要添加什麼才能從我的數據庫中獲得下十個結果。我已經提供了一小部分代碼,我認爲這是最重要的。'加載更多'獲得10個更多結果

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); 

} 
?> 

回答

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); 
    }); 
} 

編輯:在上文中,那將是更漂亮使用streamcommentuser變量,而不是直接在陣列在位置i仰視。

此外,我建議你將腳本分成幾個腳本,這些腳本的重點更加狹窄。

+1

嗨@ andy124。我已經嘗試過,仍然得到了1的結果。我更新了我通過Json撤回的其他代碼的問題。也許它需要調整。 – dave

+1

@dave變化很大。 :-)我已經更新了我的答案;請嘗試新的代碼,並讓我知道如果這是訣竅。 – Andy0708

+0

對。它接近FIREBUG的迴應,它給了我十個結果。在我的主頁上只顯示1,所有內容都顯示爲UNDEFINED。 – dave

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']) 
    ); 
}