簡體   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