gpt4 book ai didi

javascript - php 未输出 JSON 数据

转载 作者:行者123 更新时间:2023-11-28 05:24:42 26 4
gpt4 key购买 nike

我在获取从 php 文件发送的 JSON 数据以输出到我的页面时遇到问题。我想做的是创建一个评论系统,所以每当有人发表评论时,我的 AJAX 代码都会获取 SELECT 查询结果并重新加载已经列出的评论,本质上是为了显示新评论,无论是用户发布的还是其他人发布的。

我有三个文件,评论页面、AJAX 和 PHP 文件。我最初在评论页面上执行 SELECT 查询以获取页面加载的数据,然后我希望每三秒一致加载一组相同的评论。

setInterval() 正在工作,并且 JSON 正在发送,正如我在控制台中看到的那样,但它实际上并没有刷新和输出。另外,我真的只希望在创建了新评论时重新加载评论,而不是仅仅为了重新加载而重新加载评论。

有人看到我做错了什么导致 JSON 数据无法输出吗?

这是我最初获取页面加载输出的方式。这仅使用评论页面上的 php。

<div id="comment-container">
<?php
$select_comments_sql = "
SELECT c. *, p.user_id, p.img
FROM home_comments AS c
INNER JOIN (SELECT max(id) as id, user_id
FROM profile_img
GROUP BY user_id) PI
on PI.user_id = c.user_id
INNER JOIN profile_img p
on PI.user_id = p.user_id
and PI.id = p.id
ORDER BY c.id DESC
";

if ($select_comments_stmt = $con->prepare($select_comments_sql)) {
$select_comments_stmt->execute();
if (!$select_comments_stmt->errno) {
//echo "error";
}
$select_comments_stmt->bind_result($comment_id, $comment_user_id, $comment_username, $home_comments, $comment_date, $commenter_user_id, $commenter_img);
//var_dump($select_comments_stmt);
$comment_array = array();
while ($select_comments_stmt->fetch()) {
$comment_array[] = $comment_user_id;
$comment_array[] = $comment_username;
$comment_array[] = $home_comments;
$comment_array[] = $comment_date;
$comment_array[] = $commenter_user_id;
$comment_array[] = $commenter_img;
$commenter_img = '<img class="home-comment-profile-pic" src=" '.$commenter_img.'">';
if ($home_comments === NULL) {
echo 'No comments found.';
} else {
echo '<div class="comment-post-box" id="comment-'. $comment_id .'">';
//echo '<div class="comment-post-box" id="comment-'.$row['id'].'">';
echo $commenter_img;
echo '<div class="comment-post-username">'.$comment_username. '</div>';
echo '<div>'.$comment_date. '</div>';
echo '<div class="comment-post-text">'.$home_comments. '</div>';
echo '</div>';
}
}
}
?>
</div>

从 home_comments 表中选择并回显 JSON 的 PHP

$user = new User();

//Get the last insert id
$select_comments_sql = "
SELECT c. *, p.user_id, p.img
FROM home_comments AS c
INNER JOIN (SELECT max(id) as id, user_id
FROM profile_img
GROUP BY user_id) PI
on PI.user_id = c.user_id
INNER JOIN profile_img p
on PI.user_id = p.user_id
and PI.id = p.id
ORDER BY c.id DESC
";

if ($select_comments_stmt = $con->prepare($select_comments_sql)) {
//$select_comments_stmt->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$select_comments_stmt->execute();
$rows = $select_comments_stmt->fetchAll(PDO::FETCH_ASSOC);
$comments = array();
foreach ($rows as $row) {

$html = "";
//$html .= '<div class="comment-post-box">';
$html .= '<div class="comment-post-box" id="comment-'.$row['id'].'">';
$html .= '<img class="home-comment-profile-pic" src="'.$row['img'].'">';
$html .= '<div class="comment-post-username">'.$row['username']. '</div>';
$html .= '<div>'.$row['date']. '</div>';
$html .= '<div class="comment-post-text">'.$row['comment']. '</div>';
$html .= '</div>';
$data = array('id' => $row['id'], 'date' => $row['date'], 'html' => $html);
$comments[] = $data;
}
}
echo json_encode($comments);

