欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Tp5 前后端分离 模糊查询

程序员文章站 2022-06-07 15:54:52
...

一、模糊查询

正常的查询:

function searchList( pi, ps, keyword) {
	var queryData = {
		page:pi,
		size:ps,
		keyword:keyword
	};
	$.ajax({
		url: 'http://xxx.com/',
		data: queryData,
		dataType: 'json',
		success: function (obj) {
			console.log(obj);
		}
	});
}
searchList(1, 10, null);

输入关键词的查询:

//->搜索用户信息
$(document).on('click', '#searchBtn', function () {
	var keyword = $('input[name="keyword"]').val();
	if (keyword == "" || keyword == null) {
		alert('请输入搜索关键词');
		return false;
	}
	//->重新查询
	searchList(1, 10, keyword);
});

在控制器User.php:

//->得到所有用户,进行分页
public function getUserTotal($page=1, $size=10, $keyword=null) {
	$pagingOrders = ReportUserModel::getUserByPage($page, $size, $keyword);
	return $pagingOrders;
	if ($pagingOrders->isEmpty()) 
	{
		return [
			'current_page' => $pagingOrders->currentPage(),
			'data' => []
		];
	}
	$data = $pagingOrders->toArray();
	return [
		'current_page' => $pagingOrders->currentPage(),
		'data' => $data
	];
}

在模型ReportUser.php:

//->得到所有用户,分页
public static function getUserByPage($page=1, $size=10, $keyword=null) {
	if ($keyword != null || $keyword != "") {
		//->模糊查询
		$where['acc|name|email|phone|idcard'] = array('like', '%' .$keyword. '%');
		//->分页
		$pagingData = Db::table('daichao_report_user')->where($where)->order('id desc')->paginate($size, false, ['page' => $page]);
		return $pagingData;
	} else {
		//->分页
		$pagingData = Db::table('daichao_report_user')->order('id desc')->paginate($size, false, ['page' => $page]);
		return $pagingData;
	}		
}

语法:

//->模糊查询
$where['acc|name|email|phone|idcard'] = array('like', '%' .$keyword. '%');
//->分页
$pagingData = Db::table('daichao_report_user')->where($where)->order('id desc')->paginate($size, false, ['page' => $page]);
相关标签: 模糊查询