dshm8998473 2016-06-13 01:53
浏览 17

按最新订单

I do not know php coding, I have one android application which using php as backend service with json. It is fetching latest images as last added first, but for category images, its showing randomly. I am trying to sort category image also as last added image must first, If you are expert, Please help me for solve puzzle.

MY API for fetch images is like below

<?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条回答 默认 最新

  • dsx5201 2016-06-13 05:22
    关注

    just user the mysql query in php as follows:

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

    if you have stored the time of upload the data then this is perfect way to get the latest uploaded images as follows:

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

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100