ThinkPhp学习10

时间:2023-12-09 23:04:01

原文:ThinkPhp学习10

查询操作

Action模块 User下的search

 public function search(){
//判断username是否已经传入,且不为空
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']);
}
$m=M('User');
//类似SELECT * FROM `tp_user` WHERE ( `username` LIKE '%李%' ) AND ( `sex` = '0' )
$data=$m->where($where)->select();
if($data==null){
$this->error("没有查询到结果!",'index');
}
else{
$this->assign('data',$data);
$this->display('index');
}
}

User对应的index.html模板

 <div style="width:400px;margin:0 auto;">
<form action="__URL__/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='400px' align='center'>
<tr>
<th>Id</th>
<th>User</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='/thinkphp/test/index.php/User/del/id/<{$vo.id}>'>删除</a>|<a href='/thinkphp/test/index.php/User/modify/id/<{$vo.id}>'>修改</a></td>
</tr> </volist>
</table>
<input type="button" value="添加" onclick="jump()"/>
</div>

ThinkPhp学习10