Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Why isn't my CodeIgniter Pagination working?

Hello All.

I'm working on a CodeIgniter web application.

For my blog page, and in general for pages with a lot of data, I want to have a working Pagination system that only shows a certain number of links/articles/etc on each page.

Below, you'll find my pagination configuration. Although the links are printed out correctly (the number of rows/pages), but unfortunately, each page, shows me all of those links/articles/etc. So even though I have set the 'per_page' variable to 1, I get back all of my articles on all pages. What am I doing wrong? How can I fix this? Thanks in advance:

My Controller

<?php
        $this->load->library('pagination');
        $config['base_url'] = base_url().'articles/index';
        $config['total_rows'] = $data['pagination'];
        $config['per_page'] = 1;

        $this->pagination->initialize($config);

My View

<?php
<div class="container">
  <div class="row">
    <div class="col-md-9 has-margin-bottom"> 
        <img class="img-responsive center-block" src="<?php echo base_url(); ?>media/articles/<?php echo $article->img; ?>.jpg" alt="<?php echo $article->title; ?>">
        </div>
        <div class="col-md-8 col-sm-8 bulletin">
          <h4 class="media-heading"><a href="<?php echo base_url(); ?>articles/view/<?php echo $article->id; ?>"> <?php echo $article->title; ?> </a></h4>
          <p>on <?php echo date("F j, Y", strtotime($article->created)); ?> by <span class="link-reverse"><?php echo $article->first_name; ?> <?php echo $article->last_name; ?></span></p>
          <p><?php echo word_limiter($article->body, 20); ?></p>
          <a class="btn btn-primary" href="<?php echo base_url(); ?>articles/view/<?php echo $article->id; ?>" role="button">Read Article →</a> </div>
      </div>
        <?php endforeach; ?>
      <?php else: ?>
      <?php foreach ($articles as $article):?>
      <div class="row has-margin-bottom">
        <div class="col-md-4 col-sm-4"> <img class="img-responsive center-block" src="<?php echo base_url(); ?>media/articles/<?php echo $article->img; ?>.jpg" alt="<?php echo $article->title; ?>"> </div>
        <div class="col-md-8 col-sm-8 bulletin">
          <h4 class="media-heading"><a href="<?php echo base_url(); ?>articles/view/<?php echo $article->id; ?>"> <?php echo $article->title; ?> </a></h4>
          <p>on <?php echo date("F j, Y", strtotime($article->created)); ?> by <span class="link-reverse"><?php echo $article->first_name; ?> <?php echo $article->last_name; ?></span></p>
          <p><?php echo word_limiter($article->body, 20); ?></p>
          <a class="btn btn-primary" href="<?php echo base_url(); ?>articles/view/<?php echo $article->id; ?>" role="button">Read Article →</a> </div>
      </div>
      <?php endforeach; ?>
      <?php endif; ?>
      <!--Blog list-->

      <!-- PAGINATION -->

      <div class="text-center center-block">
        <ul class="pagination">
          <?php echo $this->pagination->create_links(); ?>
        </ul>
      </div>
    </div>
    <!--// col md 9-->