Pagination In php Codeigniter With Bootstrap CSS
Model Code:--
<?php
class department_model extends CI_Model{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
//fetch department details from database
function get_department_list($limit, $start)
{
$sql = 'select var_dept_name, var_emp_name from tbl_dept, tbl_emp where tbl_dept.int_hod =
tbl_emp.int_id order by var_dept_name limit ' . $start . ', ' . $limit;
$query = $this->db->query($sql);
return $query->result();
}
}
?>
Controller Code:--
<?php
class department extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->database();
$this->load->library('pagination');
//load the department_model
$this->load->model('department_model');
}
public function index()
{
//pagination settings
$config['base_url'] = site_url('department/index');
$config['total_rows'] = $this->db->count_all('tbl_dept');
$config['per_page'] = '5';
$config['uri_segment'] = 3;
$choice = $config['total_rows'] / $config['per_page'];
$config['num_links'] = floor($choice);
//config for bootstrap pagination class integration
$config['full_tag_open'] = '<ul class='pagination'>';
$config['full_tag_close'] = '</ul>';
$config['first_link'] = false;
$config['last_link'] = false;
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = '«';
$config['prev_tag_open'] = '<li class='prev'>';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '»';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class='active'><a href='#'>';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
$data['page'] = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
//call the model function to get the department data
$data['deptlist'] = $this->department_model->get_department_list($config['per_page'], $data['page']);
$data['pagination'] = $this->pagination->create_links();
//load the department_view
$this->load->view('department_view',$data);
}
}
?>
View Code:--
<!DOCTYPE html>
<html>
<head>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<title>PHP CodeIgniter Pagination with Bootstrap Styles | Example</title>
<!--link the bootstrap css file-->
<link rel='stylesheet' href='<?php echo base_url('bootstrap/css/bootstrap.css'); ?>'>
</head>
<body>
<div class='container'>
<div class='row'>
<div class='col-md-12'>
<table class='table table-striped table-hover'>
<thead>
<tr>
<th>#</th>
<th>Department Name</th>
<th>Head of Department</th>
</tr>
</thead>
<tbody>
<?php for ($i = 0; $i < count($deptlist); ++$i) { ?>
<tr>
<td><?php echo ($page+$i+1); ?></td>
<td><?php echo $deptlist[$i]->var_dept_name; ?></td>
<td><?php echo $deptlist[$i]->var_emp_name; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<div class='row'>
<div class='col-md-12 text-center'>
<?php echo $pagination; ?>
</div>
</div>
</div>
</body>
</html>
0 comments:
Post a Comment