在CodeIgniter中按Desc顺序排序数据

时间:2015-12-17 07:43:49

标签: php codeigniter

我是CodeIgniter的新手并负责维护项目。我想根据结束日期按降序对数据进行排序。这是查询:

       $this->db->select('*');    
       $this->db->from('deals');   
       $this->db->where('id IN ('.$new_data.')');
       $this->db->where('status !=', 'expired');
       $this->db->where('status !=', 'draft');
       $this->db->order_by('end_date','DESC');  

此查询未提供我的首选结果。

end_date是field_type“varchar”并以“m / d / y”格式存储

4 个答案:

答案 0 :(得分:0)

$this->db->order_by(DATE_FORMAT(end_date,'%m/%d/%Y'),'DESC');

OR

$this->db->order_by(STR_TO_DATE(end_date,'%m/%d/%Y'),'DESC');

答案 1 :(得分:0)

试试这个

$q = 
$this->db->select('*')
    ->from('deals')
    ->where("(id IN '{$new_data}' AND status != 'expired' OR status != 'draft')")
    ->order_by(STR_TO_DATE('end_date','%m/%d/%Y'),'DESC')
    ->get();

print_r($q->result_array());

答案 2 :(得分:0)

$this->db->select("*,STR_TO_DATE(end_date,'%d-%m-%Y') AS day",FALSE);
   $this->db->from('deals');   
   $this->db->where('id IN ('.$new_data.')');
   $this->db->where('status !=', 'expired');
   $this->db->where('status !=', 'draft');
   $this->db->order_by('day','DESC');

答案 3 :(得分:0)

假设数据包含在$ data [' deal_list']数组中。您可以排序为:

$tempArr = $data['deal_list'];

// Obtain a list of columns
foreach ($tempArr as $key => $row) {
    $dtime = DateTime::createFromFormat("m/d/Y",$row['end_date']);
    $timestamp = $dtime->getTimestamp();
    $volume[$key]  = $timestamp;
}

array_multisort($volume, SORT_ASC, $tempArr);
$data['deal_list'] = $tempArr;