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

来源:互联网 发布:产品过程矩阵 编辑:程序博客网 时间:2024/05/27 00:41

前端:

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;}?>




原创粉丝点击