[英]How to display an array of messages being received from database, for a chat-room
So I have attempted to create a basic chat-room for my site. 所以我试图为我的网站创建一个基本的聊天室。 I have successfully connected to the database, messages are being written and they are being received all OK. 我已成功连接到数据库,正在编写消息并且正在接收所有正常。
My problem is the messages that return from the database are displayed by letter, and line by line. 我的问题是从数据库返回的消息是按字母和逐行显示的。 Also, I have <body onload="setInterval('chat.update()', 1000)">
on my page chat.php
, whihc seems to load the same message over and over again until a new message is inputted. 另外,我在我的页面chat.php
上有<body onload="setInterval('chat.update()', 1000)">
,似乎chat.php
又一遍地加载相同的消息,直到输入新消息。
(Please see screenshot path posted, not enough rep to post image) (请查看发布的截图路径,没有足够的代表发布图片)
I suspect that I am returning a string rather than an array of strings which is why the messages are being displayed as they are. 我怀疑我返回一个字符串而不是一个字符串数组,这就是为什么消息按原样显示的原因。 I am fairly new to javascript and AJAX so I am unsure of how to proceed. 我是javascript和AJAX的新手,所以我不确定如何继续。
Can anyone share any advice on how to return and array of messages, that will perhaps solve my issue? 任何人都可以分享关于如何返回和消息数组的任何建议,这可能会解决我的问题吗?
Thanks in advance! 提前致谢!
Running MYSQL database, with chat.js
posting to process.php
. 运行MYSQL数据库,将chat.js
发布到process.php
。 chat.php
has the HTML code for the message area. chat.php
具有消息区域的HTML代码。
chat.php chat.php
<div class="container chat-container">
<div id="page-wrap">
<h2 class="forum-header">Discussion Room for: <?php echo "$session" ?> </h2>
<br>
<p id="name-area"></p>
<div id="chat-wrap"><div id="chat-area"></div></div>
<div class="md-form">
<form id="send-message-area">
<label class="message-label" for="form7">Your Message</label>
<textarea id="sendie" maxlength = '100' class="md-textarea form-control" rows="3"></textarea>
</form>
</div>
</div>
</div>
</body>
chat.js chat.js
//Updates the chat
function updateChat() {
if (!instanse) {
instanse = true;
$.ajax({
type: "POST",
url: "process.php",
data: {
'function': 'update',
'state': state
},
dataType: "json",
success: function (data) {
if (data.text) {
for (var i = 0; i < data.text.length; i++) {
$('#chat-area').append("<p>" + data.text[i] + "</p>");
}
}
document.getElementById('chat-area').scrollTop = document.getElementById('chat-area').scrollHeight;
instanse = false;
state = data.state;
}
});
} else {
setTimeout(updateChat, 1500);
}
}
process.php process.php
case('update'):
$state = $_POST['state'];
$lines = "SELECT messageBody, timeSent, nickname FROM message ORDER BY timeSent";
$result = mysqli_query($conn, $lines);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$message = $row['messageBody'];
$time = $row['timeSent'];
$nickname = $row['nickname'];
}
}
$count = count($lines);
if ($state == $count) {
$log['state'] = $state;
$log['text'] = false;
} else {
$text = array();
$log['state'] = $state + count($lines) - $state;
$text[] = $message = str_replace("\n", "", $message);
}
$log['text'] = $message;
break;
Also, I using echo json_encode($log); 另外,我使用echo json_encode($ log); Console Screenshot 控制台截图
In process.php you should save the fetched result in an array, like this. 在process.php中,您应该将获取的结果保存在数组中,如下所示。
if (mysqli_num_rows($result) > 0) {
$message = array();
while ($row = mysqli_fetch_assoc($result)) {
$message[] = $row['messageBody'];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.