Jun 21, 2014

Ketan Patel

Search with Pagination Using Session in CakePHP


Search With Pagination in CakePHP


In this post i would like to show you how you can handle search forms while preventing pagination using session in cakephp.The basic idea is to set the entered search data in a session and add that as a condition for finding the records.




We have simple PostController.php file in which we have the below code.


<?php 

class PostsController extends AppController {
    public $helpers = array('Html', 'Form','Session');
    public $components=array('Session');


    public function index() {
    $flag='0';

     if($this->request->is('post')){
     // checking if the search term title is present or not
    if($this->request->data['title']!=''){

     // if present then set its value in session
     $this->Session->write('title',$this->request->data['title']);
     
  }

    // check for the reset option
if(isset($this->request->data['reset'])&& $this->request->data['reset']!='' ){

    // on reset just empty the session value
$this->Session->write('title','');
$flag='1';
}
}

    // reading session value and adding with like query
    $search=$this->Session->read('title');
    $condiation=array();
 
  if(isset($search) && $search!=''){
    $condiation=array(
    'title LIKE'=>'%'.strip_tags($search).'%'
    );
   }

   $this->paginate = array(
'conditions'=>$condiation,
   'limit' => 2
   );
   
   $posts = $this->paginate('Post');
   $this->set(compact('posts'));
    
   // redirection on index page
   if($flag=='1'){
    $this->redirect('index');
    }
  }
}
?>


Now, let's see what we have in index.ctp file.


Below is the simple code to display all the records in tabular form.

<table>
        <tr>
            <th>Id</th>
            <th>Title</th>
            <th>Created</th>
       </tr>
    <?php foreach ($posts as $post): ?>
    <tr>
          <td><?php echo $post['Post']['id']; ?></td>
          <td>
             <?php echo $this->Html->link($post['Post']['title'],
array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?>
         </td>
         <td><?php echo $post['Post']['created']; ?></td>
  </tr>
  <?php endforeach; ?>
  <?php unset($post); ?>
</table>



just below that , i have added pagination with next and previous buttons.


<?php 
echo $this->Paginator->prev('<< Previous', null, null, array('class' => 'disabled')); 
echo $this->Paginator->numbers(); 
echo $this->Paginator->next(' Next >> ', null, null, array('class' => 'disabled')); 
?>


and top of all added search form.In this form we are reading the session value and setting as a input field value.We have one reset button to reset the result.

<table>
<tr>
        <td>
<?php
$session=$this->Session->read('title');
$value=isset($session)?$session:'';
echo $this->form->input('title',array('value'=>$value));?>
</td>
</tr>
<tr>
<td>
<?php echo $this->Form->input('Search',array('type'=>'submit','label'=>false))?>
</td>
<td>
<?php echo $this->Form->input('Reset',array('type'=>'submit','name'=>'reset','label'=>false))?>        </td>
  </tr>
</table>


Now our complete index.ctp file looks like this,


<!-- File: /app/View/Posts/index.ctp -->
<h1>Blog posts</h1>
<?php echo $this->Form->create('search');?>
<table>
<tr>
<td>
<?php
$session=$this->Session->read('title');
$value=isset($session)?$session:'';
echo $this->form->input('title',array('value'=>$value));?>
</td>
</tr>
<tr>
<td>
<?php echo $this->Form->input('Search',array('type'=>'submit','label'=>false))?>
</td>
<td>
<?php echo $this->Form->input('Reset',array('type'=>'submit','name'=>'reset','label'=>false))?>     </td>
</tr>
</table>
<?php echo $this->Form->end();?>
<table>
    <tr>
        <th>Id</th>
        <th>Title</th>
        <th>Created</th>
    </tr>
   <?php foreach ($posts as $post): ?>
    <tr>
        <td><?php echo $post['Post']['id']; ?></td>
        <td>
            <?php echo $this->Html->link($post['Post']['title'],
array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?>
        </td>
        <td><?php echo $post['Post']['created']; ?></td>
    </tr>
<?php endforeach; ?>
<?php unset($post); ?>
</table>

<?php 
echo $this->Paginator->prev('<< Previous', null, null, array('class' => 'disabled')); 
echo $this->Paginator->numbers(); 
echo $this->Paginator->next(' Next >> ', null, null, array('class' => 'disabled')); 
?>


Thats it.!! you have simple and basic demo of "Search with Pagination using Session in CakePHP"  ready..


Please share your thoughts or any suggestions for improvement or if you have any issues, queries then feel free to comment.

Ketan Patel

About Ketan Patel -

I have developed a wide range of websites using CorePHP, Opencart, CakePHP and CodeIgniter including sites for startup companies and small businesses. Apart from my blogging life, I like to read Novels, Listening music and Net surfing.

Subscribe to this Blog via Email :