Thursday, 13 March 2014

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');


0 comments:

Post a Comment