ThinkPHP文档参考地址:https://www.shouce.ren/api/view/a/12704
模型方法实现分页
得创建一个model 名字和数据库表 去除前缀后的名字一样
比如 数据库表 叫tp_goods
model名字得叫Goods
数据库表前缀 在database.php里配置
跳转首页的控制器里:
public function index()
{
//分页查询 每页10条
$goods = \app\admin\model\Goods::paginate(10);
return view('index',['g' => $goods]);
}
index.html 视图页面
<div class="well">
<!-- table -->
<table class="table table-bordered table-hover table-condensed">
<thead>
<tr>
<th>编号</th>
<th>商品名称</th>
<th>商品价格</th>
<th>商品数量</th>
<th>商品logo</th>
<th>添加时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{foreach $g as $item}
<tr class="success">
<td>{$item.id}</td>
<td><a href="javascript:void(0);">{$item.goods_name}</a></td>
<td>{$item.goods_price}</td>
<td>{$item.goods_number}</td>
<td><img src="/static/admin/img/goods01_thumb.jpg"></td>
<td>{$item.update_time}</td>
<td>
<a href="{:url('admin/goods/edit',['id'=> $item.id])}"> 编辑 </a>
<a href="#" onclick="if(confirm('确认删除?')) location.href='{:url(\'admin/goods/delete\',[\'id\'=>$item.id])}'"> 删除 </a>
</td>
</tr>
{/foreach}
</tbody>
</table>
<!-- pagination -->
<div class="">
{$g->render()}
</div>
</div>
关键代码:
修改分页 样式:
<style type="text/css">
.pagination li{list-style:none;float:left;margin-left:10px;
padding:0 10px;
background-color:#ffffff;
border:1px solid #ccc;
height:26px;
line-height:26px;
cursor:pointer;color:#fff;
}
.pagination li a{color: #bbafaf;padding: 0;line-height: inherit;border: none;}
.pagination li a:hover{background-color: #5a98de;}
.pagination li.active{background-color:#5a98de; color: #ffffff;}
.pagination li.disabled{background-color: #ffffff;color:gray;}
</style>
效果图: