【问题标题】:CodeIgniter Pagination not showing the correct rows per pageCodeIgniter 分页未显示每页正确的行
【发布时间】:2013-11-14 03:30:46
【问题描述】:

你好,我的分页不起作用。

这是我的控制器类。 customer_controller.php

     public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->model('customer_model');
        $this->load->library('pagination');


    }
    public function index()
    {


        $data['title']= 'Customer';
        $data['records'] = $this->customer_model->getAll();
        $data['groups'] = $this->customer_model->getAllCustomerGroups();
        $data['groupcodes'] = $this->customer_model->getAllCustomerGroups();

        $config['base_url'] = 'customers';
        $config['total_rows'] = $this->customer_model->record_count();
        $config['per_page'] = 1;

        $config["uri_segment"] = 3;

        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data["customers"] = $this->customer_model->fetch_customers($config["per_page"], $page);
        $data["links"] = $this->pagination->create_links();


        //$this->pagination->initialize($config);
        //echo $this->pagination->create_links();

        $this->load->view('include/header',$data);
        $this->load->view('include/navbar',$data);
        $this->load->view('customer_view', $data);
        $this->load->view('include/sidebar',$data);
        $this->load->view('include/footer',$data);

    }

Customer_model.php

   public function fetch_customers($limit, $start) {
            $this->db->limit($limit, $start);
            $query = $this->db->query('SELECT customercode, customername, customergroup, customertype, customeraddress, website FROM customercard');

            if ($query->num_rows() > 0) {
                foreach ($query->result() as $row) {
                    $data[] = $row;
                }
                return $data;
            }
            return false;
        }

customer_view.php

       <?php

            foreach ($customers as $key => $value)
            {
                    echo '<p>'. $value->customercode . '</p>';
                    echo '<p>'. $value->customername . '</p>';
            }

            ?>

我的问题是,它只是显示所有记录,而不是一次只显示 1 条记录。而且我的分页链接也没有显示。我在这里做错了什么?非常感谢您的帮助。谢谢。

【问题讨论】:

    标签: php codeigniter pagination


    【解决方案1】:

    您的分页链接将显示为

     <?php
    
            foreach ($customers as $key => $value)
            {
                    echo '<p>'. $value->customercode . '</p>';
                    echo '<p>'. $value->customername . '</p>';
            }
            echo $links;
    
            ?>
    

    也取消注释

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

    还将您的查询更改为 only

    //    $this->db->limit($limit, $start);// <-- remove this
    
            $query = $this->db->query("SELECT customercode, customername, customergroup, customertype, customeraddress, website FROM customercard limit $start, $limit");
    

    希望对你有帮助

    【讨论】:

    • +1 取消注释分页初始化语句。这也是我看到的第一个错误,因此不想为它创建新的答案。
    【解决方案2】:

    尝试替换

    $this->db->limit($limit, $start);
    $query = $this->db->query('SELECT customercode, customername, customergroup, customertype, customeraddress, website
     FROM customercard');
    

    到

    $this->db->select('customercode, customername, customergroup, customertype, customeraddress, website')->from('customercard')->limit($limit, $start)->get();
    

    【讨论】:

      【解决方案3】:

      试试这个

      型号:

      public function fetch_customers($limit, $offset) {
          $this->db->select('customercode, customername, customergroup, customertype, customeraddress, website');
          $query=$this->db->get('t_candidate', $limit, $offset);
      
          if ($query->num_rows() > 0) {
              return $query->result_array();
          }else{
              return false;
          }
      }
      

      查看: 在视图中将以下链接粘贴到您要显示链接的任何位置

       <?php 
       foreach ($customers as $key => $value)
              {
                      echo '<p>'. $value['customercode'] . '</p>';
                      echo '<p>'. $value['customername'] . '</p>';
              }
        ?>
      
       <?php echo $this->pagination->create_links(); ?>
      

      控制器: 取消注释以下行

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

      另外我宁愿建议您尝试使用 codeigniter 活动记录,因为它可以保护您的数据库免受 sql 注入

      【讨论】:

        猜你喜欢
        • 2012-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-26
        • 1970-01-01
        • 1970-01-01
        • 2013-04-07
        • 1970-01-01
        相关资源
        最近更新 更多