gpt4 book ai didi

javascript - JS在隐藏未定义值后仅返回数组中第一个id的值

转载 作者:行者123 更新时间:2023-11-28 07:32:00 25 4
gpt4 key购买 nike

我从一个页面发送了许多动态帖子 ID,并且 PHP 服务器端页面(server.php)使用这些 id 进行查询以查找 mysql 中的新添加的数据

如果在 mysql 中未找到任何新添加的数据,则返回一个未定义值。所以我添加了这个 if (msg.id !== undefined && msg.detail !== undefined && msg.name !== undefined) {//do here } 来隐藏 undefined。

但是添加上述行后,我的脚本很好地隐藏了 undefined 值,但仅返回第一个 CID 新添加的值。

这意味着如果 CID 将 ids(100, 101, 102, 103 等) 发送到 php,它只会返回 100 个 id 新添加的值并附加它。

请问问题出在哪里?

注意如果没有上面的行,它会很好地返回所有 CID 的值,但如果没有找到新数据,也会返回未定义值。

我的 JavaScript:

var CID = []; // Get all dynamic ids of posts (works well)
$('div[data-post-id]').each(function(i){
CID[i] = $(this).data('post-id');
});

function addrep(type, msg){
CID.forEach(function(id){
if (msg.id !== undefined && msg.detail !== undefined && msg.name !== undefined) {
$("#newreply"+id).append("<div class='"+ type +""+ msg.id +"'><ul><div class='newkochi'>"+ msg.name +"</div><div class='cdomment_text'>"+ msg.detail +"</ul></div>");
}
});
}

function waitForRep(){
$.ajax({
type: "GET",
url: "server.php",
cache: false,
data: {CID : CID},
timeout:15000,
success: function(data){
addrep("postreply", data);
setTimeout(waitForRep, 15000 );
},
error: function(XMLHttpRequest, textStatus, errorThrown){
setTimeout(waitForRep, 15000); }
});
}

$(document).ready(function(){
waitForRep();
});

服务器.php

while (true) {
if($_GET['CID']){ //cid got all dynamic post id as: 1,2,3,4 etc.
foreach($_GET['CID'] as $key => $value){

$datetime = date('Y-m-d H:i:s', strtotime('-15 second'));
$res = mysqli_query($dbh,"SELECT * FROM reply WHERE qazi_id=".$_GET['tutid']." AND date >= '$datetime' ORDER BY id DESC LIMIT 1") or die(mysqli_error($dbh));
$data = array();
while($rows = mysqli_fetch_assoc($res)){

$data[]=$rows;

$data['id'] = $rows['id'];
$data['qazi_id'] = $rows['qazi_id'];
$data['username'] = $rows['username'];
$data['description'] = $rows['description'];
$data['date'] = $rows['date'];
//etc. all
$id = $rows['id'];
$qazi_id = $rows['qazi_id'];
$username = $rows['username'];
$description = $rows['description'];
//etc. all
} //while close
} //foreach close

$name .='<p class="name">'.$username.' Says:</p>';
$detail .=''.$description.'';

$data['name'] = $name;
$data['detail'] = $detail;
// do others something more like as above

if (!empty($data)) {
echo json_encode($data);
flush();
exit(0);
}

} //request close
sleep(5);
} //while close

最佳答案

JavaScript 中发现的问题:

修复1:

var CID = [$('div[data-post-id]').length]; 

$('div[data-post-id]').each(function(i){
CID[i] = $(this).data('post-id');
});

修复2:

var CID = []; 
$('div[data-post-id]').each(function(i){
CID.push($(this).data('post-id'));
});

插入问题:

function addrep(type, msg){
CID.forEach(function(id){
//You are inserting the comment in all html tags havin id ='newreplay' + id
//Here you need to correct you logic like: if (msg.id == id) //do something

if (msg.id !== undefined && msg.detail !== undefined && msg.name !== undefined) {
$("#newreply"+id).append("<div class='"+ type +""+ msg.id +"'><ul><div class='newkochi'>"+ msg.name +"</div><div class='cdomment_text'>"+ msg.detail +"</ul></div>");
}
});
}

关于javascript - JS在隐藏未定义值后仅返回数组中第一个id的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29029010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com