【问题标题】:ORDER BY LATEST最新订购
【发布时间】:2016-06-13 01:53:34
【问题描述】:

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

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

【问题讨论】:

标签: php json


【解决方案1】:

只需在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");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-21
    • 2011-07-18
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多