ThinkPHP结合ajax、Mysql实现的客户端通信功能代码示例
这篇文章主要介绍了ThinkPHP结合ajax、Mysql实现的客户端通信功能,需要的朋友可以参考下
该实例通过ThinkPHP结合Ajax与mysql实现了客户端的通信功能,具体如下:
1、使用js的ajax局部刷新功能,每次刷新将数据库中读取出的新记录插入到页面的显示区域,代码如下:
window.onload =setInterval(showWords1000);//加载完成之后开始执行刷新功能
function showWords()//刷新时被调用函数,实现ajax请求
{
xmlHttp=GetXmlHttpObject();//从自定义的函数中获取请求对象。
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return;
}
var url="index.php?g=Ksks&m=Comments&a=refresh";//要请求的路径使用shuipfcms框架,所以g是分组,m是action控制器,a是模板
url=url+"&data_id="+$("#data_id").val();//接上,,data_id传的参数,用于查询数据库的id
url=url+"&lasttime="+$("i").last().siblings('input').val();//lasttime是页面显示数据的最后一个记录的时间戳,查找时根据大于这个时间戳的条件查找
xmlHttp.onreadystatechange=stateChanged;//响应状态改变时调用的函数
xmlHttp.open("GET",url,true);//使用get方法提交
xmlHttp.send();//上面传参之后,这句可要可不要
}
function stateChanged() //实现向页面插入数据功能
{
var str ="";
if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
eval("var arrs="+xmlHttp.responseText);//获取由action响应输出的数据,因为数据室php数组格式,需用eval或json转化成js数组格式
var chdiv=$("#conversation_history");//通信聊天记录框
for(var j=0;j
");//定义页面当中记录框的html格式代码
var i=$(" "+arrs[j].times+"");//i标签i为thinkphp定义的直接将时间输出到input隐藏并且存储得到数据里面的聊天发送时间
var p=$("");
p.html(arrs[j].content);//聊天内容
p.append(i);
var h3value=arrs[j].name;//发送的作者
obj.find("h3").html(h3value+":");
if(h3value=="主持人")
{obj.find("h3").addClass("presenter");}//主持人会特别显示
obj.find("td:last").append(p);
chdiv.append(obj);//追加到table里面
}
chdiv.parent().scrollTop(chdiv.parent()[0].scrollHeight+0);//滚动到底部
}
}
function GetXmlHttpObject()//定义ajax请求对象 { var xmlHttp=null; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { //Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; }
2、action控制器,代码如下:
function refresh(){ $data_id = I('get.data_id');//获得嘉宾id $lasttime = I('get.lasttime'); //print_r(date("Y-m-d H:i:s",$lasttime)); if(!$data_id) $this->error("该访谈不存在!"); $data = M("ksks_words_memoir"); //print_r(date('Y-m-d H:i:s',$lasttime)); if(!$data) $this->error("初始化数据失败!"); //$words = $data->where(array('dataid' => $data_id))->order('time asc')->select(); //print_r($data->where(array('time' => $lasttime))->select()); $where['time'] = array('gt',$lasttime); $count = $data->where($where)->count(); //print_r(date("Y-m-d H:i:s",$lasttime)); $words = $data->where($where)->select(); $datas = array();//要返回的数组 $i = 0; foreach($words as $word) { $datas[$i]['name'] = $word['name']; $datas[$i]['content'] = $word['content']; $datas[$i]['times'] = date('Y-m-d H:i:s',$word['time']); $lasttime = $word['time']; $i = $i+1; } setCookie('lasttime',$lasttime); $datas = json_encode($datas); //$this->cache(); echo $datas; }
3、聊天页面,代码如下:
class="presenter">{$vo.name}:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

Laravel and ThinkPHP are both popular PHP frameworks and have their own advantages and disadvantages in development. This article will compare the two in depth, highlighting their architecture, features, and performance differences to help developers make informed choices based on their specific project needs.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.
