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

}
?>
4

2 に答える 2

2

現在、結果セットをループしていないため、1行のみをフェッチしています。次のことを試してください。これにより、$json配列が3つの配列(ストリーム、コメント、ユーザー)に分割され、それらが配列(各行に1つ)になります。それに応じて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);
    });
}

編集:上記では、位置iの配列を検索する代わりに、変数と変数を直接使用する方がstream適切commentですuser

また、スクリプトをいくつかのスクリプトに分割して、焦点を絞ることをお勧めします。

于 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 に答える