title: Paginate search results in Cakephp
body: <p>I made a report of the databases created in a server, showing some row counters results from similar tables on those databases, using a table named Host to change the database configuration..Now I'm trying to add a simple search form in the same view to show only the macthed results. But I having problems with pagination.</p>
  
  <p>In the HostController:</p>
  
  <pre><code>    &lt;?php 
      class HostsController extends AppController {
  public $paginate = array('limit' =&gt; 1); //just as a test    
  function index() {
      $conditions = array();
      if(!empty($_GET['host_name'])){
          $asd=($_GET['host_name']);
          $asd=strtoupper($asd);
          $conditions = array(
      'OR' =&gt; array(
      'upper(Host.client_name) LIKE' =&gt; "%$asd%",
      'upper(Host.contact_name) LIKE' =&gt; "%$asd%",
      'upper(Host.contac_email) LIKE' =&gt; "%$asd%"));
      }
  
      $hosts = $this-&gt;Host-&gt;find('all', array('conditions'=&gt;$conditions));
      foreach ($hosts as $key =&gt; $host) {
          App::import('model','Product');
          $this-&gt;Product = new Product;
          $this-&gt;Product-&gt;changeDataSource($host['Host']['db_name']);
          if($this-&gt;changeDbSource($host['Host']['db_name'])) {
  
              $count = $this-&gt;Product-&gt;query('SELECT count(id) as "Product__count" from products');
              $hosts[$key]['Host']['count_products']= $count[0]['Product']['count'];
              $options = array(
                  'conditions' =&gt; array(),
                  'order' =&gt; array(
                  'Product.created DESC'));
              $product = $this-&gt;Product-&gt;find('first', $options);
              $hosts[$key]['Host']['last_product'] = $product['Product'];
              }
          App::import('model','Sale');
          $this-&gt;Sale = new Sale;
          $this-&gt;Sale-&gt;changeDataSource($host['Host']['db_name']);
          if($this-&gt;changeDbSource($host['Host']['db_name'])) {
              $count = $this-&gt;Sale-&gt;query('SELECT count(id) as "Sale__count" from sales');
              $hosts[$key]['Host']['count_sales']= $count[0]['Sale']['count'];
              $options = array(
                  'conditions' =&gt; array(),
                  'order' =&gt; array(
                  'Sale.created DESC'));
              $sale = $this-&gt;Sale-&gt;find('first', $options);
              $hosts[$key]['Host']['last_sale'] = $sale['Sale'];
          }   }
          $this-&gt;set(compact('hosts'));
          $this-&gt;paginate ();}///&lt;&lt;&lt;&lt;  Here is the problem?&gt;
  </code></pre>
  
  <p>In the view:        </p>
  
  <pre><code>    &lt;form method="GET" &gt;
      &lt;input id="clientNameSearch" type="text" name="host_name" 
      value="&lt;?php echo !empty($_GET['host_name']) ? 
      $_GET['host_name']:"";?&gt;" placeholder="Client, Contact or email of the contact" &gt; 
      &lt;input type="submit" value="Search"&gt;
      &lt;/form&gt;
  </code></pre>
  
  <p>In the app controller(with a database config per row in hosts table):</p>
  
  <pre><code>    function changeDbSource($database = 'default') {
      $db = ConnectionManager::getInstance();
      $connected = $db-&gt;getDataSource($database);
      if($connected-&gt;isConnected()) {return true;} else {return false;}}
  </code></pre>
  
  <p>So, using just         $this->paginate ();             give me the WHOLE LIST of host table (or the search result) and doesnt paginate, only the leyend of the button result of    </p>
  
  <pre><code>echo $this-&gt;Paginator-&gt;counter(array('format' =&gt; __());     
  </code></pre>
  
  <p>in the view seems to work<br>
  I've been trying some variations on paginate() arguments, using conditions, definig a custom paginate() and others options suggested here with no positive result, probably because I'm new on this.</p>
  
  <p>According to the documentation, I shoud be able to use:</p>
  
  <pre><code>    &lt;?php
      if (!empty($_GET['host_name'])){
      $asd=($_GET['host_name']);
      $asd=strtoupper($asd);
       $this-&gt;paginate = array(
      'conditions' =&gt; array(
      'OR' =&gt; array(
      'upper(Host.client_name) LIKE' =&gt; "%$asd%",
      'upper(Host.contact_name) LIKE' =&gt; "%$asd%",
      'upper(Host.contac_email) LIKE' =&gt; "%$asd%")
       ),'limit' =&gt; 2);}//just a test 
       if(empty($_GET['host_name'])) {$this-&gt;paginate();}?&gt;
  </code></pre>
  
  <p>Still, no results. </p>
  
  <p>So, if anyone know how to make it work or any suggestion, I'm all ears.
  Thanks in advance(sorry for the poor english and coding).</p>
  
  <h2>Solution:?</h2>
  
  <p>Since there was no anwsers..here is what I got:
  I just put the next code before the foreach loops</p>
  
  <pre><code>    if (!empty($_GET['host_name'])){
          $getvar=($_GET['host_name']);
          $getvar=strtoupper($getvar);
          $this-&gt;paginate = array(
          'conditions' =&gt; array(
              'OR' =&gt; array(
              'upper(Host.client_name) LIKE' =&gt; "%$getvar%",
              'upper(Host.contact_name) LIKE' =&gt; "%$getvar%",
              'upper(Host.contac_email) LIKE' =&gt; "%$getvar%",
              'upper(Host.company_ruc) LIKE' =&gt; "%$getvar%")
               ),
          'limit' =&gt;2);}   
      $hosts=$this-&gt;paginate (); 
  </code></pre>
  
  <p>And in the Index.php: </p>
  
  <pre><code>    &lt;?php 
  $args = $_GET;
  unset($args['url']);
  $paginator-&gt;options( 
          array(
              'url' =&gt; $this-&gt;passedArgs + array('?'=&gt;$args)
          )
  );?&gt;
  </code></pre>
  
  <p>It shows what I need, in the way I need, but the sort option apparenly doesnt work..I guess I wont use it</p>