1.首先創建一個model
指定表主鍵
指定表名
model裏的代碼
<?php
namespace app\common\model;
use think\Model;
class day3 extends Model
{
//主鍵
protected $pk ="id";
//表名
protected $table = "tp_articles";
}
2.創建一個對應的controller
對應controller的代碼
<?php
namespace app\index\controller;
use think\Controller;
use app\common\model\day3;
use think\Db;
use think\db\Query;
use think\db\Where;
class day3Class extends Controller
{
//分頁方法
public function page(){
//查詢tp_articles的數據 10條一頁
$data = day3::name("tp_articles")->paginate(10);
//跳轉到view里名字叫page的html頁面
return view("index/page",compact("data"));
}
}
對應view裏的html頁面代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>明天會更好</title>
<link rel="stylesheet" href="/static/css/bootstrap.css"/>
<link rel="stylesheet" href="/static/css/bootstrap-theme.css"/>
</head>
<body>
<table class="table table-responsive">
<thead>
<tr>
<th>id</th>
<th>標題</th>
<th>描述</th>
<th>內容</th>
<th>操作</th>
</tr>
</thead>
{foreach $data as $item}
<tbody>
<tr>
<td>{$item.id}</td>
<td>{$item.title}</td>
<td>{$item.desn}</td>
<td>{$item.body}</td>
<td>
<div class="btn-group">
<button type="button" class="btn btn-group-xs btn-default">修改</button>
<button type="button" class="btn btn-group-xs btn-danger">刪除</button>
</div>
</td>
</tr>
</tbody>
{/foreach}
</table>
{$data|raw}
<script src="/static/jquery.min.js"></script>
<script src="/static/js/bootstrap.js"></script>
<script src="/static/js/npm.js"></script>
</body>
</html>
展示效果圖👇