包含少量页面后,JSON分页空白

时间:2017-04-25 22:05:24

标签: php json pagination limit offset

的config.php

<?php
     define('HOST','localhost');
     define('USER','root');
     define('PASS','mypass');
     define('DB','db_news');

     $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
?>

Listnews.php

<?php 
include'con_db.php';

if (isset($_GET['page']) && is_numeric($_GET['page'])) { 
    $page= (int) $_GET['page'];
} else {
    $page= 1;  
}

$all_row = "SELECT COUNT(*) FROM berita";
$data_page = mysqli_query($con, $all_row);  
$r = mysqli_fetch_row($data_page);

$numrows = $r[0];
$rowsperpage = 10;

$totalpages = ceil($numrows / $rowsperpage);

//if current page is greater than total pages
if ($page > $totalpages) {
    //set current page to last page
    $page= $totalpages;
}

//if current page is less than first page
if ($page < 1) {
    //set current page to first page
    $page= 1;
}

// the offset of the list, based on current page
$offset = ($page - 1) * $rowsperpage;

$query = "SELECT * FROM berita order by id_berita DESC LIMIT $offset, $rowsperpage";
$result = mysqli_query($con, $query);
if(mysqli_num_rows($result)>0) {
        $response["list_data"] = array();
        while ($row = mysqli_fetch_assoc($result)) {
            $temp = array("id" => $row["id_berita"],
                        "judul" => $row["judul"], 
                        "isi" => $row["isi_berita"],
                        "tgl" => $row["tanggal"].' '.$row["jam"],
                        "dibaca" => $row["dibaca"].' Dibaca',
                        "url_berita" => $row["judul_seo"],
                        "gambar" => $row["gambar"]);
            array_push($response["list_data"], $temp);
        }

        if($result){
            $response["success"] = 1;
            $response["message"] = "Successfully Displayed";
            $response["page"] = $page;
            $response["total_pages"] = $totalpages;
            $data = json_encode($response);
            echo $data;
        }
        else{
            $response["success"] = 0;
            $response["message"] = "Try Again";
            $response["page"] = $page;
            $response["total_pages"] = $totalpages;
            $data = json_encode($response);
            echo $data;
        }
}else{
    $response["success"] = 2;
    $response["message"] = "No Details Found";
    $response["page"] = $page;
    $response["total_pages"] = $totalpages;
    $data = json_encode($response);
    echo $data;
}

/*
**$data = json_encode($response);
**echo $data;
*/
?>

用postman myhost / news / berita.php测试后?页面= 1,2,3,4,5,6显示了json数据。 showing json data with page 1-6

然后我将第7页myhost / news / berita.php?page = 7包含在空白处 after incluse page 7

先生,请帮助我,我很抱歉我的语言不好

1 个答案:

答案 0 :(得分:0)

终于完成了我的问题,只需添加utf8ize函数

function utf8ize($d) {
if (is_array($d)) {
    foreach ($d as $k => $v) {
        $d[$k] = utf8ize($v);
    }
} else if (is_string ($d)) {
    return utf8_encode($d);
}
return $d;

}

并将其用于json_encode

$response["success"] = 1;
$response["message"] = "Successfully Displayed";
$response["page"] = $halaman;
$response["total_pages"] = $totalpages;
$data = json_encode(utf8ize($response));
echo $data;