【问题标题】:Android Paginate JSON Data from PHP Mysql to AppAndroid Paginate JSON 数据从 PHP Mysql 到 App
【发布时间】:2020-07-17 02:08:45
【问题描述】:

以下 PHP 代码从 MySQL 数据库中获取所有数据并将其发送到 Android 应用程序。我希望对数据进行分页。

所有数据 PHP 代码

<?php

include 'dbconfig.php';
try {
    $conn = new PDO("mysql:host=$HostName;dbname=$DatabaseName", $HostUser, $HostPass);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT * FROM `tiffa`");
    $stmt->execute();
    
    $data = array();
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $data[] = $row;
    }
    header('Content-Type:Application/json');
    echo json_encode($data);
} catch (PDOException $e) {
    print "Connection failed! Please Try Again Or Contact Us: " . $e->getMessage() . "<br/>";
    die();
    $conn = null;
}

POJO/数据模型类

public class ImageList {
    @SerializedName("image1name")
    private String name;
    @SerializedName("county")
    private String county;
    @SerializedName("image1URL")
    private String imageurl;
    @SerializedName("image2URL")
    private String image2url;
  
    public ImageList(String name,String county,String imageurl, String image2url) {
        this.name = name;
        this.county = county;
        this.imageurl = imageurl;
        this.image2url = image2url;
    }
    public String getName() {
        return name;
    }
      String getCounty() {
       return county; 
    }
       String getImageurl() {
        return imageurl;
    }
     String getImage2url() {
        return image2url;
    }    
}

我试图传递 page_number 和 item_count(来自应用程序),但我似乎无法得到它。这是我尝试过的 PHP 代码。 POJO 保持不变。

<?php

$page_number = $_GET['page_number'];
$item_count = $_GET['item_count'];
$from = $page_number * $item_count - ($item_count - 1);
$to = $page_number * $item_count;
$data = array();

include 'dbconfig.php';
try {
    $conn = new PDO("mysql:host=$HostName;dbname=$DatabaseName", $HostUser, $HostPass);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT * FROM `tiffa`");
    $stmt->execute();

    if ($to > $stmt) {
        array_push($response, array('status' => 'end'));
        echo json_encode($response);
    } else {
        $data = array();
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $data[] = $row;
        }
        header('Content-Type:Application/json');
        echo json_encode($data);
    }
    array_push($response, array('images' => $images));
    sleep(2);
    echo json_encode($response);
catch (PDOException $e) {
    print "Connection failed! Please Try Again Or Contact Us: " . $e->getMessage() . "<br/>";
    die();
    $conn = null;
}

【问题讨论】:

  • stackoverflow.com/questions/44792931/… 可能会给你一些想法。主要是围绕在你的SQL语句中使用LIMIT。
  • 感谢@NigelRen。让我看看它并恢复。
  • 您在此 PHP 代码中有语法错误。完全删除 catch 块。

标签: php android mysql pagination pojo


【解决方案1】:

感谢@Nigel Ren 提供的线索。我解决了这个问题

<?php
$page_number = $_GET['page_no'];
$item_count = $_GET['item_cnt'];
$from = $page_number*$item_count - ($item_count-1);
$to = $page_number*$item_count;
$response=array();
$stats=array();
include 'dbconfig.php';
// Create connection
$conn = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);
    
$total = mysqli_num_rows(mysqli_query($conn, "SELECT id from db1"));
if($to>$total)
{
array_push($response,array('status'=>'end'));
echo json_encode($response);
}
else
{
array_push($response,array('status'=>'ok'));
$count = $from;
$images = array();
$start = ($page - 1) * $limit; 
//SQL query to fetch data of a range 
$sql = "SELECT * from db1 limit $start, $item_count";
//Getting result 
$result = mysqli_query($conn,$sql); 
//Adding results to an array 
$res = array(); 
while($row = mysqli_fetch_array($result))
{
$image122 = $row['image122'];
$image_path = $image122;
array_push($images,array('id'=>$count,'image_path'=>$image_path));
$count = $count+1;
}
array_push($response,array('images'=>$images));
sleep(2);
echo json_encode($response);
}

?>

【讨论】:

    猜你喜欢
    • 2013-05-17
    • 1970-01-01
    • 2014-10-23
    • 2018-03-09
    • 2019-03-04
    • 2016-09-22
    • 2016-09-06
    • 2013-06-23
    • 2012-01-22
    相关资源
    最近更新 更多