分页不会改变页面

时间:2013-03-06 11:30:08

标签: php codeigniter pagination

我正在为我的网站进行分页,我正在显示数据库中的两个表格中的数据我在页面中有分页但它的行为并不像它假设表现的那样不会改变页面。目前我在网页上显示了16条记录,并设置了$config['per_page'] = 1;,分页栏最多可达16条,但不会翻页,所有记录都显示在页面上。任何帮助将在这里得到赞赏是我的代码:

控制器

 <?php

class Result_controller extends CI_Controller{

    function getall(){

        $this->load->model('result_model');
        $data['query'] = $this->result_model->result_getall();
        // print_r($data['query']); die();


        $this->load->library('pagination');

        $config['base_url'] = 'http://localhost/Surva/index.php/result_controller/getall';
        $config['total_rows'] = $this->db->get('tblanswers, credentials')->num_rows();
        $config['per_page'] = 1;
        $config['num_links'] = 10;


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

        $data['records'] = $this->db->get('tblanswers, credentials', $config['per_page'], $this->uri->segment(1, 0))->result_array();
        $data['pagination'] = $this->pagination->create_links();
        $this->load->view('result_view', $data);




        }


}

?>

查看

   <div>    






     <table border="1">


      <tr>
         <th>Name</th>
         <th>Second Name</th>
         <th>Phone</th>
         <th>Email</th>
         <th>Answer</th>
         <th>Comment</th>
     </tr>
      <?php foreach ($query as $row): ?> 
     <tr>

         <td><?php echo $row->name; ?></td>
         <td><?php echo $row->second_name; ?></td>
         <td><?php echo $row->phone; ?></td>
         <td><?php echo $row->email; ?></td>
          <td> <?php echo $row->answerA;?>
          <?php echo $row->answerB;?>
          <?php echo $row->answerC;?></td>
         <td><?php echo $row->comment;?><br></td>

     </tr>

      <?php endforeach; ?>

     </table>  
     <?php if (isset($pagination))
      {
       echo $pagination;
      // echo "<pre>"; var_dump($query);
       } ?>

    

模型

function result_getall(){

   return $this->db->select('tblanswers.*,credentials.*')
                         ->from('tblanswers, credentials')
                         ->get()
                         ->result_object();

3 个答案:

答案 0 :(得分:1)

查询并不神奇地知道您只需要x个记录。 Codeigniter将显示您从模型中提供的任何内容。

function getall(){

        $this->load->model('result_model');
        // print_r($data['query']); die();


        $this->load->library('pagination');

        $config['base_url'] = 'http://localhost/Surva/index.php/result_controller/getall';
        $config['total_rows'] = $this->db->get('tblanswers, credentials')->num_rows();
        $config['per_page'] = 1;
        $config['num_links'] = 10;
        $config['uri_segment'] = 3; //guessing here, but this is where the uri segment you use to change pages goes.
        if($this->uri->segment('3')) {
        $offest = $this->uri->segment('3');
        } else {
        $offest = 0;
        }
        $data['query'] = $this->result_model->result_getall($config['per_page'],$offset);


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

        $data['records'] = $this->db->get('tblanswers, credentials', $config['per_page'], $this->uri->segment(1, 0))->result_array();
        $data['pagination'] = $this->pagination->create_links();
        $this->load->view('result_view', $data);




        }



Model:

function result_getall($limit=0,$offset=0){

   if($limit != 0)
   {
   return $this->db->select('tblanswers.*,credentials.*')
                         ->from('tblanswers, credentials')
                         ->limit($limit, $offset)
                         ->get()
                         ->result_object();
   } else {
   return $this->db->select('tblanswers.*,credentials.*')
                         ->from('tblanswers, credentials')
                         ->get()
                         ->result_object();
   }

注意在控制器中我将偏移量和每页传递给模型,然后使用它们来限制模型本身的返回值。

答案 1 :(得分:0)

你应该循环$记录而不是$ query。 $ query包含所有记录。

答案 2 :(得分:0)

这样做

    $this->load->library('pagination');
    $limit = 10;

    $total = $this->legend_model->get_legend_count($language_id);

    $config['base_url'] = base_url().'legend/index/';
    $config['total_rows'] = $total;
    $config['per_page'] = $limit;
    $config['uri_segment'] = 3;

    $config['first_link'] = '<< First';
    $config['last_link'] = 'Last >>';
    $config['next_link'] = 'Next ' . '&gt;';
    $config['prev_link'] = '&lt;' . ' Previous';
    $config['num_tag_open'] = '<span class="number">';
    $config['num_tag_close'] = '</span>';

    $config['cur_tag_open'] = '<span class="current"><a href="#">';
    $config['cur_tag_close'] = '</a></span>';

    $this->pagination->initialize($config);
    $data['offset'] = $offset;
    $data['legends'] = $this->legend_model->get_legend($language_id, $limit, $offset);

    $this->template->write('title', 'Legend : Manage Legend');
    $this->template->write_view('content', 'legend/index', $data);
    $this->template->render();