3

I have created a custom block that has a form which has a list of result and I wanted to know how to add pagination to the results

Thanks for any help on this

Colin

1 Answer 1

6
    /**
 * Implements hook_block_info().
 */
function custommodule_block_info() {
    $blocks = array();
    $blocks['latest_news'] = array(
        'info' => t('Latest News'),
        'cache' => DRUPAL_NO_CACHE,
    );

    return $blocks;
}

    /**
 * Implements hook_block_view().
 */
function custommodule_block_view($delta = '') {
    $block = array();
    switch ($delta) {

        case 'latest_news':
            $block['subject'] = t('');
            $block['content'] = latest_news();
            break;
    }
    return $block;
}


/*
 * Implementation of hook_theme
 * */

function custommodule_theme() {
    return array(

        'latest_news' => array('variables' => array('data' => array()),
        'template' => 'latest_news'),

    );
}

    function latest_news() {
     $query = db_select('node', 'node');
    $query = $query->fields('node', array('nid'))
            ->condition('node.status', 1) //Published.
            ->condition('node.type', 'auction_news');
    $query = $query->extend('PagerDefault');
    $result = $query->limit(20)->orderBy('node.created', 'DESC')->execute()->fetchAll(PDO::FETCH_ASSOC);
    $paging = theme('pager');

    return theme('latest_news',array('data' => $result,'paging'=>$paging));
}

In a tpl file latest_news.tpl.php

 <ul>

    <?php 
    foreach($data as $lat_news)
    {


       echo '<li>'.$lat_news->nid.'</li>';
    }
    ?>
    </ul>
    <div><?php echo $paging;?></div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.