简体   繁体   English

如何显示从数据库接收的消息数组,用于聊天室

[英]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.

相关问题 / PM regex语法,用于在聊天室中发送消息 - /PM regex syntax for sending message in chat-room javascript 聊天显示消息 - javascript chat display messages 如何在 Django 的聊天应用程序中显示消息 - How to display messages in a chat application in Django 如何将 signalR 聊天消息保存到数据库 - How to save signalR chat messages to database 如何根据flask-socketio中的房间选择显示聊天记录 - How to display chat history depending on room selection in flask-socketio 如何将图像从一个用户发送到服务器,然后使用节点 js 和 socket.io 将其显示回聊天室中的所有客户端? - How to send images from one user to server and then display it back to all clients in the chat room using node js and socket.io? AJAX请求从聊天数据库获取最新的唯一消息 - AJAX Request to get most recent, unique messages from chat database Flask:在数据库中保存聊天消息 - Flask: Saving chat messages in database 如何遍历mysql数据库中的消息并正确显示它们 - How to loop through messages from mysql database and display them correctly 在多频道聊天应用程序中,如何显示仅与特定聊天频道有关的消息? - In a multichannel chat application how can I display messages pertaining only to a specific chat channel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM