简体   繁体   English

数组未发送所有元素,仅获得第一个

[英]Array not sent all element, only get 1st one

I want to get all posts id from current page after 15 second interval to make server side php query. 我想在15秒间隔后从当前页面获取所有帖子ID,以进行服务器端php查询。 Php query will find match in Sql with id and if any data find for a particular id's, it will be .append it. php查询将在Sql中找到ID匹配的项,如果找到特定ID的任何数据,则将其追加。

So in my current page have many div with their own dynamic post id like: 因此,在我当前的页面中,有许多具有自己动态帖子ID的div,例如:

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

And I want, my javascript will get and sent this ids to a php query for find any match in Sql. 而且我想,我的javascript将获取此ID并将其发送到php查询,以查找Sql中的任何匹配项。

Here my array only got 1st post id. 在这里,我的array只有第一个帖子ID。 Problem is either my javascript array or php array 问题是我的javascript数组还是php数组

My update script: (here var CID cannot get ids, only get first id) 我的更新脚本:(此处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 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);
}
} }

Change small code and check it 更改小代码并检查

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM