0
我不知道PHP編碼,我有一個使用PHP作爲後端服務與JSON的Android應用程序。它會取出最後添加的最新圖像,但是對於類別圖像,它會隨機顯示。我正在嘗試對類別圖像進行排序,並將其作爲最後添加的圖像必須先排序,如果您是專家,請幫助我解決難題。ORDER BY LATEST
我對獲取的圖像API是像下面
<?php
include ("includes/connection.php");
include ("purchase.php");
if (purchase_status() == false) {
echo "<p>Sorry, we are unable to verify your purchase.</p>";
exit ;
} else {
if (isset($_GET['cat_id'])) {
$cat_id = $_GET['cat_id'];
$cat_img_res = mysql_query('SELECT * FROM tbl_category WHERE cid=\'' . $cat_id . '\'');
$cat_img_row = mysql_fetch_assoc($cat_img_res);
$cid = $cat_img_row['cid'];
$cat_nm = $cat_img_row['category_name'];
$files = array();
$dir = opendir(dirname(realpath(__FILE__)) . '/categories/' . $cat_img_row['category_name'] . '/');
while ($file = readdir($dir)) {
if ($file == '.' || $file == '..') {
continue;
}
$allimages[] = $file;
}
$total_arr = array_merge((array)$cat_nm, (array)$allimages);
sort($total_arr);
foreach ($total_arr as $key => $file) {
if ($key != count($total_arr) - 1) {
$array['HDwallpaper'][] = array('images' => $file, 'cat_name' => $cat_nm, 'cid' => $cid);
}
}
echo stripslashes(json_encode($array));
} else if (isset($_GET['latest'])) {
$limit = $_GET['latest'];
$query = "SELECT tbl_gallery.image,tbl_category.category_name FROM tbl_gallery
LEFT JOIN tbl_category ON tbl_gallery.cat_id= tbl_category.cid
ORDER BY tbl_gallery.id DESC LIMIT $limit";
$resouter = mysql_query($query);
$set = array();
$total_records = mysql_numrows($resouter);
if ($total_records >= 1) {
while ($link = mysql_fetch_array($resouter, MYSQL_ASSOC)) {
$set['HDwallpaper'][] = $link;
}
}
echo $val = str_replace('\\/', '/', json_encode($set));
} else {
$query = "SELECT cid,category_name,category_image FROM tbl_category";
$resouter = mysql_query($query);
$set = array();
$total_records = mysql_numrows($resouter);
if ($total_records >= 1) {
while ($link = mysql_fetch_array($resouter, MYSQL_ASSOC)) {
$set['HDwallpaper'][] = $link;
}
}
echo $val = str_replace('\\/', '/', json_encode($set));
}
}
?>
[scandir()按日期排序修改](http:// stac koverflow.com/questions/11923235/scandir-to-sort-by-date-modified) – Tristan