然后我的 AJAX 尝试刷新评论列表:

function commentRetrieve(){

$.ajax({
url: "ajax-php/comment-retrieve.php",
type: "get",
success: function (data) {
//console.log(data);
if (data == "Error!") {
alert("Unable to retrieve comment!");
alert(data);
} else {
var array = JSON.parse(data);
console.log(data);
$(array).each(function(this) {
if($('#comment-' + this.id).length == 0) {
$('#comment-container').prepend(this.html);
console.log(this.html);
}
});
}
},
error: function (xhr, textStatus, errorThrown) {
alert(textStatus + " | " + errorThrown);
console.log("error"); //otherwise error if status code is other than 200.
}
});


}
setInterval(commentRetrieve, 3000);

编辑:数据已发送

[{id: "51", date: "2016-10-26 09:25:42",…}, {id: "22", date: "0000-00-00 00:00:00",…},…]
0
:
{id: "51", date: "2016-10-26 09:25:42",…}
1
:
{id: "22", date: "0000-00-00 00:00:00",…}
2
:
{id: "21", date: "0000-00-00 00:00:00",…}
3
:
{id: "20", date: "0000-00-00 00:00:00",…}
4
:
{id: "19", date: "2016-10-23 09:56:08",…}
5
:
{id: "18", date: "2016-10-21 09:51:35",…}
6
:
{id: "16", date: "2016-10-20 14:41:10",…}
7
:
{id: "15", date: "2016-10-20 13:23:30",…}
8
:
{id: "12", date: "2016-10-20 09:10:28",…}
9
:
{id: "11", date: "2016-10-19 23:54:58",…}
10
:
{id: "10", date: "2016-10-19 23:41:52",…}
11
:
{id: "9", date: "2016-10-19 23:41:52",…}
12
:
{id: "8", date: "2016-10-19 23:41:16",…}

我收到的地方^^

enter image description here

enter image description here

最佳答案

小工作示例:

var count = 0;
var RETRIEVE_INTERVAL = 1000;
var datas = [ '[{"id": "51", "date": "2016-10-26 09:25:42", "html": "<div id=\\"comment-51\\">51</div>"}]'
, '[{"id": "22", "date": "2016-10-26 09:25:42", "html": "<div id=\\"comment-22\\">22</div>"}]'
, '[{"id": "5", "date": "2016-10-26 09:25:42", "html": "<div id=\\"comment-5\\">5</div>"}]'
, '[{"id": "34", "date": "2016-10-26 09:25:42", "html": "<div id=\\"comment-34\\">34</div>"}]'
, '[{"id": "51", "date": "2016-10-26 09:25:42", "html": "<div id=\\"comment-51\\">51</div>"}]'
, '[{"id": "10", "date": "2016-10-26 09:25:42", "html": "<div id=\\"comment-10\\">10</div>"}]'
, '[{"id": "70", "date": "2016-10-26 09:25:42", "html": "<div id=\\"comment-51\\">70</div>"}]'
, '[{"id": "22", "date": "2016-10-26 09:25:42", "html": "<div id=\\"comment-22\\">22</div>"}]'
, '[{"id": "40", "date": "2016-10-26 09:25:42", "html": "<div id=\\"comment-40\\">40</div>"}]'
];

function commentRetrieve(){
// mock data retrieval
if (count == datas.length)
return;
var data = datas[count++];

var array = JSON.parse(data);
$(array).each(function() {
if($('#comment-' + this.id).length == 0) {
$('#comment-container').prepend(this.html);
}
});

setInterval(commentRetrieve, RETRIEVE_INTERVAL);
}

$(commentRetrieve);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div id="comment-container" id="intro-box">
<div id="comment-5">5</div>
<div id="comment-10">10</div>
</div>
</body>

关于javascript - php 未输出 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40269106/

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