I thought I might share a sortable table code example that I made.
I have tried a couple that I found on git hub but were out dated and did not work. So made my own, this code below should work with codeigniter 2.2.1 and codeigniter 3 which what this is made on.
Here is just a example of how to do sort th table table links with codeigniter model.
You can change the code around any way you like.
Routes.php
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$route['admin/user/users_groups'] = "admin/user/users_groups/index"; // Needed
$route['admin/user/users_groups/(:any)/(:any)'] = "admin/user/users_groups/index/$1$2"; // Need the $1$2
?>
Example Controller:
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Demo extends CI_Controller {
public function index() {
$sort = $this->uri->segment(4);
if (!$sort) {
$sort = 'asc';
}
$this->getList('name, user_group_id', $sort);
}
public function getList($sort = 'name, user_group_id', $order = 'asc', $offset = 0) {
$data['users_groups'] = array();
$filter_data = array(
'limit' => "20",
'offset' => $this->uri->segment(5),
'sort' => $sort,
'order' => $order
);
$total_rows = $this->model_user_user_group->getTotalUserGroups();
$results = $this->model_user_user_group->getUserGroups($filter_data);
foreach ($results as $result) {
$data['users_groups'][] = array(
'user_group_id' => $result['user_group_id'],
'name' => $result['name']
);
}
$this->load->library('pagination');
$config['base_url'] = base_url('admin/user/users_groups/'.$order);
$config['total_rows'] = $total_rows;
$config['per_page'] = "20";
$config['num_links'] = 16;
$config['full_tag_open'] = "<ul class='pagination'>";
$config['full_tag_close'] ="</ul>";
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
$config['next_tag_open'] = "<li>";
$config['next_tagl_close'] = "</li>";
$config['prev_tag_open'] = "<li>";
$config['prev_tagl_close'] = "</li>";
$config['first_tag_open'] = "<li>";
$config['first_tagl_close'] = "</li>";
$config['last_tag_open'] = "<li>";
$config['last_tagl_close'] = "</li>";
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$rep_order = '';
if ($order) {
if ($order == 'asc') {
$rep_order = 'desc';
} elseif ($order == 'desc') {
$rep_order = 'asc';
}
$data['order'] = site_url('admin/user/users_groups/'. $rep_order);
} else {
$data['order'] = site_url('admin/user/users_groups/'. $rep_order);
}
return $this->load->view('template/user/user_group_list', $data);
}
}
?>
Example Users Model:
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_user_user_group extends CI_Model {
public function getUserGroups($data = array()) {
$data['order'] = ($data['order'] == 'desc') ? 'desc' : 'asc';
$sort_columns = array('user_group_id', 'name');
$data['sort'] = (in_array($data['sort'], $sort_columns)) ? $data['sort'] : 'name';
$this->db->select('user_group_id, name');
$this->db->from($this->db->dbprefix . 'user_group');
$this->db->limit($data['limit'], $data['offset'] == 1 ? 0 : $data['offset']);
$this->db->order_by($data['sort'], $data['order']);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
}
public function getTotalUserGroups() {
$query = $this->db->query("SELECT COUNT(*) AS total FROM " . $this->db->dbprefix . "user_group");
return $query->row('total');
}
?>
Example View:
Code:
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<?php if ($this->uri->segment(4) == true) { ?>
<th class="text-left"><a href="<?php echo $order;?>" class="<?php echo $this->uri->segment(4);?>">User Group ID</a></th>
<?php } else { ?>
<th class="text-left"><a href="<?php echo $order;?>" class="asc">User Group ID</a></th>
<?php }?>
<?php if ($this->uri->segment(4) == true) { ?>
<th class="text-left"><a href="<?php echo $order;?>" class="<?php echo $this->uri->segment(4);?>">Name</a></th>
<?php } else { ?>
<th class="text-left"><a href="<?php echo $order;?>" class="asc">Name</a></th>
<?php }?>
<td class="text-right">Action</td>
</tr>
</thead>
<tbody>
<?php if ($users_groups) { ?>
<?php foreach ($users_groups as $user_group) { ?>
<tr>
<td class="text-left"><?php echo $user_group['user_group_id']; ?></td>
<td class="text-left"><?php echo $user_group['name']; ?></td>
</tr>
<?php } ?>
<?php } else { ?>
<tr>
<td class="text-center" colspan="3">No Results</td>
</tr>
<?php } ?>
<br/>
<br/>
<?php echo $pagination; ?>
</tbody>
</table>
</div>
There's only one rule - please don't tell anyone to go and read the manual. Sometimes the manual just SUCKS!