【问题标题】:pagination does not change page分页不改变页面
【发布时间】:2013-02-21 04:12:52
【问题描述】:

我正在为我的网站进行分页,我正在显示来自数据库上两个表的数据,我的页面中有分页,但它的行为不像它假设的那样,它不会改变页面。目前我在网页上显示了 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();

【问题讨论】:

  • 您是否在查询中设置了限制?同时显示您的型号代码。
  • 模型是一个简单的获取所有功能
  • 就是这个问题,模型需要限制和偏移,看我的回答。
  • 检查 $this->uri->segment(1, 0) 返回的内容..
  • 试试@karmafunk 的建议。我认为它会工作

标签: php codeigniter pagination


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

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

【讨论】:

  • 我认为他使用此函数来获取所有记录以分配给 $config['total_rows']。但他也没有那样做。通过获取此查询的结果,他什么也不做。有点混乱
  • 没错,我会解决这个问题,我个人会采取不同的做法,但我不会重写他的所有代码。
  • 我编辑了我的控制器和模型作为您的示例现在我收到此错误遇到 PHP 错误严重性:通知消息:未定义变量:偏移量文件名:控制器/result_controller.php 行号:20 任何建议?
  • 查看编辑,我没有考虑到您可能在第 1 页上没有 uri 段。我倾向于在基本页面上传递该段以避免这种情况。请记住,uri 段必须与您的分页创建它的位置相匹配。
  • tnx 为您提供帮助,我在使用您的建议时遇到了几个错误,以下是错误:else 附近有大括号,veriambel $offset 使用了 2 个分配。
【解决方案2】:

您应该循环通过 $records 而不是 $query。 $query 包含所有记录。

【讨论】:

  • 你能给我举个例子吗,因为我是编程新手,我很害怕 tnx 发帖
  • 这是答案的基础,但实际上相当于“Google it”,这并不是很有帮助。
【解决方案3】:

这样做

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-16
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 2021-01-09
    • 2023-03-30
    • 2014-10-02
    • 1970-01-01
    相关资源
    最近更新 更多