代码点火器分页未显示每页的正确行


CodeIgniter Pagination not showing the correct rows per page

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

这是我的控制器类。 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
        foreach ($customers as $key => $value)
        {
                echo '<p>'. $value->customercode . '</p>';
                echo '<p>'. $value->customername . '</p>';
        }
        echo $links;
        ?>

也取消评论

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

同时将查询更改为仅

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

希望这有帮助

尝试替换

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

试试这个

型:

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注入的影响