codeigniter 在控制器中对提取的数据进行排序

kokeuurv  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(139)

我有一个困难的时间排序我的数据。因为我提取我的数据使用2列。有没有一种方法,我可以排序它在控制器, AJAX ,模型?任何建议将不胜感激。

正如我们在这里所看到的,我正在获取测试1的所有数据。但是,当我获取它时,它不是按升序排列的。

我的数据库:

检视:

<div id="testdisplay"></div>

** AJAX **

<script>

testfetching();
function testfetching(){

var eventID = $('#eventID').val();

  $.ajax({
        url:"<?=site_url('testing/fetching')?>",
        method:"POST",
        data:{eventID:eventID},
        dataType:"json",
        success:function(data){
                
        $('#testdisplay').html(data);
    
            
        
        },
    })

}
</script>

控制器:

// is there a way where i can sort my data here in controller?
    // I have tried sorting it using sort($data); but it is not working. 

         function fetching(){
        $entry= $this->matchings->entryfetch();
        
        
        $data = '';
        $data='

    <div class="card table-hover table-responsive" style="display: flex;">
    <table class="mt-3 ml-2" style="">
    <thead>
    <tr>
    <th style="float: left;">NO.</th>
    <th style="float: left;">ENTRY NAME</th>


    </tr></thead>';
        
        
        foreach($entry as $entry){
            
            $match= $this->matchings->matchfetch($entry->handlerID);
            
            $data.='<thead>
            <tr>
            <th style="float: left; page-break-after: always;"><hr style="">'.$entry->handlerID.'&nbsp;'.$entry->entryName.'</th><th>&nbsp;</th>  ';
            
            
            
            foreach($match as $match){
                

                
                if($match->handlerIDM === $entry->handlerID){
                    $name=$match->handlertestW;
                    $count=$match->cockNoM;
                }else{
                    
                    $name=$match->handlerM;
                    $count=$match->cockNoW;
                }
                
                
    
                
                
                if($match->handlerM === $entry->entryName){
                    $data.='<tbody>
<tr><td style="float: right; margin-right: 5px;">'.$count.'</td>
    

';
                    
                }else{
                    
                    $data.='<tbody><tr> <td style="float: right; margin-right: 5px;">'.$count.'</td>
                        
                       

';
                    
                }
                
                
                
                
                $data.='<td></td></tr></tbody>';
                //
            }
            
            
        }
        
        $data .='</table></div>';

        echo json_encode($data);
 
     
    }

型号:

function entryfetch(){
        $eventID = $this->input->post('eventID');
        
        $this->db->where('eventID', $eventID);
        $this->db->group_by('handlerID');
        $this->db->order_by('handlerID');
        $query = $this->db->get('entry_test');
        
        return $query->result();
        
        
    }
    
    function matchfetch($entryid){
        
        $eventID = $this->input->post('eventID');
        
        $this->db->where('eventID', $eventID);
        $this->db->where('handlerIDM', $entryid);

      
      
        
        $this->db->or_where('handlerIDW', $entryid);
        $this->db->where('eventID', $eventID);
      
       
    
        $query = $this->db->get('matching');
        
        return $query->result();
        
        
    }
uqjltbpv

uqjltbpv1#

如果要对同一个表的两列中的数据进行排序,则需要使用UNION。
我将只使用你在帖子顶部提供的列和你查询的列,希望这能帮助你开始。
产品型号:

function matchfetch($entryid){
    $eventID = $this->input->post('eventID');

    return $this->db->query(
        'SELECT handlerM AS handler, cockNoM AS cockNo
        FROM matching
        WHERE eventID = ? AND handlerIDM = ?

        UNION

        SELECT handlertestW AS handler, cockNoW AS cockNo
        FROM matching
        WHERE eventID = ? AND handlerIDW = ?

        ORDER BY cockNo', [
        $eventID, $entryID, $eventID, $entryID
    ])->result();
}

每个SELECT查询都会创建一个表(一个用于handlerM+cockNoM,另一个用于handlertestW+cockNoW),UNION会将它们堆叠起来:

然后可以使用ORDER BY对结果表进行排序。
在结果集中,handlerM/handlertestW被重命名为handlercockNoM/cockNoW被重命名为cockNo,因此您还需要更新控制器:
控制器:

foreach($match as $match){
    $name=$match->handler;
    $count=$match->cockNo;

    $data.='<tbody><tr><td style="float: right; margin-right: 5px;">'.$count.'</td>';
    $data.='<td></td></tr></tbody>';
}

如果结果集中需要额外的列,请将它们添加到两个SELECT查询中。

ztmd8pv5

ztmd8pv52#

我在您的查询中没有看到任何订单类型,即ASC、DESC

$this->db->order_by('handlerID', 'ASC');

相关问题