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

A Guide to UTF-8 for PHP and MySQL

Data Encoding: A Guide to UTF-8 for PHP and MySQL As a MySQL or PHP developer, once you step beyond the comfortable confines of English-only character sets, you quickly find yourself entangled in the wonderfully wacky world of UTF-8. On a previous job, we began running into data encoding issues when displaying bios of artists from all over the world. It soon became apparent that there were problems with the stored data, as sometimes the data was correctly encoded and sometimes it was not. This led programmers to implement a hodge-podge of patches, sometimes with JavaScript, sometimes with HTML charset meta tags, sometimes with PHP, and soon. Soon, we ended up with a list of 600,000 artist bios with double- or triple encoded information, with data being stored in different ways depending on who programmed the feature or implemented the patch. A classical technical rat’s nest.Indeed, navigating through UTF-8 related data encoding issues can be a frustrating and hair-pul...

How To Create Shortcodes In WordPress

We can create own shortcode by using its predified hooks add_shortcode( 'hello-world', 'techsudhir_hello_world_shortcode' ); 1. Write the Shortcode Function Write a function with a unique name, which will execute the code you’d like the shortcode to trigger: function techsudhir_hello_world_shortcode() {    return 'Hello world!'; } Example: [hello-world] If we were to use this function normally, it would return Hello world! as a string 2. Shortcode function with parameters function techsudhir_hello_world_shortcode( $atts ) {    $a = shortcode_atts( array(       'name' => 'world'    ), $atts );    return 'Hello ' . $a['name'] . !'; } Example: [hello-world name="Sudhir"] You can also call shortcode function in PHP using do_shortcode function Example: do_shortcode('[hello-world]');

Integrating Kafka with Node.js

Integrating Kafka with Node.js Apache Kafka is a popular open-source distributed event streaming platform that uses publish & subscribe mechanism to stream the records(data). Kafka Terminologies Distributed system: Distributed system is a computing environment where various software components located on different machines (over multiple locations). All components coordinate together to get stuff done as one unit.   Kafka Broker: Brokers are cluster of multiple servers. Message of each topic are split among the various brokers. Brokers handle all requests from clients to write and read events. A Kafka cluster is simply a collection of one or more Kafka brokers. Topics: A topic is a stream of "related" messages. Its unique throughout application. Kafka producers write messages to topics. Producer: Producer publishes data on the topics. A producer sends a message to a broker and the broker receives and stores messages. Consumers: Consumers read data from topics. A consu...