本文实例讲述了Yii框架结合sphinx,Ajax实现搜索分页功能的方法。分享给大家供大家参考,具体如下:
效果图:
控制器:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
<?php
namespace backend\\controllers;
use Yii;
use yii\\web\\Controller;
use yii\\data\\Pagination;
use SphinxClient;
use yii\\db\\Query;
use yii\\widgets\\LinkPager;
use backend\\models\\Goods;
class SouController extends Controller
{
//显示搜索页面
public function actionIndex()
{
//接受搜索值
$sou =Yii:: $app ->request->get( 'sou' );
$p1 =Yii:: $app ->request->get( 'p1' );
$p2 =Yii:: $app ->request->get( 'p2' );
//echo $sou.$p1.$p2;die;
//sphinx搜索
$cl = new SphinxClient();
$cl -> SetServer( '127.0.0.1' ,9312);
$cl -> SetConnectTimeout(3);
$cl -> SetArrayResult(true);
if ( $sou )
{
//只搜索条件
$cl -> SetMatchMode(SPH_MATCH_ANY);
}
else
{
//全局扫描
$cl -> SetMatchMode(SPH_MATCH_FULLSCAN);
}
//设置价格(注意:创建索引时,价格属性定义为int)
if ( $p1 && $p2 )
{
$cl ->SetFilterRange( 'price' , $p1 , $p2 );
}
//搜索查询关键字
$res = $cl ->Query( $sou , "mysql_goods" );
//ajax分页
$model = new Goods();
foreach ( $res [ 'matches' ] as $key => $val )
{
$ids [] = $val [ 'id' ];
}
//查询条件数据
$query = $model ->find()->where([ 'id' => $ids ]);
$countQuery = clone $query ;
$pages = new Pagination([ 'totalCount' => $countQuery -> count (), 'defaultPageSize' =>3]);
//分页
$models = $query ->offset( $pages ->offset)
->limit( $pages ->limit)
->all();
//关键字变红
foreach ( $models as $k => $v )
{
$models [ $k ][ 'goods_name' ]= str_replace ( "$sou" , "<font color='red'>$sou</font>" , $v [ 'goods_name' ]); //将关键字替换成红色字体
}
//显示列表,分配数据
return $this ->render( 'index' , [
'res' => $models ,
'pages' => $pages ,
'sou' => $sou ,
'p1' => $p1 ,
'p2' => $p2
]);
}
}
?>
|
视图层:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
<?php
use yii\\helpers\\Html;
use yii\\widgets\\ActiveForm;
use yii\\widgets\\LinkPager;
$form = ActiveForm::begin([
'action' => 'index.php?r=sou/index' ,
'method' => 'get'
]) ?>
<center>
<div id= "list" >
商品名称:
<input type= "text" name= "sou" value= "<?php echo $sou?>" >
价格区间:
<input type= "text" name= "p1" value= "<?php echo $p1?>" >---<input type= "text" name= "p2" value= "<?php echo $p2?>" >
<input type= "submit" value= "搜索" >
<table border= "1" style= "width:500px;" >
<tr>
<th>ID</th>
<th>商品名称</th>
<th>商品价格</th>
</tr>
<?php foreach ( $res as $key => $v ){?>
<tr>
<td><?php echo $v [ 'id' ];?></td>
<td><?php echo $v [ 'goods_name' ];?></td>
<td><?php echo $v [ 'price' ];?></td>
</tr>
<?php }?>
</table>
<!--分页-->
<?= LinkPager::widget([ 'pagination' => $pages ]) ?>
</div>
</center>
<?php ActiveForm:: end () ?>
<!--显示-->
<?php $this ->beginBlock( 'test2' ) ?>
$(document).on( 'click' , '.pagination a' , function (e)
{
//阻止page显示,看地址
e.preventDefault();
var href = $(this).attr( 'href' );
$.post(href, function (msg){
$( '#list' ).html(msg);
})
});
<?php $this ->endBlock();
$this ->registerJs( $this ->blocks[ 'test2' ] , yii\\web\\View::POS_END)
?>
|
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
相关文章
猜你喜欢
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-27 59
-
2025-05-29 34
-
2025-05-29 38
-
详解使用Mybatis-plus + velocity模板生成自定义的代码
2025-05-29 79 -
2025-05-29 54
热门评论