public function index()
{
if ($this->request->isAjax())//判断是否请求isAjax()方法,限制只有ajax请求时才去查询数据
{
//list构造参数(查询条件,主键,排序规则,选择页数后覆盖的数据,页面显示的条数)= 调用基类中的buildparams()方法,去生成查询所需条件
//位置application/common/controller/Backend.php
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
//搜索数据条数$total,搜索具体数据$list
//$this->model:这个模型对应数据库
//with:with的参数可以是字符串,表示单个关联查询,也可以是数组,表示多个关联查询。如果你还额外加了些统计查询,一定不要忘了把with也加上
//where:表示查询条件
//order:表示以排序字段$sort作为查询字段(这里是主键id),并以查询规则为$order(例如desc,asc)的顺序进行查询
//count:表示查询的数据有多少条
//limit:表示从起始值为$offset开始,每页显示$limit条数据
//select:表示对数据进行查询
//具体参数信息见application/common/controller/Backend.php中的buildparams()方法
$total = $this->model
->with(["category"])
->where($where)
->order($sort, $order)
->count();
$list = $this->model
->with(["category"])
->where($where)
->order($sort, $order)
->limit($offset, $limit)
->select();
//将$total的值存入数组中的"total"中,将$list的值存入数组中的"rows"中,并将数组赋值给$result
$result = array("total" => $total, "rows" => $list);
//将$result以json的格式返回
return json($result);
}
//表示调用\thinkphp\library\think\view.php的fetch()方法,作用:解析和获取模板内容 用于输出
return $this->view->fetch();
}
数组使用:
例子:
下方$result为数组信息

public function test()
{
$data = Db::table('customers')
->where([
'customer_code' =>['like','吴江'.'%']//当栏位customer_code的值为吴江XXX的值
])
->column('customer_code');//查询栏位customer_code这一行的数据
$result = array("total"=>1,"rows"=>$data);//定义数组
return json($result);//输出结果
}
返回结果为
