1.首先这是两个表,一个博文表 ex_blog 一个博文分裂表 ex_blog_type
2.控制器中的方法名为 actionList(),下面就是代码了。
<img src="https://img-blog.csdn.net/20160728082045023?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
<pre name="code" class="php"> /*
* 博文列表页
*/
public function actionList(){
//查询出分类
$blog_type = Blog_type::find()->asArray()->all();
//判断是否是POST提交的数据、如果是POST提交则 执行搜索 否则执行渲染模板
$request = \Yii::$app->request;
if($request->isPost){
//接收要搜索的数据
$title = \Yii::$app->request->post('title');
$author = \Yii::$app->request->post('author');
$type_name = \Yii::$app->request->post('type_name');
//判断搜索的字段是否为空 不为空则 拼接SQL 语句
$sql = "select * from ex_blog INNER JOIN ex_blog_type ON ex_blog.type_id=ex_blog_type.type_id where 1";
if(!empty($title)){
$sql .= " and title like '%$title%'";
}
if(!empty($author)){
$sql .= " and author like '%$author%'";
}
if(!empty($type_name)){
$sql .= " and type_name like '%$type_name%'";
}
//执行SQL语句查询数据信息
$blogInfo = Blog::findBySql($sql)->asArray()->all();
//搜索后关键字标红
foreach($blogInfo as $key => $value){
$blogInfo[$key]['title'] = str_replace($title,"<font color='red'>$title</font>",$value['title']);
$blogInfo[$key]['author'] = str_replace($author,"<font color='red'>$author</font>",$value['author']);
$blogInfo[$key]['type_name'] = str_replace($type_name,"<font color='red'>$type_name</font>",$value['type_name']);
}
//将数据转换为JSON返回
echo json_encode($blogInfo);
}else{
/*$query = (new \yii\db\Query())->from('ex_blog')->innerJoin('ex_blog_type','ex_blog.type_id=ex_blog_type.type_id');
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count(),'pageSize'=>3]);
$blogInfo = $query->offset($pages->offset)->limit($pages->limit)->all();*/
//查询出所有的数据信息
$blogInfo = (new \yii\db\Query())->from('ex_blog')->innerJoin('ex_blog_type','ex_blog.type_id=ex_blog_type.type_id')->all();
//渲染模板
return $this->renderPartial('list', [
'blog' => $blogInfo,
'blog_type' => $blog_type,
]);
}
}
3.模板页面的代码 如下
<script>
function search(){
//获取搜索的数据
var title = $("#title").val();
var author = $("#author").val();
var type_name = $("#type_name").val();
//发送AJAX请求
$.ajax({
url:"?r=index/list",
data:{title:title,author:author,type_name:type_name},
type:"POST",
success:function(msg){
//将JSON数据转换为对象
obj = eval("("+msg+")");
var tr = "";
for(var i in obj){
tr += "<tr class='content'>"
tr += "<td>"+obj[i].id+"</td>";
tr += "<td>"+obj[i].title+"</td>";
tr += "<td>"+obj[i].author+"</td>";
tr += "<td>"+obj[i].type_name+"</td>";
tr += '<td><img src='+obj[i].pic+' alt="封面" width="50" height="50"></td>';
tr += "<td>"+obj[i].time+"</td>";
tr += "<td> <a href=''>删除</a> | <a href=''>编辑</a> </td>";
tr += "</tr>"
}
//将原有的内容移除
$(".content").remove();
//将返回的数据追加到原来位置
$("#tr").after(tr);
}
});
}
</script>
4.搜索的 table 表格
<table align="center" width="800" style="text-align: center">
<tr>
<td colspan="2">标题:<input type="text" id="title"></td>
<td colspan="2">作者:<input type="text" id="author"> </td>
<td colspan="2">
类型:<select id="type_name">
<option value="">--请选择--</option>
<?php foreach($blog_type as $val){ ?>
<option value="<?=$val['type_name'];?>"><?=$val['type_name'];?></option>
<?php } ?>
</select>
</td>
<td><input type="button" value="搜一搜" οnclick="search()"></td>
</tr>
</table>