简体   繁体   English

yii分页搜索不起作用

[英]yii pagination with searching not working

I am implementing pagination (yii1) in my list page with search items: when I search something the pagination total item 5 (I mean link like 1 2 3 4 5) and the search's fine. 我正在用搜索项在列表页面中实现分页(yii1):当我搜索某项时,分页总计项5(我的意思是像1 2 3 4 5这样的链接)并且搜索很好。

When I click a second page in pagination link, the total items increased to 12(i mean link like 1 2 3 4 5 ...10 11 12). 当我单击分页链接的第二页时,项目总数增加到12(我的意思是链接,例如1 2 3 4 5 ... 10 11 12)。

My query is working fine with like operator. 我的查询与类似运算符正常工作。 I don't know where it's getting changed.. 我不知道它在哪里改变。

This is in my controller 这是在我的控制器中

 if((isset($_POST['searchtext'])) && (!empty($_POST['searchtext']))){

       $sql ="select * from table1 where name LIKE '\'%'.$_POST['searchtext'].'%\'" ; 

       $sql_count = "select count(*) from table1 LIKE '\'%'.$_POST['searchtext'].'%\'" ;

       $count=Yii::app()->db->createCommand($sql_count)->queryScalar();


   }else{

          $sql = "select * from table1";
          $sql_count = "select * from table1";
         $count=Yii::app()->db->createCommand($sql_count)->queryScalar();
  }
   $dataProvider=new CSqlDataProvider($sql, array(
                'totalItemCount'=>$count,
                'sort'=>array(
                 'attributes'=>array(
                 'id','userid','user_email',
                                    ),
                            ),
            'pagination'=>array(
            'pageSize'=>9,
            ),
    ));
         $this->render('listallview',array(
            'dataProvider'=>$dataProvider,


    ));

And my view 我的看法

        <ul class="list ">
          <?php

          $this->widget('bootstrap.widgets.TbListView', array(
            'dataProvider'=>$dataProvider,
            'itemView'=>'_view',

            ));

          ?>
       </ul>

What could be the problem? 可能是什么问题呢?

if you want to use the pagination feature, you must configure the totalItemCount property to be the total number of rows (without pagination). 如果要使用分页功能,则必须将totalItemCount属性配置为总行 (无分页)。

Try in your controller: 尝试在您的控制器中:

if((isset($_POST['searchtext'])) && (!empty($_POST['searchtext']))){

   $sql ="select * from table1 where name LIKE '\'%'.$_POST['searchtext'].'%\'" ; 

   //$sql_count = "select count(*) from table1 LIKE '\'%'.$_POST['searchtext'].'%\'" ;

   //$count=Yii::app()->db->createCommand($sql_count)->queryScalar();


   }else{

      $sql = "select * from table1";
     //$sql_count = "select * from table1";
     //$count=Yii::app()->db->createCommand($sql_count)->queryScalar();
     }

     $count=Yii::app()->db->createCommand('SELECT COUNT(*) FROM table1')->queryScalar();

     $dataProvider=new CSqlDataProvider($sql, array(
            'totalItemCount'=>$count,
            'sort'=>array(
             'attributes'=>array(
             'id','userid','user_email',
                                ),
                        ),
        'pagination'=>array(
        'pageSize'=>9,
        ),
));
     $this->render('listallview',array(
        'dataProvider'=>$dataProvider,


));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM