android - php mysql sort by date (Recent) -
i have code fetching data in mobile application sql first added showing first, need set showing last added first in android application. have api code below, in latest showing per requirement cid , aid need same latest ( must show recent first). code below thanks
<?php include("includes/connection.php"); error_reporting(0); mysql_query("set names 'utf8'"); if(isset($_get['cid'])) { if(isset($_get['cat_id'])) { $cat_id=$_get['cat_id']; $query="select * tbl_quotes left join tbl_category on tbl_quotes.cat_id= tbl_category.cid tbl_quotes.cat_id='".$cat_id."'"; } else if(isset($_get['latest'])) { $limit=$_get['latest']; $query="select * tbl_quotes left join tbl_category on tbl_quotes.cat_id= tbl_category.cid order tbl_quotes.id desc limit $limit"; } else { $query="select cid,category_name,category_image tbl_category"; } $resouter = mysql_query($query); $set = array(); $total_records = mysql_num_rows($resouter); if($total_records >= 1){ while ($link = mysql_fetch_array($resouter, mysql_assoc)){ $set['quotes'][] = $link; } } /* output in format */ //echo $val= str_replace('\\/', '/', json_encode($set)); header( 'content-type: application/json; charset=utf-8' ); echo str_replace('\\/', '/',json_encode($set,json_unescaped_unicode)); } else{ if(isset($_get['aid'])) { $author_id=$_get['aid']; $qry="select * tbl_quotes left join tbl_author on tbl_quotes.aid= tbl_author.author_id tbl_quotes.aid='".$author_id."'"; } else if(isset($_get['latest'])) { $limit=$_get['latest']; $qry="select * tbl_quotes left join tbl_author on tbl_quotes.aid= tbl_author.author_id left join tbl_category on tbl_quotes.cat_id= tbl_category.cid order tbl_quotes.id desc limit $limit"; } else { $qry="select author_id,author_name,author_image tbl_author"; } $resultouter = mysql_query($qry); $author_set = array(); $total_records = mysql_num_rows($resultouter); if($total_records >= 1){ while ($link = mysql_fetch_array($resultouter, mysql_assoc)){ $author_set['quotes'][] = $link; } } // echo $value= str_replace('\\/', '/', json_encode($author_set)); /* output in format */ header( 'content-type: application/json; charset=utf-8' ); echo str_replace('\\/', '/',json_encode($author_set,json_unescaped_unicode)); } ?>
when use select
statement query data table, result set not sorted in orders. sort result set, use order by
like :-
order column1 [asc|desc]
in code :-
$query="select * tbl_quotes left join tbl_category on tbl_quotes.cat_id= tbl_category.cid order tbl_quotes.id desc,date desc limit $limit";
if column data type not date
or datetime
cast()
:-
cast(column_name datetime); cast(column_name date);
Comments
Post a Comment