mysql+php+ajax实现分页查询(JSON拼接)

前端:

common.js

function $(id) {
	return document.getElementById(id);
}
function createXhr(){  
    var xhr;  
    if(window.XMLHttpRequest){  
        xhr = new XMLHttpRequest();  
    }else{  
        xhr = new ActiveXObject("Microsoft XMLHttp");  
    }  
    return xhr;  
}
handle.js

var currentPage = 1;
		var totalPage = 1;
		
		function getPage(num) {
			console.log("getPage"+num);
			var xhr = createXhr();
			xhr.open("get","php/fenye.php?currentPage="+num,true);
			console.log("11111");
			xhr.onreadystatechange = function() {
				if(xhr.readyState == 4 && xhr.status == 200){
					var resText = xhr.responseText;
					console.log(resText);
					var arr = JSON.parse(resText);
					var n = 1;
					for(var i=0;i<arr.length-1;i++){
						console.log(arr[i].cla_path);
						$("d2_img"+n).src = "../admin/"+arr[i].cla_path;
						++n;
					}
					console.log(arr[arr.length-1]);
					var tmp = parseInt(arr[arr.length-1]);
					totalPage = tmp;
				}
			}
			xhr.send(null);
		}

		function prePage() {
			if(currentPage == 1){
				return;
			}else{
				getPage(currentPage - 1);
				currentPage -= 1;
			}
		}

		function nextPage() {
			if(currentPage == totalPage){
				return;
			}else{
				getPage(currentPage + 1);
				currentPage += 1;
			}
		}

		window.onload = function() {
			getPage(1);
		}
后端:

fenye.php

<?php 
	
	header("Content-Type:application/json");

	require("init.php");

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

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

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

	if($result === false){
		die("异常,请检查SQL语句:");
		echo $sql;
	}else{
		$sql = "SELECT count(*) FROM xs_classic";
		$result1 = mysqli_query($conn,$sql);
		$rows = mysqli_fetch_row($result1);
		$rowCount = $rows[0];
		$totalPage = ceil($rowCount / $pageSize);
		$lastStr = "{\"totalPage\":$totalPage}";
		$arr = mysqli_fetch_all($result,1);
		Array_push($arr,$lastStr);
		$arr = json_encode($arr);
		echo $arr;
	}
?>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值