Я создал некоторых пользователей в своей панели, а затем назначил им некоторую информацию / данные в соответствии с правами пользователя.
Мой вопрос Я хочу отображать и искать только данные «кто в сети», или пользователь при входе может видеть и искать только те данные, которые ему / ей назначены администратором на стороне сервера DataTable.
Пожалуйста помоги
Вот мой код ниже
HTML-код
<table id="table" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Type</th>
<th>Email</th>
<th>Country</th>
<th>Username</th>
<th>Mobile</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<script type="text/javascript">
var table;
$(document).ready(function() {
table = $('#table').DataTable({
"processing": true, //Feature control the processing indicator.
"serverSide": true, //Feature control DataTables' server-side processing mode.
"responsive": true,
"iDisplayLength": 50,
// Load data for the table's content from an Ajax source
"ajax": {
"url": "<?php echo site_url('customer/ajax_list')?>",
"type": "POST"},
//Set column definition initialisation properties.
"columnDefs": [
{
"targets": [ -1 ], //last column
"orderable": false, //set not orderable
},
],
});
});
</script>
</tbody>
</table>
контроллер
public function ajax_list() {
$table = 'rs_tbl_users_data'; # DATABASE TABLE NAME
if( $this->session->userdata('user_id') == 1 &&
$this->session->userdata('user_type') == 1) {
$wherUserID_FK = ''; # WHERE CONDITION FOR ADMIN
# empty is for admin to see all data
}
else {
# WHERE CONDITION FOR LOGIN USER
$wherUserID_FK = array('user_id_fk' => $this->session->userdata('user_id'));
}
$column = array('user_id','username','email','first_name','mobile','reg_date','status','user_type','country','user_id_fk');
$order = array('user_id' => 'DESC');
$list = $this->dataTable->get_datatables($table, $column, $wherUserID_FK, $order);
$data = array();
$no = $_POST['start'];
foreach ($list as $person) {
$no++;
$row = array();
$row[] = $person->user_id;
$row[] = $person->first_name;
// USER TYPE IS USER IS ADMIN / RESELLER OR ONLY USER
if( $person->user_type == 1){
$userType = 'Admin';
} else if( $person->user_type == 2 ){
$userType = 'Reseller';
} else if( $person->user_type == 3) {
$userType = 'Customer';
} else {
$userType = '';
}
$row[] = '<button type="button" class="btn btn-xs btn-warning">'.$userType.'</button>';
$row[] = $person->email;
if( !empty($person->country) ) {
$cntry = $this->countryName( $person->country );
} else {
$cntry = 'No Country Found.';
}
$row[] = '<button type="button" class="btn btn-xs btn-primary">'.$cntry.'</button>';
$row[] = $person->username;
if( !empty($person->mobile) ){
$contactNum = $person->mobile;
} else {
$contactNum = 'No Contact';
}
$row[] = $contactNum;
# STATUS RECORD SECTION START
if($person->status == 1 ) {
$status = '<a href="" class="btn btn-xs btn-info">Active</a>';
} else {
$status = '<a href="" class="btn btn-xs btn-danger">InActive</a>';
}
$row[] = $status;
# STATUS RECORD SECTION END
//ADD HTML FOR ACTION / OPERATIONS
$row[] = '<div class="btn-group btn-group-xs" role="group" aria-label="Action Buttons">
<a class="btn btn-sm btn-success" href="" title="Edit"><i class="glyphicon glyphicon-pencil"></i> Edit</a>
<a class="btn btn-sm btn-danger" href="" title="Hapus"><i class="glyphicon glyphicon-trash"></i> Delete</a></div>';
$data[] = $row;
}
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->dataTable->count_all($table, $wherUserID_FK),
"recordsFiltered" => $this->dataTable->count_filtered($table, $wherUserID_FK),
"data" => $data,
);
//output to json format
echo json_encode($output);
}
модель
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Datatable_model_new extends CI_Model {
var $table;
var $column;
var $order;
var $where;
public function __construct()
{
parent::__construct();
$this->load->database();
}
private function _get_datatables_query($tableName, $where)
{
$this->db->from($tableName);
if( !empty($where) ) {
$this->db->where($where);
}
$i = 0;
foreach ($this->column as $item)
{
if($_POST['search']['value']) {
if( $i === 0){
$this->db->like($item, $_POST['search']['value']);
}
else {
$this->db->or_like($item, $_POST['search']['value']);
}
}
$column[$i] = $item;
$i++;
}
if(isset($_POST['order']))
{
# FOR DESC
$this->db->order_by($column[$_POST['order']['0']['column']], 'DESC');
}
else if(isset($this->order))
{
$order = $this->order;
$this->db->order_by(key($order), $order[key($order)]);
}
//echo $query = $this->db->last_query();exit;
}function get_datatables($tableName, $columnArray, $where, $orderBY)
{
$this->table = $tableName;
$this->column = $columnArray;
$this->order = $orderBY;
$this->where = $where;
if( !empty($where) ){
$this->where = $where;
}
$this->_get_datatables_query($tableName, $where);
$term = $_REQUEST['search']['value'];
//$this->_get_datatables_query($term, $this->table, $this->where);
if($_POST['length'] != -1)
$this->db->limit($_POST['length'], $_POST['start']);
//$this->db->from($tableName);
$query = $this->db->get();
//echo $query = $this->db->last_query();exit;
return $query->result();
}
function count_filtered($tableName = NULL, $where = NULL)
{
//$this->_get_datatables_query();
$this->db->from($tableName);
if( !empty($where) ) {
$this->db->where($where);
}
$query = $this->db->get();
//echo $query = $this->db->last_query();exit;
return $query->num_rows();
}
public function count_all($tableName = NULL, $where = NULL)
{
$this->db->from($tableName);
if( !empty($where) ) {
$this->db->where($where);
}
//echo $query = $this->db->last_query();exit;
return $this->db->count_all_results();
}
public function get_by_id($id)
{
$this->db->from($this->table);
//$this->db->where('channel_id',$id);
$query = $this->db->get();
return $query->row();
}
public function save($data)
{
$this->db->insert($this->table, $data);
return $this->db->insert_id();
}
public function update($where, $data)
{
$this->db->update($this->table, $data, $where);
return $this->db->affected_rows();
}
public function delete_by_id($id)
{
//$this->db->where('channel_id', $id);
$this->db->delete($this->table);
}
}
Задача ещё не решена.
Других решений пока нет …