ThinkPHP -Ajax, SQL返回最新10条数据方法
//js模板
//引入jq var API_URL = "http://chat.com/index.php/api/chat/"; function message_load(){ $.post( API_URL + "messageLoad", {fromid, toid}, function(e){ console.log(e); },'json' ); }
控制器模板
<?php namespace app\api\controller; use think\Controller; use think\Db; use think\Request; class Chat extends Controller {
返回10条数据方法
public function messageLoad() { if (Request::instance()->isAjax()) { $fromid = input('fromid'); $toid = input('toid'); $where = "(c_fromid=$fromid and c_toid=$toid) or (c_fromid=$toid and c_toid=$fromid)"; $result = Db::name('communication')->where($where)->field("c_fromid as fromid, c_content as content")->order('c_time', 'desc')->limit("10")->select(); $result = array_reverse($result); return $result; } }
2