數據表結構:
表名:test
id | name | age |
1 | 胡大 | 21 |
2 | 王二 | 22 |
3 | 張三 | 23 |
4 | 李四 | 24 |
5 | 孫五 | 25 |
6 | 錢六 | 26 |
7 | 牛七 | 27 |
8 | 周八 | 28 |
9 | 鄭九 | 29 |
10 | 趙屎 | 30 |
PHP代碼(test.php):
<?php
header("Content-type: text/html; charset=utf-8");
//引入medoo
require_once 'medoo.php';
use Medoo\Medoo;
$page = ($_GET["page"]-1)* $_GET["limit"];//數組以0起始
$limit = $_GET["limit"];
$id = $database->select("test","id", ["LIMIT" => [$page , $limit]]);//根據分頁獲取ID
$list = $database->select("test","*", ["id" => $id]);//請求數據庫數據
//加工:
print_r($list); //輸出數組
echo "<hr/>";
//循環遍歷
foreach($list as $key=>$value){
extract($list[$key]);
echo "ID:".$id."姓名:".$name."值:".$age."<br/>";
}
?>
使用方法:
http://localhost/test.php?page=1&limit=5
顯示效果:
Array ( [0] => Array ( [id] => 1 [name] => 胡大 [age] => 21 ) [1] => Array ( [id] => 2 [name] => 王二 [age] => 22 ) [2] => Array ( [id] => 3 [name] => 張三 [age] => 23 ) [3] => Array ( [id] => 4 [name] => 李四 [age] => 24 ) [4] => Array ( [id] => 5 [name] => 孫五 [age] => 25 ) )
ID:1姓名:胡大值:21
ID:2姓名:王二值:22
ID:3姓名:張三值:23
ID:4姓名:李四值:24
ID:5姓名:孫五值:25
http://localhost/test.php?page=2&limit=5
Array ( [0] => Array ( [id] => 6 [name] => 錢六 [age] => 26 ) [1] => Array ( [id] => 7 [name] => 牛七 [age] => 27 ) [2] => Array ( [id] => 8 [name] => 周八 [age] => 28 ) [3] => Array ( [id] => 9 [name] => 鄭九 [age] => 29 ) [4] => Array ( [id] => 10 [name] => 趙屎 [age] => 30 ) )
ID:6姓名:錢六值:26
ID:7姓名:牛七值:27
ID:8姓名:周八值:28
ID:9姓名:鄭九值:29
ID:10姓名:趙屎值:30
配合layui實現分頁:
<?php
header("Content-type: text/html; charset=utf-8");
//引入medoo
require_once 'medoo.php';
use Medoo\Medoo;
$out= array('code' =>'0' ,"msg"=>"","count"=>"","data"=>"" );//定義返回數組
$page = ($_GET["page"]-1)* $_GET["limit"];//數組以0起始
$limit = $_GET["limit"];
$id = $database->select("test","id", ["LIMIT" => [$page , $limit]]);//根據分頁獲取ID
$list = $database->select("test","*", ["id" => $id]);//請求數據庫數據
$out["count"]=$database->count("test");
$out["data"] = $list;
echo json_encode($out);
?>
效果: