【问题标题】:Display table records order by time in descending按时间降序显示表记录
【发布时间】:2017-11-16 01:06:01
【问题描述】:

我正在使用以下代码显示表格记录。记录正在显示,但顺序不正确(asc/desc)。

// storing  request (ie, get/post) global array to a variable  
$requestData= $_REQUEST;


$columns = array( 
// datatable column index  => database column name
    0 =>'***', 
    1 => '***',
    2=> '***',
        3=> 'time',
        4=> '***'
);

// getting total number records without any search
$sql = "SELECT * ";
$sql.=" FROM details where account_id=".$_SESSION['admin_id'];

$query=mysqli_query($conn, $sql) or die("server_response.php: get details");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;  // when there is no search parameter then total number rows = total number filtered rows.


$sql = "SELECT * ";
$sql.=" FROM details where account_id=".$_SESSION['admin_id'];
if( !empty($requestData['search']['value']) ) {   // if there is a search parameter, $requestData['search']['value'] contains search parameter
    $sql.=" AND ( *** LIKE '".$requestData['search']['value']."%' ";    
    $sql.=" OR *** LIKE '".$requestData['search']['value']."%' ";
        $sql.=" OR *** LIKE '".$requestData['search']['value']."%' ";
        $sql.=" OR time LIKE '".$requestData['search']['value']."%' ";

    $sql.=" OR *** LIKE '".$requestData['search']['value']."%' )";
}
$query=mysqli_query($conn, $sql) or die("server_response.php: get details");
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result. 
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
/* $requestData['order'][0]['column'] contains colmun index, $requestData['dir'][0]['order'] contains order such as asc/desc  */    
$query=mysqli_query($conn, $sql) or die("server_response.php: get details");

$data = array();
while( $row=mysqli_fetch_array($query) ) {  // preparing an array
    $nestedData=array(); 

    $nestedData[] = $row[""];
    $nestedData[] = $row[""];
    $nestedData[] = $row[""];
        $nestedData[] = $row["time"];
        $nestedData[] = $row[""];

    $data[] = $nestedData;
}



$json_data = array(
            "draw"            => intval( $requestData['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw. 
            "recordsTotal"    => intval( $totalData ),  // total number of records
            "recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
            "data"            => $data   // total data array
            );

echo json_encode($json_data);  // send data as json format

?>

我需要按时间降序显示记录。如何按降序对记录进行排序?

【问题讨论】:

标签: javascript php mysql


【解决方案1】:

尝试编写mysql查询按时间降序

SELECT something , something FROM mytable ORDER BY time DESC;

【讨论】:

  • 在这种情况下不起作用。此处需要更改。
  • 您的查询是否正常工作并且您有输出..?你想按时间降序输出吗?
  • 是的。我要出去了。只有记录不是按降序排列的。想要按时间顺序显示记录。
【解决方案2】:

在你的 PHP 中试试这个对你有帮助

$json = '[{"date": "2011-08-07", "name": "bob"},{"date": "2011-07-07", "name": "jones"},{"date": "2011-09-03", "name": "blair"},{"date": "2009-01-01", "name": "test"}]';
$arr = json_decode($json, true);
echo 'Before Sorting <br>';
print_r($arr);
function cmp($a, $b)
{
    return strcmp($a['date'], $b['date']);
}
usort($arr, "cmp");
echo "<br><br> After Sorting <br>";
print_r($arr);

【讨论】:

  • OP 正在尝试在 SQL 查询中进行排序。
猜你喜欢
  • 1970-01-01
  • 2018-06-21
  • 2022-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-09
相关资源
最近更新 更多