在codeigniter中对特定字段名应用distinct()

时间:2014-03-19 08:09:22

标签: php json codeigniter distinct

我想将distinct()应用于表中的一些选定字段名。该表包含字段app_no,app_name,loan_code,其中categoryloan_code正在重复的类别。 我需要获取所有app_no,但结果只有categoryloan_code,它们将作为json数组发送到我的视图页面。 我的观点页面,

<style>
#search_field{
font-family:Geneva, Arial, Helvetica, sans-serif;
float:right;
padding-top:2%;
margin-right:1%;
}
</style>
<script type="text/javascript">
$(document).ready(function () {
var theme = getDemoTheme()
$(".filter-input").jqxInput({ placeHolder:"Select Criteria",  width:200, height:25, theme:theme });
$("#criteria").change(function() {
    $("#filterInput").val('');
    var value = $("#criteria option:selected").text();      
    var info =  $("#criteria option:selected").val();       
    var url = "<?php echo site_url('admin_sona/get_application_info'); ?>";
    var source =
                {
                datatype: "json",
                datafields: [   { name: info }  ],
                url: url
                };
    var dataAdapter = new $.jqx.dataAdapter(source);
    if(info!='NULL'){
        $("#filterInput").jqxInput({ source:dataAdapter, placeHolder:"Enter "+value, displayMember:info, valueMember:info, width:200, height:25, theme:theme });
    }
    else{
        $("#filterInput").jqxInput({ source:dataAdapter, placeHolder:"Select Criteria",  width:200, height:25, theme:theme });
    }
});
});
</script>
<div id="search_field"> Filter search : 
<select id="criteria" class="select">
    <option value="NULL" selected="selected" >- -Select- -</option>
    <option value="app_no" >Application No</option>
    <option value="app_name">Application Name</option>
    <option value="category">Category</option>
    <option value="loan_code">Loan Code</option>
</select>
<input id="filterInput" class="filter-input"/>  
</div>

我的控制器到了这里,

function get_application_info() //call from 'general/form_trackable_list'
{
    echo(json_encode($this->lams_admin_model->get_application_info())); 
}

我的模特在这里,

function get_application_info()
{
$off = $this->session->userdata['ksdc_logged_in']['office_code'];
$this->db->distinct('app.category, loan_code'); 
$this->db->select('app.app_no, app.category, loan_code, app.app_name');
//  $this->db->group_by('app.category');
$this->db->from('tbl_application app');
if($off!='HOF'){
    $this->db->where('office_code', $off); 
}
$this->db->join('tbl_loantype', 'tbl_loantype.loan_id = app.loan_id');
$query = $this->db->get();
//      get data and store in a json array
if($query->result()):
    foreach($query->result_array() as $row):    
        $info[] = array(    
                        'app_no' => $row['app_no'],
                        'app_name' => $row['app_name'],
                        'category' => $row['category'],
                        'loan_code' => $row['loan_code'],
                        );
    endforeach;
endif;
return $info;        
}   

有人请帮助我..Thankyou

1 个答案:

答案 0 :(得分:0)

您只需将group_by添加到您的子句中,以使其与值不同。这是一个SQL问题,不是别的。您需要按app_no分组,还需要分类和loan_code。检查下面的代码。如果不起作用,请注释掉SQL的回显并发布它,以检查正在执行的SQL CI

function get_application_info()
{
    $off = $this->session->userdata['ksdc_logged_in']['office_code'];

    // Delete THIS:
    // $this->db->distinct('app.category, loan_code'); 

    $this->db->select('app.app_no, app.category, loan_code, app.app_name');
    $this->db->from('tbl_application app');
    if($off!='HOF'){
        $this->db->where('office_code', $off); 
    }
    $this->db->join('tbl_loantype', 'tbl_loantype.loan_id = app.loan_id');

    // Comment out group_by, but add the other field:
    $this->db->group_by('app_no, app.category, loan_code');

    $query = $this->db->get();

    // If not working, comment this out to output the SQL:
    // echo $this->db->last_query();

    ...

}