pagination in simple module

edited October 2011 in Modules
I'm getting stuck trying to implement pagination on a simple module.

My module is based on the news module tutorial and I'd like to paginate the
view that displays all of the excerpts.

Can any one point me to an example of how to do this?

Comments

  • edited 11:45AM
    Not sure if you know this already or not but CI comes with a Pagination class and is what FUEL uses for pagination as well.
    http://codeigniter.com/user_guide/libraries/pagination.html
  • edited 11:45AM
    Thanks for that.

    I've tried to implement this in my view but I'm still stuck.

    Right now I have a loop that displays teaser excerpts and that is what I'd like to paginate. I don't want to use the table helper for this.

    Here is my view code plus the pagination code the I have so far...

    <!-- set page specific meta info in header --> <?php fuel_set_var('page_title', 'Finance'); ?> <?php fuel_set_var('meta_description', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent hendrerit venenatis tincidunt.'); ?> <!-- page content --> <div class="grid_8" role="main"> <?php $slug = uri_segment(5); if (!empty($slug)) { $news_item = fuel_model('news', array('find' => 'one', 'where' => array('slug' => $slug))); if (empty($news_item)) show_404(); } else { // filter post list by silo $news = fuel_model('news', array('where' => array('silos.name' => 'finance'))); } ?> <?php if (!empty($news_item)) : ?> <?php fuel_set_var('page_title', $news_item->headline); ?> <div class="news_item"> <h2><?=$news_item->headline?></h2> <div class="date"><?=$news_item->release_date_formatted?></div> <?=$news_item->content_formatted?> </div> <?php else: ?> <?php foreach($news as $item) : ?> <div class="home-teaser"> <article> <hgroup> <h3><a href="<?=$item->url?>"><?=$item->headline?></a> - <?=$item->silo?></h3> <span class="post-info">Posted on <?=$item->release_date_formatted?></span> </hgroup> <div class="block"><?=$item->get_excerpt_formatted(300, 'Read Full Story &raquo;')?></div> </article> </div> <?php endforeach; ?> <?php $CI->load->library('pagination'); $config['base_url'] = 'http://localhost/otpp/finance/'; $config['total_rows'] = $CI->db->get('news')->num_rows(); $config['per_page'] = 2; $config['num_links'] = 10; $CI->pagination->initialize($config); echo $CI->pagination->create_links(); ?> <?php endif; ?> </div> <div class="grid_4"> <aside> <div class="box"> <?php $this->my_navigation->write_side_menu('news'); ?> </div> </aside> </div> <div class="clearfix"></div>

    Can this be done?
  • edited 11:45AM
    Is there anything being displayed now? Also, how many rows are being returned?
  • edited 11:45AM
    5 rows are returned, which is correct, and the pagination links are showing up but the loop I have set up is displaying all 5 excerpts.
  • edited 11:45AM
    You will need to limit the amount of data returned as well as pass the offset into the fuel_model query like so:
    fuel_model('news', array('where' => array('silos.name' => 'finance'), 'limit' => 5, 'offset' => uri_segment(3));
    Where uri_segment(3) being the reference to the URI segment that holds the offset number which gets generated by the pagination links.
  • edited 11:45AM
    Thanks so much yet again. I'll have to send you a case of beer :)

    It's working, but with a small issue remaining. Link 1 remains bold and not active no matter which page I am on. And if I'm on the last page, the next arrow still shows with no previous arrow displayed at all.

    I thought this would be a config issue but I'm having no luck with that.
  • edited 11:45AM
    Ha! Spoke too soon.

    I hadn't added $config['uri_segment'] = 2; to match the offset.

    It's all good now.
  • edited 11:45AM
    Glad you got it working. We are still working on our big Beer button for the site :)
  • edited 11:45AM
    I realized that I need to filter total_rows to only get rows that are set to the finance 'silo': 'where' => array('silos.name' => 'finance')

    Is there a way to modify $config['total_rows'] = $CI->db->get('news')->num_rows(); to do this?
  • edited 11:45AM
    You should be able to do a "where" active record call before that to filter the list like so:
    $CI->db->where(array('silos.name' => 'finance')); $config['total_rows'] = $CI->db->get('news')->num_rows();
  • edited 11:45AM
    Wow, that was a lot more concise than I thought it would be. Thank you.
  • Hello Admin,

    I did the same way that explained above but the thing is when I click the page 1, I got 404 error.

    $CI->load->library('pagination');
    $config['base_url'] = base_url('publication/notices');
    $CI->db->where(array('published' => 'yes'));
    $config['total_rows'] = $CI->db->get('tbl_notices')->num_rows();
    $config['per_page'] = $limit;
    $config['uri_segment'] = 3;
    $CI->pagination->initialize($config);
    echo $CI->pagination->create_links();

    Thanks

  • Hello, please any help on this.

  • Is this page setup in the CMS, a view file, or is it run through a controller?

    If it's a CMS page or a view file, you may need to adjust the MY_config$config['max_page_params'] = 0; to something like:

    $config['max_page_params'] = array('mypage' => 1);
    

    If it's a controller, make sure you have a route that includes the parameter as to what page

  • Thanks, it is now working perfectly.

Sign In or Register to comment.