你好,我的分页不起作用。
这是我的控制器类。的 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条记录。而且我的分页链接也不显示。我在这里做错了什么?非常感谢帮助。感谢。
答案 0 :(得分: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");
希望这会有所帮助
答案 1 :(得分: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();
答案 2 :(得分: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注入