1.普通关键词搜索框
模板部分代码:
<form name=‘searchform‘ action=‘/index.php/module/controller/search‘ method=‘get‘>
<input name=‘keyword‘ type=‘text‘ value=‘<?php echo input(‘get.keyword‘);?>‘ class=‘input-text‘ />
<input type=‘submit‘ name=‘search‘ class=‘button‘ vlalue=‘<?php echo lang(‘search‘)?>‘ />
</form>
<div class=‘‘>相似用户名:{$list[‘name‘];}</div>
控制器部分代码:
function search(){
$keyword = isset($_GET[‘keyword‘])?$_GET[‘keyword‘] : ‘‘;
if($keyword){
$where[‘name‘] = [‘like‘,‘%‘.$keyword.‘%‘];
}
$list = Db::name(‘datatable‘)->where($where)->order(‘id DESC‘)->select();
$this->assign(‘list‘,$list);
return $this->fetch(‘‘);
}
2.有关键词提示的输入框
模板部分代码:
<input type=text id=txtkey>
<ul class=‘list‘></ul>
<script>
$(‘#txtkey‘).on(‘keyup‘,function(){
var key = $(this).val();
$(‘.list‘).empty();
showmodLoading();
var s=‘‘;
$.get(‘/index.php/module/controller/method‘,{key:encodeURI($(‘#txtkey‘).val())},function(data){
if(data.data){
$.each(data.data,function(k,v){
s+=‘<li data-id="‘+v.userid+‘" onclick="selitem(\‘‘+v.userid+‘\‘,\‘‘+v.nickname+‘\‘)">‘+v.nickname+‘</option>‘;
})
}
$(‘.list‘).append(s);
$(‘.list‘).show();
modLoaded();
$(document).one(‘click‘,function(){
$(‘.list‘).hide();
})
})
});
function selitem(id,name)
{
$(‘#ry‘).append(‘<option value="‘+id+‘">‘+name+‘</option>);
}
$(‘#dosubmit‘).on(‘click‘,function(){
$(‘#ry option‘).each(function(){
$(this).attr(‘selected‘,‘true‘);
});
});
</script>
控制器部分代码:
public function search()
{
$key = input(‘get.key‘);
$datas = Db::name(‘table‘)->field(‘name‘)->where(array(‘nickname|username‘=>array(‘like‘,‘%‘.$key.‘%‘)))->select();
return (array(‘status‘=>1,‘data‘=>datas));
}