选择按两个字段排序的表值

时间:2016-11-18 04:32:20

标签: php mysql codeigniter datatable

我有一个表名'ap_appointment',它有字段'ap_time'(TIME)和'ap_date'(DATE)。我想以这样的方式对结果进行排序,结果数组必须按“降序”顺序按“ap_date”DATE排序,同时结果数组必须按“升序”顺序按ap_time TIME排序。我正在使用mysql

var $order = array('ap_date' => 'desc','ap_time' => 'asc'); // default order 

public function __construct()
{
    parent::__construct();
    $this->load->database();
}

private function _get_datatables_query()
{

    $CI =& get_instance();
    $userinfo=$CI->session->userdata('userinfo');
    $this->db->from($this->table);
    // Filter the appoinment list for particular Clinic & Receptionist
    $this->db->where('ap_clinic_id',$userinfo['id']);

    // Filter the appoinment list for particular doctor
    if($userinfo['type']=='doctor')
    {
    $this->db->where('ap_doctor',$userinfo['user_id']);
    }

    $i = 0;

    foreach ($this->column_search as $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.
                $this->db->like($item, $_POST['search']['value']);
            }
            else
            {
                $this->db->or_like($item, $_POST['search']['value']);
            }

            if(count($this->column_search) - 1 == $i) //last loop
                $this->db->group_end(); //close bracket
        }
        $i++;
    }

    if(isset($_POST['order'])) // here order processing
    {
        $this->db->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
    } 
    else if(isset($this->order))
    {
        $order = $this->order;
        $this->db->order_by(key($order), $order[key($order)]);
    }
}

function get_datatables()
{
    $this->_get_datatables_query();
    if($_POST['length'] != -1)
    $this->db->limit($_POST['length'], $_POST['start']);
    $query = $this->db->get();
    return $query->result();
}

结果输出表必须是这样的 enter image description here

0 个答案:

没有答案