数组未发送所有元素,只获取第一个元素


Array not sent all element, only get 1st one

我想在间隔15秒后从当前页面获取所有posts id,以进行服务器端php查询。Php查询将在Sql中找到与id匹配的数据,如果找到特定id的任何数据,则为.append-it.

所以在我当前的页面上有很多div,它们有自己的动态post-id,比如:

<div class="case" data-post-id="111"></div>
<div class="case" data-post-id="222"></div>
<div class="case" data-post-id="333"></div>
<div class="case" data-post-id="anything else dynamic no"></div>

我希望,我的javascript将获取并发送这些id到php查询,以便在Sql中查找任何匹配项。

在这里,我的array只得到了第一个帖子id。问题是我的javascript数组或php数组

我的更新脚本:(此处var CID无法获取id,只能获取第一个id)

//make array to get id
var CID = []; //get dynamic id 
$('div[data-post-id]').each(function (i) {
    CID[i] = $(this).data('post-id');
});
function addrep(type, msg) {
    CID.forEach(function (id) {
        $("#newreply" + id).append("");
    });
}
var tutid = '<?php echo $tutid; ?>';
function waitForRep() {
    $.ajax({
        type: "GET",
        url: "/server.php",
        cache: false,
        data: {
            tutid: tutid,
            // this way array containing all ID's can be sent:
            cid: CID
        },
        timeout: 15000,
        success: function (data) {
            addrep("postreply", data);
            setTimeout(
            waitForRep,
            15000);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            setTimeout(
            waitForRep,
            15000);
        }
    });
}

server.php

if($_REQUEST['tutid'] && $_REQUEST['cid']){
    //make array to cid to get id
    foreach($_REQUEST['cid'] as $key => $value){
       $res = mysqli_query($dbh,"SELECT * FROM test WHERE id =".$value." AND page_id=".$_REQUEST['tutid']." ORDER BY id DESC LIMIT 1") or die(mysqli_error($dbh));
        $rows =  mysqli_fetch_assoc($res);  
        $row[] = array_map('utf8_encode', $rows); //line 80
        $data = array();
        $data['id'] = $rows['id']; 
        //etc all
            //do something
if (!empty($data)) {
    echo json_encode($data);
    flush();
    exit(0);
}
} }

更改小代码并检查

//make array to get id
var CID = []; //get dynamic id 
$('div[data-post-id]').each(function (i) {
    CID[CID.length] = $(this).data('post-id');
});