layui資料庫查詢及資料處理
阿新 • • 發佈:2018-12-01
資料庫資料查詢及返回資料(例):
//********************************************************************************** case 'getmess': $page = $_REQUEST['page']; $limit = $_REQUEST['limit']; $unames = $_REQUEST['unames']; $times = date("Y-m-d H:i:s"); $logs = $_REQUEST['logs']; $names = $_REQUEST['names']; // 寫sql語句 $sql = "SELECT count(*) FROM messboard WHERE $names = '$unames'"; // 執行SQL語句 $res = $db->query($sql); // 返回結果集 $mrr = $res->fetch_all(); if($mrr){ $sql = "SELECT * FROM messboard WHERE $names= '$unames' ORDER BY mid DESC limit ".($page-1)*$limit.",$limit"; $res = $db->query($sql); // 新建陣列 $arr1 = array(); $arr2 = array(); while($attr = $res->fetch_assoc()){ array_push($arr1,$attr); }$arr2["code"] = 0; $arr2["msg"] = ""; $arr2["count"] = $mrr[0][0]; $arr2["data"] = $arr1; include('../../../../../public/logpublic.php'); echo json_encode($arr2); } break; //************************************************************************
資料響應格式:
JavaScript前臺頁面資料處理(例):
function showmess(){ var preid = $('#preid').html(); var uid = $('#uid').html(); var unames = $('#unames').html(); //********************************************************************************** layui.use('table', function(){ var table = layui.table; table.render({ where:{ type:'getmess' ,logs:'檢視個人留言板' ,unames:$('#unames').html() ,names:'rnames' } ,elem: '#mess_id' ,height:'450px' ,url:'messboard/messboard_php.php' ,toolbar: '#toolbarDemo' ,limit:'8' ,title: '留言板表' ,cols: [[ {type: 'checkbox', fixed: 'left'} ,{field:'mid', title:'id',width:50} ,{field:'snames', title:'傳送人', width:150} ,{field:'rnames', title:'接收人', width:150} ,{field:'content', title:'留言內容', width:350} ,{field:'messtime', title:'留言時間', width:182} ,{field:'state', title:'狀態', width:100} ,{fixed: 'right', title:'操作', width:150,templet:function(d){ return "<div class='layui-btn-group'><button class='layui-btn layui-btn-sm' onClick='reply("+d.mid+")'>回覆</button><button class='layui-btn layui-btn-sm layui-btn-danger' onClick='del_mess("+d.mid+")'>刪除</button></div>" }} ]] ,page: true })