zoukankan      html  css  js  c++  java
  • php处理分页数据并返回json

    <?php
    header('content-type:application/json;charset=utf-8');//设置浏览器解析格式为json
    header('Access-Control-Allow-Origin: *');
    $curPage = $_GET['page'];//前台传递的当前页
    $pageSize = 20;//页容量大小
    $start = ($curPage - 1) * $pageSize;//起始页数
    $totalPage = 0;//总页数
    $conn = mysqli_connect("localhost", "root", "root","s");//数据库连接参数
    $pageList = array();//用来封装分页数据的数组
    	
    //执行分页查询的同时在数据库中设置一个新字段,总记录数
    $sql="SELECT * ,(select count(*) FROM s) as total from s limit $start,$pageSize";
    $result = mysqli_query($conn, $sql);
    	
    while ($row = mysqli_fetch_assoc($result)) {
    	$items = array(
    		"itemid" => $row['itemid'],
    		"itemtitle" => $row['itemtitle'],
    	    "image" => $row['itempic'],
    	    "price" => $row['orderfee']//
    	    );
    	    //总页数等于总记录数/页容量,向上取整, 如3.5页,要有第四页
    	    $totalPage = ceil($row['total'] / $pageSize);
    	    array_push($pageList, $items);//填充分页数据
    	}
    mysqli_close($conn);//关闭连接
    echo json_encode($pageList,JSON_UNESCAPED_UNICODE);
    ?>
    

      

  • 相关阅读:
    bzoj2733: [HNOI2012]永无乡
    bzoj3141: [Hnoi2013]旅行
    bzoj3144: [Hnoi2013]切糕
    bzoj3140: [Hnoi2013]消毒
    bzoj3139: [Hnoi2013]比赛
    bzoj3142: [Hnoi2013]数列
    bzoj3572: [Hnoi2014]世界树
    bzoj2286: [Sdoi2011]消耗战
    bzoj3611: [Heoi2014]大工程
    The Unsolvable Problem
  • 原文地址:https://www.cnblogs.com/zbl3033/p/13363809.html
Copyright © 2011-2022 走看看