您的位置:首页 > 编程语言 > PHP开发

mysql+php+ajax实现用户管理(简单版,日期处理)

2017-11-17 10:52 591 查看
user.html

<div id="right">
<div id="right_head">
<p>
<span>用户管理</span>
<span class="right_head_span1">欢迎:</span>
<span>XXX</span>
<a href="" class="right_head_span2">退出</a>
</p>
</div>

<div id="right_con">
<table border="1" cellspacing="0">
<thead>
<tr>
<td>用户名</td>
<td>分组</td>
<td>邮箱</td>
<td>手机号</td>
<td>注册时间</td>
<td>操作</td>
</tr>
</thead>
<tbody id="tby">

</tbody>
<tfoot>
<tr>
<td>
<a class="tfoot_a" onclick="getPage(1)">首页</a>
</td>
<td>
<a class="tfoot_a" onclick="prePage()">上一页</a>
</td>
<td>
<a class="tfoot_a" onclick="nextPage()">下一页</a>
</td>
<td>
<a class="tfoot_a" onclick="lastPage()">尾页</a>
</td>
<td>
<p>
转到<input type="text" id="in_pNum">页
<button onclick="goPage()">GO</button>
</p>
</td>
<td class="foot_last_td">
第<span id="tfoot_span1"></span>页/共<span id="tfoot_span2"></span>页
</td>
</tr>
</tfoot>
</table>
</div>
</div>

<script src="js/common.js"></script>
<script>
function getPage(num) {
var xhr = createXhr();
xhr.open("get","php/user.php?currentPage="+num,true);
console.log("1111");
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
var resText = xhr.responseText;
console.log(resText);
if(resText != "0"){
var arr = JSON.parse(resText);
var str = "";
var pageInfo = JSON.parse(arr[arr.length-1]);
for(var i=0;i<arr.length-1;i++){
str += "<tr>";
str += "<td>" + arr[i].uname + "</td>";
if(arr[i].groups == 1)
str += "<td>" + "管理员" + "</td>";
else if(arr[i].groups == 0)
str += "<td>" + "普通用户" + "</td>";
str += "<td>" + arr[i].email + "</td>";
str += "<td>" + arr[i].phone + "</td>";
str += "<td>" + arr[i].reg_time + "</td>";
str += "<td><a class=\"tfoot_a\" onclick=\"deleteUser(" + num + "," + arr[i].uid + ")\">删除</a>" + "</td>";
str += "</tr>";
}
$("tby").innerHTML = str;
$("tfoot_span1").innerHTML = pageInfo.currentPage;
$("tfoot_span2").innerHTML = pageInfo.totalPage;
}else{
$("tby").innerHTML = "";
}
}
}
xhr.send(null);
}

function prePage() {
console.log("prePage");
var currentPage = Number($("tfoot_span1").innerHTML);
getPage(currentPage-1);
}

function nextPage() {
console.log("nextPage");
var currentPage = Number($("tfoot_span1").innerHTML);
getPage(currentPage+1);
}

function lastPage() {
console.log("lastPage");
var lastPage = Number($("tfoot_span2").innerHTML);
getPage(lastPage);
}

function goPage() {
console.log("goPage");
var pageNum = Number($("in_pNum").value);
console.log(pageNum);
getPage(pageNum);
}

function deleteUser(num,uid) { //加一个页面参数 并刷新该页面
//alert("确定删除?");
var xhr = createXhr();
xhr.open("get","php/deleteUser.php?uid="+uid,false);
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
var resText = xhr.responseText;
console.log(resText);
if(resText == "1"){
console.log("删除成功" + num);

getPage(num);
}else if(resText == "0"){
console.log("无数据");
getPage(num);
}
}
}
xhr.send(null);
}

window.onload = function() {
getPage(1);
}
</script>
</body>
</html>

user.php
<?php

require("init.php");

@$currentPage = $_REQUEST["currentPage"];
if($currentPage == "" || $currentPage == null){
$currentPage = 1;
}
@$pageSize = $_REQUEST["pageSize"];
if($pageSize == "" || $pageSize == null){
$pageSize = 10;
}

$sql = "SELECT count(*) FROM xs_user";
$result = mysqli_query($conn,$sql);
$arr = mysqli_fetch_row($result);
$rowCount = $arr[0];
//echo $rowCount;
$totalPage = ceil($rowCount/$pageSize);
if($currentPage > $totalPage){
$currentPage = $totalPage;
}
if($currentPage < 1){
$currentPage = 1;
}

$start = ($currentPage - 1) * $pageSize;
$sql = "SELECT * FROM xs_user LIMIT $start,$pageSize";
$result = mysqli_query($conn,$sql);

if($result === false){
echo "异常,请检查SQL语句:";
echo $sql;
}else{
$count = mysqli_affected_rows($conn);
if($count == 0){
echo 0;
}else if($count > 0){
$arr = mysqli_fet
dbbb
ch_all($result,1);
if($result === false){
echo "异常,请检查SQL语句:";
echo $sql;
}else{
//var_dump($arr);
for($i=0;$i<count($arr);$i++){
$arr[$i]["reg_time"] = date("Y-m-d H:i",$arr[$i]["reg_time"]);
}
$lastStr = "{\"currentPage\":$currentPage,\"totalPage\":$totalPage}";
Array_push($arr,$lastStr);
$arr = json_encode($arr);
echo $arr;
}
}
}
?>

deleteUser.php
<?php

require("init.php");

@$uid = $_REQUEST["uid"];

$sql = "DELETE FROM xs_user WHERE uid = '$uid'";
$result = mysqli_query($conn,$sql);

if($result === false){
echo "异常,请检查SQL语句:";
echo $sql;
}else{
$count = mysqli_affected_rows($conn);
if($count == 0){
echo 0;
}else if($count > 0){
echo 1;
}
}
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: