title: How to add Search and Filter Criteria in Yii
body: <p>I am developing a database application using Yii Framework. I am reading tables from MySQL database and displaying them to the user. I need the user to be able to filter the fields in the table or search for a certain value. </p>
  
  <p>For example, I have a table named "supermarkets":</p>
  
  <pre><code>CREATE TABLE IF NOT EXISTS `supermarkets` (
    `Name` varchar(71) NOT NULL,
    `Location` varchar(191) DEFAULT NULL,
    `Telephone` varchar(68) DEFAULT NULL,
    `Fax` varchar(29) DEFAULT NULL,
    `Website` varchar(24) DEFAULT NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  </code></pre>
  
  <p>.../model/supermarkets:</p>
  
  <pre><code>&lt;?php
  
  namespace app\models;
  
  use yii\db\ActiveRecord;
  
  class Supermarkets extends ActiveRecord
  {
  
  
  
  }
  </code></pre>
  
  <p>.../views/supermarkets/index.php:</p>
  
  <pre><code>&lt;?php
  use yii\helpers\Html;
  use yii\widgets\LinkPager;
  ?&gt;
  &lt;h1&gt;Supermarkets&lt;/h1&gt;
  &lt;ul&gt;
  &lt;?php
  $array = (array) $supermarkets;
  
  function build_table($array){
  
      // start table
  
      $html = '&lt;table class="altrowstable" id="alternatecolor"&gt;';
  
      // header row
  
      $html .= '&lt;tr&gt;';
  
      foreach($array[0] as $key=&gt;$value){
  
              $html .= '&lt;th&gt;' . $key . '&lt;/th&gt;';
  
          }
  
      $html .= '&lt;/tr&gt;';
  
      // data rows
  
      foreach( $array as $key=&gt;$value){
  
          $html .= '&lt;tr&gt;';
  
          foreach($value as $key2=&gt;$value2){
  
              $html .= '&lt;td&gt;' . $value2 . '&lt;/td&gt;';
  
          }
  
          $html .= '&lt;/tr&gt;';
  
      }
  
      // finish table and return it
  
      $html .= '&lt;/table&gt;';
  
      return $html;
  
  }
  
  
  
  echo build_table($array);
  
  ?&gt;
  </code></pre>
  
  <p>....Controllers/SupermarketsController:</p>
  
  <pre><code>&lt;?php
  
  namespace app\controllers;
  
  use yii\web\Controller;
  
  use yii\data\Pagination;
  use app\models\Supermarkets;
  class SupermarketsController extends Controller
  {
      public function actionIndex()
      {
          $query = supermarkets::find();
  
          $pagination = new Pagination([
              'defaultPageSize' =&gt; 20,
              'totalCount' =&gt; $query-&gt;count(),
          ]);
  
          $supermarkets = $query-&gt;orderBy('Name')
              -&gt;offset($pagination-&gt;offset)
              -&gt;limit($pagination-&gt;limit)
              -&gt;all();
  
          return $this-&gt;render('index', [
              'supermarkets' =&gt; $supermarkets,
              'pagination' =&gt; $pagination,
          ]);
      }
  }
  </code></pre>
  
  <p>I need the user to be able to filter the table or search its fields by one or more attribute. How can I do this?</p>