<?php foreach ($data as $id => $v) {
$tpl = ' <li> <h3>%s</h3> <h2><a href="%d.html" target="_blank">%s</a></h2></li> ';
echo sprintf($tpl, $v['date'], $id, $v['title']);
} ?>
1
qiayue 2018-02-20 09:51:20 +08:00 1
不要一次把数据全部取出来,每页想显示多少条就取多少条,第几页就偏移多少
|
2
zyqf 2018-02-20 10:26:46 +08:00 via Android
Mysql limit offset
|
3
zyqf 2018-02-20 10:28:30 +08:00 via Android 1
对不起,没审题,自己在 PHP 里写个偏移方法
|
4
liuhaotian 2018-02-20 11:01:07 +08:00 1
array_slice: http://php.net/manual/en/function.array-slice.php
|
5
phpok 2018-02-20 11:05:14 +08:00 1
数据都出来了,将一维数组割成多个数组,用 array_chunk,然后下标从 0 开始表示第一页就是了~
|
6
xiamuguizhi OP @phpok 能写下代码? php 新手 纠结中!!!!
|
7
des 2018-02-20 11:49:16 +08:00 via Android 1
|
8
phpok 2018-03-01 23:03:34 +08:00
@xiamuguizhi
代码如下: ``` php $list = array(1,2,3,4,5,6,7,8,9); $mlist = array_chunk($list,4); //看分出了多少 echo "<pre>".print_r($mlist,true)."</pre>"; //计算共有几页 $totalpage = count($mlist); $nowpage = isset($_GET['pageid']) ? intval($_GET['pageid']) : 1; if(!$nowpage){ $nowpage = 1; } $position = $nowpage - 1; $prevpage = $nowpage > 1 ? "?pageid=".($nowpage-1) : '?pageid=1'; $nextpage = ($nowpage + 1) > $totalpage ? '?pageid='.$totalpage : '?pageid='.($nowpage + 1); $list = $mlist[$position]; echo "<pre>".print_r($list,true)."</pre>"; echo 'Toal: '.$totalpage; echo ' <a href="'.$prevpage.'">Prev</a> '; echo ' <a href="'.$nextpage.'">Next</a> '; ``` |