我在CodeIgniter中工作分页。我也看到了教程和用户手册。但我无法找到正确的方法,他们在Controller中调用数据库进行分页。我想要一个合适的解决方案
这是控制器
public function index() {
$this->load->library('pagination');
$config['base_url'] = 'index.php/Emp';
$config['total_rows'] = 200;
$config['per_page'] = 2;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$data = array();
$data['showEmployeeTable']=$this->Em->selectEmployeeData(); //FOR SHOWING A EMPLOYEE DATABASE TABLE
if($query = $this->Em->getDesignationData()) //grabs all record
{
$data['records'] = $query;
}
if($query = $this->Em->getCityRecord()) //grabs all record
{
$data['records_city'] = $query;
}
//$this->load->view('emp_update', $data);
$this->load->view('erv', $data);
}
这是我的模特
public function getDesignationData() {
$query = $this->db->get('emp_desig'); //model created for get data
return $query->result();
}
public function getCityRecord() {
$query = $this->db->get('city'); //model created for get data
return $query->result();
}
// ***************** VEDDING PLAN MODELS **********************************************************
public function selectEmployeeData() {
$query = $this->db->get('emp_manage');
return $query;
}
那么我怎样才能在View页面上显示正确的分页?请一步一步地回答我。我是Codeigniter的新手。
这在视图中。
<?php echo $this->pagination->create_links(); ?>
答案 0 :(得分:3)
你的控制器应该是
public function index() {
$this->load->library('pagination');
$config = array();
$config['base_url'] = 'index.php/Emp';
$config['total_rows'] = 200;
$config['per_page'] = 2;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['showEmployeeTable']=$this->Em->selectEmployeeData($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
if($query = $this->Em->getDesignationData()) //grabs all record
{
$data['records'] = $query;
}
if($query = $this->Em->getCityRecord()) //grabs all record
{
$data['records_city'] = $query;
}
$this->load->view('erv', $data);
}
你的模特应该是
public function selectEmployeeData($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get('emp_manage');
return $query;
}
在你的观点中添加以下行
<?php echo $links; ?>
答案 1 :(得分:0)
查看此代码并根据您的要求进行一些更改:
你的控制器应该是:
<html>
<head>
<script src="js/jquery.min.js"></script>
<script language="Javascript" type="text/javascript">
$(document).ready(function() {
var regex_userId = /^[0-9\\ ]{2,50}$/;
var $contactno = $('#contactno');
$('.status').hide();
$('#btn_click').click(function() {
if ($contactno.val().length < 1) {
$('.status').show().text("Please enter a value.");
} else {
if (regex_userId.test($contactno.val())) {
// Show below as .status
$('.status').show().text("valid");
} else {
$('.status').show().text("Contact number must be a numeric value (0 - 9).");
}
}
});
});
</script>
</head>
<body>
<h3 class='status'></h3>
<input type='text' id='contactno' />
<button class='js-validator' id='btn_click'>click</button>
</body>
</html>
您的型号:
function list($offset=0)
{
$num_rows=$this->db->count_all("table_name");
$config['base_url'] = site_url('list');
$config['total_rows'] = $num_rows;
$config['per_page'] = 100;
$config['num_links'] = round($config['total_rows']/$config['per_page']);
//$config['use_page_numbers'] = TRUE;
$config['page_query_string']=TRUE;
$this->pagination->initialize($config);
$data["results"] = $this->Em->selectEmployeeData($config["per_page"], $page);
$this->load->view('viewfile',$data);
}
在视图中
function selectEmployeeData($limit,$offset)//fetch all data with pagination
{
$data = array();
$this->db->limit($limit, $offset);
$Q = $this->db->get('table');
if($Q->num_rows() > 0)
{
foreach ($Q->result_array() as $row)
{
$data[] = $row;
}
}
$Q->free_result();
return $data;
}