我很難對資料進行排序。因為我使用 2 列來獲取我的資料。有沒有辦法可以在控制器、ajax、模型中對其進行排序?任何建議將不勝感激。

正如我們在這里看到的,我正在獲取測驗 1 的所有資料。但是當我獲取它時,它不是按升序排列的。
我的資料庫:

看法:
<div id="testdisplay"></div>
阿賈克斯
<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 style="display: flex;">
<table 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.' '.$entry->entryName.'</th><th> </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();
}
uj5u.com熱心網友回復:
如果您嘗試對同一表的兩列中的資料進行排序,則需要使用 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重命名為handler和cockNoM/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 查詢中。
uj5u.com熱心網友回復:
我在您的查詢中沒有看到任何訂單型別。即ASC、DESC
$this->db->order_by('handlerID', 'ASC');
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/453534.html
