按最新订单

时间:2016-06-13 01:53:34

标签: php json

我不知道php编码,我有一个使用php作为json后端服务的android应用程序。它是最后添加的最新图像,但对于类别图像,它随机显示。我正在尝试对类别图像进行排序,因为最后添加的图像必须首先,如果您是专家,请帮助我解决难题。

获取图片的MY 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));
    }
}
?>

1 个答案:

答案 0 :(得分:0)

只需在php中使用mysql查询,如下所示:

mysql_query("SELECT * FROM tbl_category WHERE cid='".$cat_id."' ORDER BY cid DESC");

如果您已经存储了上传数据的时间,那么这是获取最新上传图像的完美方式,如下所示:

mysql_query("SELECT * FROM tbl_category WHERE cid='".$cat_id."' ORDER BY uploaded_time DESC");