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

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" οnclick="getPage(1)">首页</a>
						</td>
						<td>
							<a class="tfoot_a" οnclick="prePage()">上一页</a>
						</td>
						<td>
							<a class="tfoot_a" οnclick="nextPage()">下一页</a>
						</td>
						<td>
							<a class="tfoot_a" οnclick="lastPage()">尾页</a>
						</td>
						<td>
							<p>
								转到<input type="text" id="in_pNum">页
								<button οnclick="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\" οnclick=\"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_fetch_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;
		}
	}
?>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值