此例子在yii2.0框架做的包括分页,sphinx搜索,
<?php
//use yii\helpers\Html;
//use yii\widgets\ActiveForm;
use yii\widgets\LinkPager;
?>
<script src="./public/jquery-1.7.2.min.js"></script>
<center>
<form action="?r=rikao/lian4" method="post">
<input type="text" name="search" value="<?php echo isset($search)?$search:"";?>">
<input type="submit" value="搜索" style='border-radius:5px'>
</form>
<table border=1>
<tr>
<td>选项</td>
<td>序号</td>
<td>商品名称</td>
<td>商品品牌</td>
<td>商品状态</td>
<td>操作</td>
</tr>
<?php foreach($model as $k=>$v){ ?>
<tr>
<td><input type="checkbox" name="box[]" value="<?php echo $v['good_id'];?>"></td>
<td><?php echo $v['good_id']?></td>
<td><?php echo $v["g_name"]?></td>
<td><?php echo $v["g_brand"]?></td>
<td class="save"><?php
if($v["is_up"]==1){
echo "<span>上架</span>";
}else{
echo "<span>下架</span>";
}
?>
</td>
<td pid="<?php echo $v['good_id'] ?>" class="a"><?php if($v["is_up"]==0){ ?>
<a href="javascript:;" class="is_up" >上架</a>
<?php }else{ ?>
<a href="javascript:;" class="is_up" >下架</a>
<?php } ?>
</td>
</tr>
<?php } ?>
<tr>
<td colspan="3">
<button οnclick=Duibi()>对比</button>
<button id="paixu">排序</button>
</td>
</tr>
</table>
<?php echo LinkPager::widget(['pagination' => $pages,]);?>
<div id="div1"></div>
</center>
<script>
function Duibi(){
_str=[];
//each获取每个值
$("input[name='box[]']:checked").each(function(){
_str.push($(this).val());
})
if(_str.length != 2){
alert("只能先择两个")
}else{
var str="";
var box =document.getElementsByName('box[]');
for(var i=0;i<box.length;i++){
if(box[i].checked==true){
str =str+','+box[i].value;
}
}
str=str.substr(1);
$.getJSON("?r=rikao/duibi",{good_id:str},function(msg){
var str = '';
str+= "<h2>商品对比</h2><table border=1><tr><td>商品名称</td><td>商品货号</td><td>商品品牌</td><td>商品价格</td></tr>"
for(var i=0;i<msg['res'].length;i++)
{
//拼接
str+="<tr><td>"+msg['res'][i].g_name+"</td><td>"+msg['res'][i].g_sn+"</td><td>"+msg['res'][i].g_brand+"</td><td>"+msg['res'][i].price+"</td></tr>";
}
str+="</table>";
$("#div1").html(str);
})
}
}
// 排序
$("#paixu").click(function(){
location.href="?r=rikao/paixu";
})
//修改状态
// $(document).on("click",".is_up",function(){
// $(".is_up").click(function(){
$(".is_up").live("click",function(){
_this = $(this);
pid = $(this).parent().attr("pid");
// alert(pid)
$.get("?r=rikao/save",{pid:pid},function(msg){
if(msg==1){
_this.parent().prev().html("上架");
_this.html('<a href="javascript:;" class="is_up" >下架</a>');
}else{
_this.parent().prev().html("下架");
_this.html('<a href="javascript:;" class="is_up" >上架</a>');
}
})
})
</script>
controller页面调用yii2.0分页类,Sphinx类use SphinxClient;
use yii\web\UploadedFile;
public function actionLian4(){
$search = \Yii::$app->request->post("search");
include './sphinx/sphinxapi.php'; //包含sphinxapi类
$sphinx= new SphinxClient(); //实例化
$sphinx->SetServer('127.0.0.1',9312);
$sphinx->SetConnectTimeout (1);
$res=$sphinx->Query($search,"*");
if($search==""){
$query = Goods::find();
}else{
$arr="";
foreach($res['matches'] as $key=>$val){
$arr.= $key.",";
}
$arr= substr($arr,0,-1);
$query = Goods::find()->where("good_id in($arr)");
}
$countQuery = clone $query;
//$pages = new Pagination(['totalCount' => $countQuery->count()]);
$pages = new Pagination([
'totalCount' => $countQuery->count(),
'totalCount' => $query->count(),
'pageSize' => 10 //每页显示条数
]);
$models = $query->offset($pages->offset)
->limit($pages->limit)
->all();
//var_dump($models);die;
//搜索变红
foreach($models as $k=>$v){
$models[$k]['g_name']=str_replace($search,"<font style='font-weight:bold;color:#f00'>$search</font>",$v['g_name']);
}
return $this->render('lian4', [
'model' => $models,
'pages' => $pages,
'search' => $search,
]);
}
//对比
public function actionDuibi(){
$good_id = \Yii::$app->request->get("good_id");
//echo $good_id;die;
$sql = "SELECT * FROM goods WHERE good_id in($good_id) ";
$query= \Yii::$app->db->createCommand($sql)->queryAll();
//echo $query;
echo json_encode(array("res"=>$query));
}
//排序
public function actionPaixu(){
$query = Goods::find()->OrderBy("good_id desc");
$countQuery = clone $query;
//$pages = new Pagination(['totalCount' => $countQuery->count()]);
$pages = new Pagination([
'totalCount' => $countQuery->count(),
'totalCount' => $query->count(),
'pageSize' => 10 //每页显示条数
]);
$models = $query->offset($pages->offset)
->limit($pages->limit)
->all();
return $this->render('lian4', [
'model' => $models,
'pages' => $pages,
]);
}
//修改状态
public function actionSave(){
$pid = \Yii::$app->request->get("pid");
$sql = "SELECT * FROM goods WHERE good_id = '$pid' ";
$query= \Yii::$app->db->createCommand($sql)->queryAll();
if($query[0]['is_up'] ==0){
\Yii::$app->db->createCommand("update goods set is_up =1 where good_id = '$pid'")->execute();
echo 1;
}else{
\Yii::$app->db->createCommand("update goods set is_up =0 where good_id = '$pid'")->execute();
echo 2;
}
}