繁体   English   中英

CodeIgniter分页每页显示的行数不正确

[英]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注入

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM