I want to show timestamp descending order, but my query is not working, here is my code:
$this->db->order_by("DATE(field_name)", "DESC");
I am getting this, but when I display in jquery datatable, it became ascending order, how to solve this?
In jQuery Datatable, you have an config option to sort the table, by default, the first one. Be sure it is well positionned if you want to keep a specific data order.
$('#myDataTable').DataTable({
order: [[0, 'desc']],
});
The first argument 0
is index of the column to sort by (here, the first column, 1
for the second one, etc).
Or, you can disable Datatable sorting, passing an empty array to the order
option :
$('#myDataTable').DataTable({
order: [],
});
Try this code :
$this->db->order_by("field_name", "DESC");
You need to remove DATE in your query.
Please try below
$this->db->order_by("field_name", "DESC");
Here filed_name = Your table field value (timestamp in this case.)
Also, if it does not work please provide your error.
The problem here is Jquery Datatable, which by default order by the first column of the table.
If ordering is enabled (ordering), then DataTables will perform a first pass order during initialisation. Using this parameter you can define which column(s) the order is performed upon, and the ordering direction. The order must be an array of arrays, each inner array comprised of two elements:
see more on Datatables Order
you have two options here, all of them in the JS code,dont worry about the php code.
like this:
$("#table").dataTable({
"order":[[positionofcolumtoorder,'desc']]
});
as this:
$('#table').dataTable( {
"order": []
} );
$this->db->order_by("field_name", "DESC");