2017-05-16 70 views
0

我是在表後從here的例子,我知道了所有正在運行的,除了一件事我不能想出的DataTable表順序的表格中,使用「numeroregisto」列的值,當這些值爲「XXX/YY」時 - 我需要它,以便首先對斜槓右側的數字進行排序,然後對其進行排序剩下。 右邊的數字總是兩位數字,左邊的數字可以從一位到五位數字不等。有兩個規則

在另一個OCASION,採用了不同的方法,我被告知這樣做的:

ORDER BY cast(substring_index(numeroregisto, '/', - 1) AS signed INTEGER), cast(substring_index(numeroregisto, '/', 1) AS signed INTEGER) ASC 

但我似乎無法弄清楚如何使這種方法的工作:

var $table_registos = 'registos'; 
    var $column_order_registos = array('numeroregisto','dataregisto','amostra','especies.especie','requisitante',null); //set column field database for datatable orderable 
    var $column_search_registos = array('registos.numeroregisto','registos.dataregisto','registos.amostra','especies.especie','requisitante'); //set column field database for datatable searchable just firstname , lastname , address are searchable 
    var $order_registos = array('numeroregisto' => 'ASC'); // default order 


private function _get_datatables_query_registos() 
    { 
     $this->db->select('registos.*, origemmaterial.id AS omid, origemmaterial.origem, meioconservacao.id AS mcid, meioconservacao.meio, especies.id AS esid, especies.especie'); 
     $this->db->from('registos, origemmaterial, meioconservacao, especies'); 
     $this->db->where('registos.origemid=origemmaterial.id AND registos.meioid=meioconservacao.id AND registos.especieid=especies.id'); 


     $i_registos = 0; 

     foreach ($this->column_search_registos as $item) // loop column 
     { 
      if($_POST['search']['value']) // if datatable send POST for search 
      { 

       if($i_registos===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_registos) - 1 == $i_registos) //last loop 
        $this->db->group_end(); //close bracket 
      } 
      $i_registos++; 
     } 

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

任何幫助將是一個優先事項,非常感謝提前!

+0

目前的代碼有什麼問題?你能否將代碼縮小到你認爲問題所在的部分? – Shadow

+0

我已經按照你的建議編輯了這篇文章,並認爲它現在應該很清楚。 謝謝! – Baya

回答

0

問題已解決,全由我自己決定! 只是希望我能想出一個更優雅的解決方案:

private function _get_datatables_query_registos() 
    { 
     $this->db->select('registos.*, origemmaterial.id AS omid, origemmaterial.origem, meioconservacao.id AS mcid, meioconservacao.meio, especies.id AS esid, especies.especie'); 
     $this->db->from('registos, origemmaterial, meioconservacao, especies'); 
     $this->db->where('registos.origemid=origemmaterial.id AND registos.meioid=meioconservacao.id AND registos.especieid=especies.id'); 


     $i_registos = 0; 

     foreach ($this->column_search_registos as $item) // loop column 
     { 
      if($_POST['search']['value']) // if datatable send POST for search 
      { 

       if($i_registos===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_registos) - 1 == $i_registos) //last loop 
        $this->db->group_end(); //close bracket 
      } 
      $i_registos++; 
     } 

     if(isset($_POST['order'])) // here order processing 
     { 
      if($_POST['order']['0']['column']=='0' && $_POST['order']['0']['dir']=='asc'){ 

      $this->db->order_by('substring(numeroregisto,-2,2) ASC'); 


     } 
     elseif($_POST['order']['0']['column']=='0' && $_POST['order']['0']['dir']=='desc'){ 

      $this->db->order_by('substring(numeroregisto,-2,2) DESC'); 


     } 
      else{ 
      $this->db->order_by($this->column_order_registos[$_POST['order']['0']['column']], $_POST['order']['0']['dir']); 
      } 
     } 
     else if(isset($this->order_registos)) 
     { 
      $order_registos = $this->order_registos; 
      $this->db->order_by('substring(numeroregisto,-2,2) ASC'); 
     } 
    }