Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pagination Codeigniter

I'm using Codeigniter..and I'm new to this. What I'm trying to do is to use pagination in my code but the problem is the pagination link is not shown at the bottom.

The controller:

function index() {
  $this -> load -> library('pagination');
  $config['base_url'] = 'Rfetch1_controller/index/';
  $total = $this -> db -> count_all('info');
  $per_page = 4;

  $config['total_rows'] = $total;
  $config['per_page'] = $per_page;
  $this -> pagination -> initialize($config);
  $data['pagination'] = $this -> pagination -> create_links();
  $data['list'] = $this -> Rfetch1_model -> get_s($config['per_page'], $this -> uri -> segment(3));
  if ($data['list'] !== null) {

    $this -> load -> view('Rfetch1_view', $data);
  } else {
    $this -> load -> view('noresult');

  }
}

The model :

function get_s($num, $offset) {
  $this -> db -> select('subject, id, problem,image'); // field name
  $sql = $this -> db -> get('info', $num, $offset); // table name
  if ($sql -> num_rows() > 0) {
    foreach($sql -> result() as $row) {
      $data[$row -> id] = $row -> subject;
    }
    return $data;
  } else {
    return null;
  }
}

The view :

foreach($list as $id => $title): ?>
  <? echo anchor('Rfetch1_controller/get_by_id/'.$id, $title); ?>
<?php endforeach;?>
<?php echo $this->pagination->create_links(); ?>

Why the pagination link is not showing?

like image 801
martan Avatar asked Mar 21 '26 11:03

martan


1 Answers

You've already created links in your controller with this:

$data['pagination'] = $this->pagination->create_links();

so you just need to echo $pagination in your view:

<?php echo $pagination; ?>

You didn't load the pagination library in your view, just your controller - so you can't use the create_links() method in your view. But since you did use that method in your controller and pass it through to your view in the $data array, you can use that variable in your view.

like image 171
swatkins Avatar answered Mar 24 '26 15:03

swatkins



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!