【问题标题】:pagination not working properly pages are not changing [duplicate]分页无法正常工作页面没有改变[重复]
【发布时间】:2013-03-07 07:18:21
【问题描述】:

嗨,我是 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);
       } 
      ?>       

【问题讨论】:

标签: php codeigniter pagination


【解决方案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();

}

【讨论】:

  • tnx 的答案,但它并没有改变它仍然以相同的方式执行的任何内容。你还有其他建议吗?
  • 在您的模型中请包含echo $this-&gt;db-&gt;last_query(); 在此处复制粘贴结果并将结果粘贴到您的 phpmyadmin 查询字段中,看看您是否真的得到任何结果
  • 对不起,我很难理解你能用更多的细节吗?
  • 我修改了我的第一个答案,用新的替换你的模型函数
  • SELECT tblanswers.*, credentials.* FROM (tblanswers) LEFT JOIN credentials ON tblanswers.answerid = credentials.cid
【解决方案2】:

控制器:

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

}

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

【讨论】:

    【解决方案3】:

    对 sql 查询使用限制函数。例如,如果您想在第一次使用 sql 查询限制 0,10 时在页面上显示 10 条记录

    【讨论】:

    • 这个我知道问题是所有的记录都被显示了并且分页没有正确运行我想我需要一个偏移量之类的东西,但我不知道如何实现它
    • 使用此代码定义偏移量,并在使用此方法之前加载uri helper $uri_segment = 3; $offset = $this->uri->segment($uri_segment);
    猜你喜欢
    • 2012-08-04
    • 1970-01-01
    • 2013-12-14
    • 2014-09-21
    • 2013-08-22
    • 2012-09-25
    • 2016-07-29
    • 2017-11-27
    • 1970-01-01
    相关资源
    最近更新 更多