gpt4 book ai didi

php - 如何在 PHP 中对 JSON 数据进行分页?

转载 作者:行者123 更新时间:2023-11-29 01:06:19 25 4
gpt4 key购买 nike

我无法从 PHP 直接访问数据库。如果是这样,我可以简单地完成分页。在这里,我向 PHP Web 服务发送 GET 请求,数据库的结果以 JSON 格式获取。我有一个表,我想在其中显示数据库值。由于数据库表包含超过1000条记录,我想分页显示数据。

$json_data_fromdb= httpGet($ur3l."fromtable?u=".$var1."&ip=".$var2);
$array = json_decode($json_data_fromdb, true);
$x=count($array['qqq']);
$array = $array['qqq'];

上面给出的是GET请求,对应的JSON存储在$array中。

如何使用 PHP 对 JSON 数组进行分页?

我的 JSON 数据是这样的:

{
"qqq": [
{
"a": "Conne",
"b": "1",
"c": "2014-05-19T15:40:06+05:30",
"d": {
"d1": "dani",

"d6": "admin"
}

},
{
"a": "igroup'",
"b": "1235",
"c": "2014-05-27T11:23:11+05:30",
"d": {
"d1": "sev",

"d6": "eev"
}

}
]
}

下面给出的是我的 HTML 表格

 <table id="show" >
<thead >
<tr >
<th>1stheader</th>
<th>2stheader</th>
<th>3stheader</th>
<th>4stheader</th>
<th>5stheader</th>
</tr>
</thead>
<tbody>
<?php
for($i=0; $i<$x; $i++)
{
$a= $array[$i]['a'];
$b= $array[$i]['d']['d1'] ;
$c= $array[$i]['d']['d2'] ;
$d= $array[$i]['b'];
$e= $array[$i]['c'];

?>
<tr >
<td><?php echo $a; ?></td>
<td><?php echo $b ?></td>
<td><?php echo c; ?></td>
<td><?php echo d; ?></td>
<td><?php echo $e; ?></td>
</tr>
<?php } ?>
</tbody>
</table>

最佳答案

如果你真的只想用 PHP 来做,你可以自己做。但我强烈建议使用 jquery 插件来进行分页以使其更容易。话虽这么说,如果你想自己卷起来,你可以做这样的事情(有点乱,但像这样。)考虑这个例子:

$sample_data = '{ "qqq": [ { "a": "Conne", "b": "1", "c": "2014-05-19T15:40:06+05:30", "d": { "d1": "dani", "d6": "admin" } }, { "a": "test1", "b": "1235", "c": "2014-    05-27T11:23:11+05:30", "d": { "d1": "sev", "d6": "eev" } }, { "a": "test2", "b": "1235", "c": "2014-05-27T11:23:11+05:30", "d": { "d1": "sev", "d6": "eev" } }, { "a": "test3", "b":     "1235", "c": "2014-05-27T11:23:11+05:30", "d": { "d1": "sev", "d6": "eev" } }, { "a": "test4", "b": "1235", "c": "2014-05-27T11:23:11+05:30", "d": { "d1": "sev", "d6": "eev" } }, { "a":     "test5", "b": "1235", "c": "2014-05-27T11:23:11+05:30", "d": { "d1": "sev", "d6": "eev" } }, { "a": "test6", "b": "1235", "c": "2014-05-27T11:23:11+05:30", "d": { "d1": "sev", "d6": "eev"     } }, { "a": "test7", "b": "1235", "c": "2014-05-27T11:23:11+05:30", "d": { "d1": "sev", "d6": "eev" } }, { "a": "test8", "b": "1235", "c": "2014-05-27T11:23:11+05:30", "d": { "d1": "sev",     "d6": "eev" } }, { "a": "test9", "b": "1235", "c": "2014-05-27T11:23:11+05:30", "d": { "d1": "sev", "d6": "eev" } }, { "a": "test10", "b": "1235", "c": "2014-05-27T11:23:11+05:30", "d": {     "d1": "sev", "d6": "eev" } }, { "a": "test11", "b": "1235", "c": "2014-05-27T11:23:11+05:30", "d": { "d1": "sev", "d6": "eev" } } ]}';

// just normal getting data
$raw_data = json_decode($sample_data, true);
$raw_data = $raw_data['qqq'];

// use get variable to paging number
$page = !isset($_GET['page']) ? 1 : $_GET['page'];
$limit = 5; // five rows per page
$offset = ($page - 1) * $limit; // offset
$total_items = count($raw_data); // total items
$total_pages = ceil($total_items / $limit);
$final = array_splice($raw_data, $offset, $limit); // splice them according to offset and limit

?>
<!-- print links -->
<?php for($x = 1; $x <= $total_pages; $x++): ?>
<a href='index.php?page=<?php echo $x; ?>'><?php echo $x; ?></a>
<?php endfor; ?>
<table border="1" cellpadding="10">
<tr><th>Column 1</th><th>Column 2</th><th>Time</th><th>Column 4</th></tr>
<?php foreach($final as $key => $value): ?>
<tr>
<?php foreach($value as $index => $element): ?>
<td><?php echo !is_array($element) ? $element : implode(',', $element); ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>

Just a sample

关于php - 如何在 PHP 中对 JSON 数据进行分页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23909250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com