Tuesday, 12 August 2014

Cakephp Search Form

In this CakePHP tutorial I would like to show you how I handle search forms, while preserving pagination.
The basic principal is to read the posted variables, and redirect the user to a page with the appropriate filters in the URL.

Add the Search Form

Lets add a search form on the index page to search through the records. index.ctp


<?php echo $form->create('Post',array('action'=>'search'));?>
    <fieldset>
        <legend><?php __('Post Search');?></legend>
    <?php
        echo $form->input('Search.keywords');
        echo $form->input('Search.id');
        echo $form->input('Search.name',array('after'=>__('wildcard is *',true)));
        echo $form->input('Search.body',array('after'=>__('wildcard is *',true)));
        echo $form->input('Search.active',array(
            'empty'=>__('Any',true),
            'options'=>array(
                0=>__('Inactive',true),
                1=>__('Active',true),
            ),
        ));
        echo $form->input('Search.created'array('after'=>'eg: >= 2 weeks ago'));
        echo $form->input('Search.category_id');
        echo $form->input('Search.tag');
        echo $form->input('Search.tag_id');
        echo $form->submit('Search');
    ?>
    </fieldset>
<?php echo $form->end();?>

No comments:

Post a Comment