0

嗨,我是 codeigniter 的新手,并试图在我的项目中构建一个分页功能,看起来一切正常,除了分页没有改变页面。畏惧知道出了什么问题,谁能帮助我?这是我的代码:

控制器

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;
        $config['uri_segment'] = 3;

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

        $data['records'] = $this->Result_model->result_getall($config['per_page'], $this->uri->segment(3));
        $data['pagination'] = $this->pagination->create_links();
        $this->load->view('result_view', $data);

模型

       function result_getall(){

  $this->db->select('tblanswers.*,credentials.*');
  $this->db->from('tblanswers');
  $this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'LEFT'); 
  $query = $this->db->get();
  return $query->result();

    }

看法

<?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);
       } 
      ?>       
4

3 回答 3

1

试试这个:
将此部分添加到控制器

$page = $this->uri->segment(3);
if($page == '')
{
    $page = 1;
}

并修改这一行

$data['records'] = $this->db->get('tblanswers, credentials', $config['per_page'], $page)->result_array();

编辑:更改您的模型:

function result_getall()
{

    $this->db->select('tblanswers.*,credentials.*');
    $this->db->from('tblanswers');
    $this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'LEFT'); 
    $query = $this->db->get();
    //return $query->result();
    echo $this->db->last_query();

}
于 2013-03-07T07:28:43.053 回答
0

控制器:

 function getall()
 {

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

    $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'] = 2;
    $config['num_links'] = 5;
    $config['uri_segment'] = 3;
    $this->pagination->initialize($config);
    /*
    CHANGE $this->YOURMODELNAME-> to the name of the model
    */
    $data['records'] = $this->YOUMODELNAME->result_getall($config['per_page'], $this->uri->segment(3));
    $data['pagination'] = $this->pagination->create_links();
    $this->load->view('result_view', $data);
}

模型:

function result_getall($perPage, $page)
{

    $this->db->select('tblanswers.*,credentials.*');
    $this->db->from('tblanswers');
    $this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'LEFT'); 
    $this->db->limit($perPage, $page);
    $query = $this->db->get();
    return $query->result();

}

我已将您需要修改的部分放在大写字母中。让我知道这是否可行

于 2013-03-07T08:23:21.477 回答
0

use limit function on sql query. for ex if you want to show 10 record on page use in sql query limit 0,10 first time

于 2013-03-07T13:20:15.673 回答