2016-04-23 65 views
1

過濾器我要創建過濾器創建具有多輸入笨

這裏是我的模式

<?php 
function show($branch,$department,$employee,$status,$from,$to,$category) 
{ 
    $this->db->select('a.*,b.name,c.cityname'); 
    $this->db->from('expreport a'); 
    $this->db->join('expuser b','a.createdby=b.unique_id'); 
    $this->db->join('expcity c','a.cityid=c.citycode'); 

    if($branch != "" || $status !="" || $from !="" || $to !="" || $department !="" || $employee !="") 
    { 
    $this->db->select('d.employeename,e.branchname,f.deptname'); 
    $this->db->from('expemployee d'); 
    $this->db->join('expbranch e','d.branchid=e.branchcode'); 
    $this->db->join('expdepartment f','d.deptid=f.deptcode'); 
    $this->db->like('branchid', $branch); 
    $this->db->or_like('deptid', $department); 
    $this->db->or_like('employeeid', $employee); 
    $this->db->where('status', $status); 
    $this->db->where('fromdate <=',$from); 
    $this->db->where('todate >=',$to); 
    } 

    if($category !="") 
    { 
    $this->db->select('g.category'); 
    $this->db->from('expcategory g'); 
    $this->db->like('catcode', $category); 
    } 

    $this->db->group_by('reportcode'); 
    $result = $this->db->get(); 
    echo $this->db->last_query(); 
    return $result->result(); 
} 
?> 

我有鑑於7倍的投入,我想分開$分公司,$狀態,$等,所以查詢可以從1變量執行,所以我不必輸入所有變量,因爲當我只是填寫1輸入,查詢不執行

All Answers will be appreciated ,謝謝:)

+0

僅使用單變量$數據在函數參數時傳遞給從控制器使用$數據[「分支」]起作用。 –

+0

你能舉一些例子嗎? @AmitChauhan –

+0

所以我應該使模型中的7個功能?一旦函數包含每個輸入的查詢?? –

回答

0

我做了類似的事情。我有一個數據過濾來顯示基於兩個過濾器。我有類別過濾器和位置過濾器。 它在捷克,但單選按鈕的第一行是類別,第二行是位置。

enter image description here

// After user hits "Show events"  
if($submit == "Submit"){ 
    //check if category filter was applied 
    if(isset($category_filter) && $category_filter!='' && $category_filter!='everything'){ 
     //Add category number into variable and then into SQL query 
     $cat_sql_condition = 'AND category='.$category_filter.''; 
    }else{ 
     $cat_sql_condition=''; 
    } 
    //check if location filter was applied 
    if(isset($location_filter) && $location_filter!='' && $location_filter!='everything'){ 
     $loc_sql_condition = 'AND location_id='.$location_filter.''; 
    }else{ 
     $loc_sql_condition=''; 
    } 
    //Here i am creating the final query 
    $query_string = "SELECT * FROM tbl_events WHERE visibility=1 AND start_date> ".$current_unix_timestamp." ".$cat_sql_condition." ".$loc_sql_condition." ORDER BY start_date;"; 
}else{ 
    //Without submit 
    $query_string = "SELECT * FROM tbl_events WHERE visibility=1 AND start_date>'.$current_unix_timestamp.' ORDER BY start_date"; 
}