martan
martan

Reputation: 95

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?

Upvotes: 3

Views: 7237

Answers (3)

Malek
Malek

Reputation: 11

Searching Articles in CodeIgniter

Step 1: Create model search function.

public function search($query)
{
   $q=$this->db->from('articles')
      ->like('title',$query)
      ->get();
   return $q->result();
}

Step 2: Create search function in controller.

public function search()
{
   $this->load->library('form_validation');
   $this->form_validation->set_rules('query','Query','required');

   if(! $this->form_validation->run())
    $this->index();

   $query=$this->input->post('query');
   $this->load->model('articlesmodel','articles');
   $articles=$this->articles->search($query);

   $this->load->view('public/search_results',compact('articles'));
}

Step 3: Create search_results in view.

<?php include ('public_header.php');?>
<div class="container">
   <h1>Serach Results</h1>
   <table class="table">
    <thead>
     <tr>
      <td>Sr No.</td>
      <td>Article Title</td>
      <td>Published On</td>
     </tr>
    </thead>
    <tbody>
     <tr>
    <?php if(count($articles)):?>
     <?php $count=$this->uri->segment(3,0);?>
     <?php foreach($articles as $article ):?>
      <td><?= ++$count?></td>
      <td><?= $article->title?></td>
      <td><?= "Date" ?></td>
     </tr>
     <?php endforeach;?>

    <?php else: ?>
     <tr>
      <td colspan="3"> No Records Found.</td>
     </tr>
     <?php endif;?>
    </tbody>
   </table>
</div>

<?php include ('public_footer.php');?>

Upvotes: 0

Xristos Boursinos
Xristos Boursinos

Reputation: 61

$this->load->model('reciever');
                $this->load->library('uri');
                $this->load->library('pagination');
                $config['base_url'] = base_url(). 'users_ci/users';
                $config['total_rows'] = $this->reciever->getRows();
                $config['per_page'] = 4;
                 $config['full_tag_open'] = '<ul class="pagination">';
                $config['full_tag_close'] = '</ul>';            
                $config['prev_link'] = '&laquo;';
                $config['prev_tag_open'] = '<li>';
                $config['prev_tag_close'] = '</li>';
                $config['next_link'] = '&raquo;';
                $config['next_tag_open'] = '<li>';
                $config['next_tag_close'] = '</li>';
                $config['cur_tag_open'] = '<li class="active"><a href="#">';
                $config['cur_tag_close'] = '</a></li>';
                $config['num_tag_open'] = '<li>';
                $config['num_tag_close'] = '</li>';
                $config["num_links"] = round( $config["total_rows"] / $config["per_page"] );
                $config['users']= $this->reciever->getUsers(4,$this->uri->segment(3));
                $this->pagination->initialize($config);
                $config['pages'] = $this->pagination->create_links();
                $this->load->view('users',$config);

this is the function that i have in my controler! and in the view i have this one

<div><?php echo $pages; ?></div>

with this you can use bootstrap also for the pagination view. You must query your database exactly with the per page you want to saw.

Upvotes: 0

swatkins
swatkins

Reputation: 13630

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.

Upvotes: 2

Related Questions