分页Codeigniter

时间:2011-10-31 13:40:51

标签: codeigniter pagination

我正在使用Codeigniter ..我是新手。我想要做的是在我的代码中使用分页,但问题是分页链接没有显示在底部。

控制器:

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');

  }
}

模特:

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;
  }
}

观点:

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

为什么没有显示分页链接?

3 个答案:

答案 0 :(得分:2)

您已经在控制器中创建了链接:

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

因此您只需在视图中回显$pagination

<?php echo $pagination; ?>

您没有在视图中加载分页库,只加载了控制器 - 因此您无法在视图中使用create_links()方法。但是,由于您确实在控制器中使用了该方法并将其传递到$data数组中的视图,因此您可以在视图中使用该变量。

答案 1 :(得分:0)

$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);

这是我在我的控制器中的功能! 并且在视图中我有这个

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

有了这个,你也可以使用bootstrap作为分页视图。 您必须使用要查看的每页完全查询数据库。

答案 2 :(得分:0)

在CodeIgniter中搜索文章

第1步:创建模型搜索功能。

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

第2步:在控制器中创建搜索功能。

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'));
}

第3步:在视图中创建search_results。

<?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');?>