1

你好我的分页不工作。

这是我的控制器类。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);

    }

客户模型.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 条记录。而且我的分页链接也没有显示。我在这里做错了什么?非常感谢您的帮助。谢谢。

4

3 回答 3

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");

希望这可以帮助

于 2013-11-14T06:33:14.120 回答
0

试试这个

模型:

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 注入

于 2013-11-14T06:33:23.347 回答
0

尝试更换

$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();
于 2013-11-14T04:13:09.423 回答