您的位置:首页 > 编程语言 > PHP开发

ThinkPHP学习笔记(八)条件查询

2013-08-25 16:30 148 查看
一、建立项目与配置项目及数据库基本操作等内容:请参考《ThinkPHP学习笔记(一 ~ 七)》

二、查询模块实现:

效果图如下



1、首先在C层UserAction.class.php中添加public function search() {}方法

public function search() {
$m=M('User');
if(isset($_POST['username']) && $_POST['username']!=null);
{
$where['username']=array('like',"%{$_POST['username']}%");
}
if(isset($_POST['sex']) && $_POST['sex']!=null)
{
$where['sex']=array('eq',$_POST['sex']);
}

$arr=$m->where($where)->select();
$this->assign('data',$arr);
$this->display('index');//在User/index页面展示搜索结果,不在重新增加一个search.html页面
}


2、,并在V层添加查询表单:

<html>
<head>
<title>Index</title>
<script>
function jump() {
window.location="__URL__/add";
}
</script>
</head>

<body>
<form align='center' action='/ThPHP/index.php/User/search' method='post'>
用户名:<input type="text" name='username'/>
性别:<input type="radio" name='sex' value='1'>男 <input type="radio" name='sex' value='0'>女
<input type="submit" value='搜索'>
</form>
<table border='1' width='500' align='center'>
<tr>
<th>id</th>
<th>username</th>
<th>sex</th>
<th>操作</th>
</tr>
<volist name='data' id='vo'>
<tr>
<td><{$vo.id}></td>
<td><{$vo.username}></td>
<td><{$vo.sex}></td>
<td><a href="/ThPHP/index.php/User/del/id/<{$vo.id}>">删除</a> | <a href="/ThPHP/index.php/User/modify/id/<{$vo.id}>">修改</a></td>
</tr>
</volist>
</table>
<center>
<button onclick="jump()">添加</button>
</center>
</body>
</html>

这样就实现了搜索功能,打开http://localhost/ThPHP/index.php/User/  ,然后输入搜索条件,点击搜索,在User/index页面展示搜索结果($this->display('index');)

其他:order()、limit()

$arr=$m->order(arr('id'=>'desc','sex'=>'asc'))->limit(2,3)->select();按id降序,sex升序,第二个位置,去长度3的所有元素
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: