案例代码
逻辑层
if (!empty($title)) {
$condition[] = [ 'title', 'like', '%' . $title . '%' ];
}
$condition[] = [ 'delete', '=','0' ];
$order_by = 'addtime desc';
$field = 'id,title,wen_id,addtime,delete';
$res = $WenModel->gethuidaList($condition, $page_index, $page_size, $order_by,$field);
模型层
public function gethuidaList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'addtime desc',$field)
{
$totalCount = Db::name('wenda')->where( $condition)->count();
// 计算总页数
$page_count = ceil($totalCount / $page_size);
// 执行查询,这里没有实现分页逻辑,仅作为数据格式转换的示例
$list = Db::name('wenda')
->where( $condition)
->select()
->toArray();
// 构造与pageList相同的数据格式
$result = [
'page_count' => $page_count, // 总页数
'count' => $totalCount, // 总记录数
'list' => $list // 当前页的数据
];
return $this->success($result);
}
标签:count,title,list,DB,查询,page,condition,niushop From: https://www.cnblogs.com/79524795-Tian/p/18096870