Использование серверных данных в Codeignier
мой контроллер код:
public function ajax_list(){
try {
$adfs_login = unserialize($_SESSION['AdfsUserDetails']);
// echo "<pre>";
// print_r($adfs_login);
if (!empty($this->input->post('for')) && $this->input->post('for') == 'OrderList') {
$column_order = array('a.order_id', 'a.customer_id', 'a.patient_id', 'a.colleague_name', 'a.type',
'a.store_num', 'a.store_name', 'a.status_code',
'a.is_same_as_last_month', 'a.collection_time', 'a.create_date_time', 'a.remarks',
'a.last_update_date_time', 'a.hkpg_flag', 'a.store_phone'
, 'b.customer_id', 'b.title', 'b.fname', 'b.lname',
'b.dob', 'b.IS_MEDICAL_CARD', 'b,SCHEME_NUMBER', 'b.STATUS', 'c.building_name', 'c.house_unit',
'c.house_num', 'c.STREET_ADDRESS', 'c.locality'
, 'c.town', 'c.COUNTRY', 'c.post_code', 'c.country');
$column_search = array_map('strtoupper', array_change_key_case(array('b.fname','b.lname'), CASE_UPPER));
$order = array_map('strtoupper', array_change_key_case(array('a.order_id' => 'desc'), CASE_UPPER));
$where = '';
if ($adfs_login->attributes['Group'][0] == 'APP_IM_APP_Boots_ROI_Prescription_'.$this->project_phase.'_Store-User') {
$where = array_map('strtoupper', array_change_key_case(array('a.store_num' =>
$adfs_login->attributes['storeNumber'][0]), CASE_UPPER));
} else if ($adfs_login->attributes['Group'][0] == 'APP_IM_APP_Boots_ROI_Prescription_'.$this->project_phase.'_CSC-Agent') {
// $where = array_map('strtoupper',array_change_key_case(array('a.store_num'=>$this->input-post('store_id')),CASE_UPPER));
$where = '';
} else if ($adfs_login->attributes['Group'][0] == 'APP_IM_APP_Boots_ROI_Prescription_'.$this->project_phase.'_Admins') {
// $where = array_map('strtoupper',array_change_key_case(array('a.store_num'=>$this->input-post('store_id')),CASE_UPPER));
$where = '';
}
if (!empty($this->input->post('store_id')) && $this->input->post('store_id') != '-1') {
$where = array_map('strtoupper', array_change_key_case(array('a.store_num' =>
$this->input->post('store_id')), CASE_UPPER));
}
$join_array = array_map('strtoupper', array_change_key_case(array("patients b" =>
"a.patient_id=b.patient_id", "patient_addresses c" =>
"a.patient_id=c.patient_id"), CASE_UPPER));
$select = "a.order_id,a.customer_id as cust_id,a.patient_id,a.colleague_name,a.type,a.store_num,a.store_name,a.status_code,
a.is_same_as_last_month,a.collection_time,a.create_date_time,a.remarks,a.last_update_date_time,a.hkpg_flag,a.store_phone
,b.customer_id,b.patient_id as p_id,b.title,b.fname,b.lname,b.dob,b.status,c.building_name,
c.house_unit,c.house_num,c.STREET_ADDRESS,c.locality
,c.town,c.COUNTRY,c.post_code";
}
$list = $this->adfs->get_datatables('prescriptions a', $column_order, $column_search, $order, $where, $join_array, $select);
// print_r($this->db->last_query());die;
$sn = 0;
$chatList = array();
foreach ($list as $Chat_list) {
$chatList[$sn] = $Chat_list;
$sn++;
}
$i = 1;
// $this->load->helper('word_limit');
// $row=array();
foreach ($list as $newchatList) {
$data = array();
if (!empty($this->input->post('for')) && $this->input->post('for') == 'OrderList') {
$data[] = $newchatList->CREATE_DATE_TIME;
}
$order_id = '';
if ($adfs_login->attributes['Group'][0] == 'APP_IM_APP_Boots_ROI_Prescription_'.$this->project_phase.'_Store-User')
{
$order_id = '<a href="'.base_url('prescription-details/'.urlencode(base64_encode($newchatList->ORDER_ID))).'">'.$newchatList-ORDER_ID.'</a>';
}else{
$order_id = $newchatList->ORDER_ID;
}
$data[] = $order_id;
$data[] = $newchatList->FNAME . ' ' . $newchatList->LNAME;
$addresses = '';
if(!empty($newchatList->HOUSE_NUM)){
$addresses.=$newchatList->HOUSE_NUM.', ';
}
if(!empty($newchatList->BUILDING_NAME)){
$addresses.=$newchatList->BUILDING_NAME.', ';
}
if(!empty($newchatList->STREET_ADDRESS)){
$addresses.=$newchatList->STREET_ADDRESS.', ';
}
if(!empty($newchatList->TOWN)){
$addresses.=$newchatList->TOWN.', ';
}
if(!empty($newchatList->LOCALITY)){
$addresses.=$newchatList->LOCALITY.', ';
}
if(!empty($newchatList->POST_CODE)){
$addresses.=$newchatList->POST_CODE.', ';
}
if(!empty($newchatList->COUNTRY)){
$addresses.=$newchatList->COUNTRY;
}
$data[] = $addresses;
$data[] = $newchatList->DOB;
$type = '';
if ($adfs_login->attributes['Group'][0] == 'APP_IM_APP_Boots_ROI_Prescription_'.$this->project_phase.'_Store-User')
{
$type = ucwords($newchatList->TYPE);
} else if ($adfs_login->attributes['Group'][0] == 'APP_IM_APP_Boots_ROI_Prescription_'.$this->project_phase.'_CSC-Agent')
{
$type = $newchatList->STORE_NAME;
} else if ($adfs_login->attributes['Group'][0] == 'APP_IM_APP_Boots_ROI_Prescription_'.$this->project_phase.'_Admins') {
$type = $newchatList->STORE_NAME;
}
$data[] = $type;
$data[] = '<div class="progress" style="height:50px">
<div class="progress-bar" style="width:100%;height:50px">Unacknowledged</div>
</div>';
$row[] = $data;
$i++;
}
//echo $this->input->post($this->security->get_csrf_token_name());
if (isset($row) && count($row) > 0) {
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->adfs->count_all('prescriptions a', $where, $join_array),
"recordsFiltered" => $this->adfs->count_filtered('prescriptions a', $column_order,
$column_search, $order, $where, $join_array, $select),
"data" => $row,
$this->security->get_csrf_token_name() => $this->security->get_csrf_hash()
);
} else {
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => 0,
"recordsFiltered" => 0,
"data" => '',
$this->security->get_csrf_token_name() => $this->security->get_csrf_hash(),
);
}
//output to json format
echo json_encode($output);
}catch( Exception $e ) {
log_message( 'error', $e->getMessage( ) . ' in ' . $e->getFile() . ':' . $e->getLine() ); // on error
}
}
Моя Модель Код:
private function _get_datatables_query($table,$column_order='',$column_search='',$order1='',$field_arry='',$select='')
{
if(!empty($select)) {
$this->db->select($select);
}
$this->db->from($table);
if (!empty($field_arry) && count($field_arry) > 0 && $table != '') {
foreach ($field_arry as $key => $value) {
$this->db->where($key, $value);
}
}
$i = 0;
// print_r($column_search);
$names='';
// echo $_POST['search']['value'];
if(!empty($_POST['search']['value'])){
$names = explode(' ',$_POST['search']['value']);
}
// print_r($names);
foreach ($column_search as $key => $item) // loop column
{
if($_POST['search']['value']) // if datatable send POST for search
{
if($i===0) // first loop
{
$this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can
combine with other WHERE with AND.
if(count($names) > 1) {
$this->db->like("CONCAT(CONCAT('%',$item),'%')", $names[$key]);
}else{
$this->db->or_like("CONCAT(CONCAT('%',$item),'%')",
$_POST['search']['value']);
}
}
else
{
if(count($names) > 1) {
$this->db->or_like("CONCAT(CONCAT('%',$item),'%')", $names[$key]);
}else{
$this->db->or_like("CONCAT(CONCAT('%',$item),'%')",
$_POST['search']['value']);
}
}
if(count($column_search) - 1 == $i) //last loop
$this->db->group_end(); //close bracket
}
$i++;
}
if(isset($_POST['order'])) // here order processing
{
$this->db->order_by($column_order[$_POST['order']['0']['column']],
$_POST['order']['0']['dir']);
}
else if(isset($order1))
{
$order = $order1;
$this->db->order_by(key($order), $order[key($order)]);
}
}
function get_datatables($table,$column_order='',$column_search='',$order1='',$field_array='',$join_array='',$select='')
{
$this->_get_datatables_query($table,$column_order,$column_search,$order1,$field_array,$select);
// $this->output->enable_profiler(TRUE);
if($_POST['length'] != -1)
$this->db->limit($_POST['length'], $_POST['start']);
if (!empty($join_array) && count($join_array) > 0 && $table != '') {
foreach ($join_array as $key => $value) {
$this->db->join($key, $value);
}
}
$query = $this->db->get();
return $query->result();
}