Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Search in cakephp

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 348
    Comment on it

    We can put a search box in our project by using the cakephp component.

    It is very useful and can provide highly functionality across the controller.

    All you need to do is include component in your controller and call component function to fetch data.

    This search component will be searching the parameter of the box and generate the condition and refine search.

    You can download the component from the link below

    http://download.designaeon.com/search2.0.rar

    It is for 2x ver.

    Now copy the search component in the component and include this in your controller.

    public $components=array('Search');

    Now create search form that can be used across the views.

    <div class="search" style="margin: 10px;">
    <fieldset>
    <h2>Search <?php echo $this--->params['controller'] ?></h2>
    
    <hr />
    
    <?php echo $this--->Form->create(array('action' => 'contact', 'type' => 'GET')); ?>
    <?php $name = isset($this--->params['url']['name']) ? $this->params['url']['name'] : null;
    echo $this->Form->input('id', array( 'label'=>'ID','type'=>'text'));
    echo $this->Form->input('name', array('label'=>'Name','value'=>$name));
    echo $this->Form->submit('Search',array('class'=>'button'));
    ?>
    
    <?php echo $this->Form->end(); ?></fieldset>
    </div>

    To create search component work fine with pagination we can create another element called set_pagination_named.ctp file which can be copied in this path.

    http://C:\xampp\htdocs\your app name\app\views\elements\searchForm\set_pagination_named.ctp:

    This element will check the search condition and pass them on to th the paginator.

    [crayon-5704d3e5a3742984554998   lang="php" decode="true" ]<?php 
    $data_arr=array(); 
     if(isset($this->params['url']) || isset($this->params['named'])){
     $data=isset($this->params['named'] ) && !empty($this->params['named']) ? $ this->params['named']:$this->params['url'];
     $possibledata=array('name','id');//possible fields
     foreach($data as $key=>$value){
        if(in_array($key,$possibledata) && !empty($value)){
      $data_arr[$key]=$data[$key];
     }
    }
    
    }
    ?>
    <?php $this->Paginator->options(array(
    'evalScripts' => true,
    'url'=>$data_arr
    ));
    ?>

    Now we create the controller action.

    You can create this on any function where you would like to use the search component.

    say for ex contact

    This is there in you contact function

    function contact() {
    $this->Page->recursive = 0;
    $conditions=$this->Search->getConditions();
    $this->set('pages', $this->paginate(null,$conditions));
    }

    When you will search the parameter it will going to show all the search result by search component.

    Now we create the contact.ctp

    </pre>
    [crayon-5704d3e5a3764360706114   lang="php" decode="true" ]<div class="pages contact">
    <?php
    echo $this->element('searchForm/set_pagination_named');//set pagination named element 
    ?>
    <?php 
    echo $this->element('searchForm/form') ?>//search form element
    ?>
    	<h2><?php __('Pages');?></h2>
    	<table cellpadding="0" cellspacing="0">
    	<tr>
    			<th><?php echo $this->Paginator->sort('id');?></th>
    			<th><?php echo $this->Paginator->sort('parent_id');?></th>
    			<th><?php echo $this->Paginator->sort('name');?></th>			
    			<th><?php echo $this->Paginator->sort('slug');?></th>
    			<th><?php echo $this->Paginator->sort('status');?></th>
    			<th><?php echo $this->Paginator->sort('created');?></th>
    			<th><?php echo $this->Paginator->sort('modified');?></th>
    			<th class="actions"><?php __('Actions');?></th>
    	</tr>
    	<?php
    	$i = 0;
    	foreach ($pages as $page):
    		$class = null;
    		if ($i++ % 2 == 0) {
    			$class = ' class="altrow"';
    		}
    	?>
    	<tr<?php echo $class;?>>
    		<td><?php echo $page['Page']['id']; ?>&nbsp;</td>
    		<td>
    			<?php echo $this->Html->link($page['ParentPage']['name'], array('controller' => 'pages', 'action' => 'view', $page['ParentPage']['id'])); ?>
    		</td>
    		<td><?php echo $page['Page']['name']; ?>&nbsp;</td>
    		<td><?php echo $page['Page']['slug']; ?>&nbsp;</td>
    		<td><?php echo $page['Page']['status']; ?>&nbsp;</td>
    		<td><?php echo $page['Page']['created']; ?>&nbsp;</td>
    		<td><?php echo $page['Page']['modified']; ?>&nbsp;</td>
    		<td class="actions">
    			<?php echo $this->Html->link(__('View', true), array('action' => 'view', $page['Page']['id'])); ?>
    			<?php echo $this->Html->link(__('Edit', true), array('action' => 'edit', $page['Page']['id'])); ?>
    			<?php echo $this->Html->link(__('Delete', true), array('action' => 'delete', $page['Page']['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $page['Page']['id'])); ?>
    		</td>
    	</tr>
    <?php endforeach; ?>
    	</table>
    	<p>
    	<?php
    	echo $this->Paginator->counter(array(
    	'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true)
    	));
    	?>	</p>
    
    	<div class="paging">
    		<?php echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class'=>'disabled'));?>
    	 | 	<?php echo $this->Paginator->numbers();?>
     |
    		<?php echo $this->Paginator->next(__('next', true) . ' >>', array(), null, array('class' => 'disabled'));?>
    	</div>
    </div>

    Now the new search box will be create to search by using search component.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: