Skip to main content

CakePHP pagination

Paginate provide a set of controls to navigate through paged data.

Pagination inside Controller
class RecipesController extends AppController {
    public $components = array('Paginator');
    public $paginate = array(
        'limit' => 25,
        'contain' => array('Article')
    );
}

Pagination define inside function
public function list_recipes() {
    $this->Paginator->settings = $this->paginate;
    // similar to findAll(), but fetches paged results
    $data = $this->Paginator->paginate('Recipe');
    $this->set('data', $data);
}

You can filter the records by passing conditions as second
parameter to the paginate() function:
$data = $this->Paginator->paginate(
    'Recipe',
    array('Recipe.title LIKE' => 'a%')
);

Pagination with behaviour 
$this->paginate=array('conditions'=>
          array('NewArticle.status'=>1,'NewArticle.new_cat'=>$catid),
         'contain'=>array('NewsCategory.category_name','Member.first_name'),
         'order'=>'NewArticle.id DESC','limit'=>8);

$newInfo=$this->paginate('NewArticle');

Pagination Helper in View

Notes:The Pagination helper is used to output pagination controls such 
            as page numbers and next/previous links. 

<table>
    <tr>
        <th><?php echo $this->Paginator->sort('id', 'ID'); ?></th>
        <th><?php echo $this->Paginator->sort('title', 'Title'); ?></th>
    </tr>
       <?php foreach ($data as $recipe): ?>
    <tr>
        <td><?php echo $recipe['Recipe']['id']; ?> </td>
        <td><?php echo h($recipe['Recipe']['title']); ?> </td>
    </tr>
    <?php endforeach; ?>
</table>

// Shows the page numbers
echo $this->Paginator->numbers();

// Shows the next and previous links
echo $this->Paginator->prev( '« Previous', null,null,array('class' => 'disabled'));
echo $this->Paginator->next( 'Next »',null,null,array('class' => 'disabled'));

// prints X of Y, where X is current page and Y is number of pages
echo $this->Paginator->counter();

Setting ‘format’ to range would output like ‘1 - 3 of 13’:
echo $this->Paginator->counter(array(
    'format' => 'Page {:page} of {:pages}, showing {:current} records out of
             {:count} total, starting on record {:start}, ending on {:end}'

));

Pagination with Bootstrap theme

<div class="pagination pagination-right">
    <ul>
        <?php
            echo $this->Paginator->prev( '<<', array( 'class' => '', 'tag' => 'li' ),
            null, array( 'class' => 'disabled', 'tag' => 'li' ) );
            echo $this->Paginator->numbers( array( 'tag' => 'li', 'separator' => '',
             'currentClass' => 'active', 'currentTag' => 'a' ) );
            echo $this->Paginator->next( '>>', array( 'class' => '', 'tag' => 'li' ),
              null, array( 'class' => 'disabled', 'tag' => 'li' ) );
        ?>
    </ul>

</div>

Full text searching in CakePHP
  $this->paginate = array(
            'limit' => 15,
            'fields' => array('*', "MATCH (data) AGAINST ('$q') AS rating"),
            'conditions' =>  "MATCH(SearchIndex.data) AGAINST('$q' IN BOOLEAN MODE)",
            'order' => array(
                'rating' => 'desc',
            ),
    );
    $paginatedResults = $this->paginate('SearchIndex');

Join with pagination
$joins = array(
array(
'table'=>'vhire_parent.accounts',
'type'=>'inner',
'alias'=>'Account',
'conditions'=>array('Jobposting.account_id = Account.id')
)
);

$this->paginate['joins'] = $joins;

Condition on join pagination
$this->paginate['conditions'] = array('Account.account_type !='=>'System');

Fetch fields in pagination
$this->paginate['fields'] = array('Jobposting.posting_code','Account.account_type');

$fields = array('Topic.id','Topic.name','Topic.status','Topic.created','group_concat(distinct(Grade.name)) as grades','group_concat(distinct(Tag.name)) as tags');

$joins = array(
array('table' => 'topics', 'alias' => 'Topic', 'type' => 'left', 'conditions' => array('Topic.id = TopicGradeTag.topic_id')),
array('table' => 'tags', 'alias' => 'Tag', 'type' => 'left', 'conditions' => array('Tag.id = TopicGradeTag.tag_id')),
array('table' => 'grades', 'alias' => 'Grade', 'type' => 'left', 'conditions' => array('Grade.id = TopicGradeTag.grade_id'))
   
);

$this->paginate = array(
'conditions' => $conditions,
'join' => $joins,
'limit' => 30,
'recursive'=>-1,
'order' => $order,
'group'=>array('TopicGradeTag.topic_id'),
'fields' => $fields,
);
$data = $this->paginate('TopicGradeTag');


Comments

Popular posts from this blog

6 guaranteed steps how to create CRON JOB FUNTION in wordpress

Create Cron Job function in Wordpress plugin Step 1: Register function on plugin activate  register_activation_hook(__FILE__, 'activate_one'); Step 2: add_filter function for interval //Filter for Adding multiple intervals add_filter( 'cron_schedules', 'intervals_schedule' ); // define interval function   function intervals_schedule($schedules) {   $schedules['everyminute'] = array(    'interval' => 60, // Every 1 minutes    'display'  => __( 'Every 1 minutes' )   );   return $schedules;  } Step 3: Activate hook function   //Schedule a first action if it's not already scheduled   function activate_one() {   if (!wp_next_scheduled('wpyog_cron_action')) {    wp_schedule_event( time(), 'everyminute', 'wpyog_cron_action');   }  } Step 4: Cron hook function   //Hook into that action that'll fire after 1 minutes   add_action('wpyog_cron_action', 'execute_

How to Create a jQuery Autocomplete in Wordpress

How to Create a jquery-ui Autocomplete in wordpress. Autocomplete provides suggestions while you type into the text field. In Wordpress we fetch dynamically matched pattern. Include javascript and css files in header. Create action inside functions.php or inside plugin code. add_action('wp_head', 'custom_register_scripts'); function custom_register_scripts(){ wp_register_style( 'techsudhir_jquery_ui_css', plugin_dir_url(__FILE__) . 'css/jquery-ui.css', false,'1.0.0' ); wp_enqueue_style( 'techsudhir_jquery_ui_css' ); wp_register_script('techsudhir_jquery_ui_js',plugin_dir_url(__FILE__) . 'js/jquery-ui.js',array('jquery'),'1.1', false); wp_enqueue_script('techsudhir_jquery_ui_js'); wp_localize_script( 'techsudhir_autocomplete', 'jqueryAutocomplete', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); wp_enqueue_script( 'techsudhir_

Facebook Login

Step 1: Go on Apps & create new Apps Step 2: Enter the app name & choose category Step 3: Enter the site domain Script code <div id="fb-root"></div> <script   src="http://connect.facebook.net/en_US/all.js#appId=<?php echo '603609636351597';?>"> </script> <script>    FB.init({              appId:'<?php echo '603609636351597';?>', cookie:true,              status:true, xfbml:true           });   FB.getLoginStatus(function(response) {   }); Login function call on click function faceLogin(){  FB.login(function(response) {    if (response.authResponse) {  FB.api('/me', function(response) { $('input[id=facebook_login_fb]').val(response['id']); $('#fb_login_form').submit();  });    } else {  console.log('User cancelled login or did