php服务按降序排序

时间:2014-12-10 12:45:59

标签: php mysql json web-services

我在php上写了这些服务,这里我在最近30天的当前日期的特定位置写了30天的列表。升序工作正常,但我需要降序到基于日期明智。

    <?php
include("db.php");
$day=date("d");
$month=date("m");
$year=date("Y");
$day1=$day-30;


if($day1<0){
$month1=$month-1;
if($month1==0){
$month1=12;
$year1=$year-1;
}
else{
$year1=$year;
}
}

for ($x = 0; $x<=$day; $x++) {
    $daynmonth[]= "(day=$x and month=$month and year=$year)";
}

$day1 = str_replace("-","","$day1");

for ($y = 0; $y<=$day1; $y++) {
    $daynmonth1[]= "(day=".$y." and month=$month1 and year=$year1)";
} 
//print_r($daynmonth1);
$daynmonth2=implode( " or ", $daynmonth);
$daynmonth3=implode( " or ", $daynmonth1);
//echo $daynmonth3;
if($_GET['city']){
$loc=$_GET['city'];

$sq=mysql_query("select * from city where city='$loc' ORDER BY city ASC");
while($re1=mysql_fetch_array($sq)){
    $id = $re1['id'];
    $city = $re1['city'];
$sql="select * from price where city=".$id." and ".$daynmonth3." and ".$daynmonth2." GROUP BY month,day ORDER BY month desc";
$sql2=mysql_query($sql);
    while($re2=mysql_fetch_array($sql2)){
        // create each line in the city

        $day112=$re2["day"];
        if(strlen($day112)==1){
        $day113="0".$re2["day"];
        }
        else{
        $day113=$re2["day"];
        }
        $line["date"] = $day113.'-'.$re2["month"].'-'.$re2["year"];

        if($re2['price']=='')
        { 
            $line["Price"] = "--";
        }
        else
        {
            $line["Price"] = $re2["price"];
        }

        // add the line to the city
        $cities[$city][] = $line;
    }
}
// encode to json
echo json_encode($cities);
}
?>

你可以建议我在哪里修改以获得降序。在这里,我得到升序的结果

    {
  "India": [
    {
      "date": "01-12-2014",
      "Price": "409"
    },
    {
      "date": "02-12-2014",
      "Price": "409"
    },
    {
      "date": "03-12-2014",
      "Price": "409\t"
    },
    {
      "date": "04-12-2014",
      "Price": "409"
    },
    {
      "date": "05-12-2014",
      "Price": "409"
    },
    {
      "date": "06-12-2014",
      "Price": "397"
    },
    {
      "date": "07-12-2014",
      "Price": "397"
    },
    {
      "date": "08-12-2014",
      "Price": "397"
    },
    {
      "date": "09-12-2014",
      "Price": "397"
    },
    {
      "date": "01-11-2014",
      "Price": "352"
    },
    {
      "date": "02-11-2014",
      "Price": "354"
    },
    {
      "date": "03-11-2014",
      "Price": "354"
    },
    {
      "date": "04-11-2014",
      "Price": "354"
    },
    {
      "date": "05-11-2014",
      "Price": "360"
    },
    {
      "date": "06-11-2014",
      "Price": "377"
    },
    {
      "date": "07-11-2014",
      "Price": "379"
    },
    {
      "date": "08-11-2014",
      "Price": "379"
    },
    {
      "date": "09-11-2014",
      "Price": "379"
    },
    {
      "date": "10-11-2014",
      "Price": "379"
    },
    {
      "date": "11-11-2014",
      "Price": "384"
    },
    {
      "date": "12-11-2014",
      "Price": "389"
    },
    {
      "date": "13-11-2014",
      "Price": "390"
    },
    {
      "date": "14-11-2014",
      "Price": "390"
    },
    {
      "date": "15-11-2014",
      "Price": "396"
    },
    {
      "date": "16-11-2014",
      "Price": "405"
    },
    {
      "date": "17-11-2014",
      "Price": "405"
    },
    {
      "date": "18-11-2014",
      "Price": "409"
    },
    {
      "date": "19-11-2014",
      "Price": "414"
    }
  ]
}

1 个答案:

答案 0 :(得分:2)

试一试:

$sql="select * from price where city=".$id." and ".$daynmonth3." and ".$daynmonth2." GROUP BY month,day ORDER BY year, month, day desc";

而不是:

$sql="select * from price where city=".$id." and ".$daynmonth3." and ".$daynmonth2." GROUP BY month,day ORDER BY month desc";