3

我有一个函数返回与city_id按城市名称排序的城市名称列表。

我想在选择框中显示它们。在 Firefox (23.0.1) 中,即使有订单也能正常工作。但在 IE (10) 和 chrome (29.0.1547.66 m) 的情况下,顺序不正确。我正在使用 PHP 和 zend 框架。我的代码:

$cities = $countryCityModel->getCities($country);
 print json_encode(array('status'=>'Success',
                         'data'=>$cities)
                  );
 public function getCities($countryId){
    if(!$countryId)
    return false;
    $mdb = $this->getOldDbAdapter(); 
    $sql = $mdb->select()
               ->from('cities', array('city_id','city'))
               ->where('country_id = ?', $countryId)
               ->order("city");
    return $mdb->fetchPairs($sql);
 }
$.ajax({
 url : baseUrl + '/lead/index/get-cities',
 dataType : 'json',
 data : {
          country:country_id
        },
        beforeSend : function(){
          $("#holder_popup").show();
        },
        complete: function(){
          $("#holder_popup").hide();   
        },
        success : function(res) {
           var sbuOptions = "<option value=''>--SELECT--</option>"
           if(res.status == 'Success'){
             for(i in res.data){
               sbuOptions += "<option value='"+i+"'>"+res.data[i]+"</option>";
             }
             $("#city").html(sbuOptions);

           }else{
              $("#city").html(sbuOptions);
              alert(res.msg);
           }
        },
        error : function(jqXHR, exception) {                                
            }
        });

返回值如下:

{

    "status":"Success",
    "data":{
        "53029":"DURRES",
        "53331":"ELBASAN",
        "40239":"FIER",
        "16235":"KAMEZ",
        "42191":"KAVAJE",
        "41375":"KUKES",
        "53581":"PESHKOPI",
        "57686":"SHIJAK",
        "56756":"SHKODER",
         "4496":"TIRANA",
        "41342":"VLORE",
        "19454":"VORE"
    }

}

请帮助我,如何解决这个问题?

4

2 回答 2

2

你能把你的代码更新到这个

public function getCities($countryId)
{
    $resultArrary = array();
    if($countryId)
    {
        $mdb = $this->getOldDbAdapter(); 
        $sql = $mdb->select()
                   ->from('cities', array('city_id','city'))
                   ->where('country_id = ?', $countryId)
                   ->order("city");
        $result = $mdb->fetchAll($sql);

        foreach($result as $key => $city )
        {
            $resultArrary[$key]['id'] =  $city['city_id'];
            $resultArrary[$key]['city'] =  $city['city'];
        }
    }

    return $resultArrary;
 }

for(i in res.data)
{
    cityData = res.data[i];
    sbuOptions += "<option value='"+cityData.id+"'>"+cityData.city+"</option>";
}

似乎 chrome 正在使用索引自动对 json 对象进行排序。

REF:
Chrome 和 IE 自动对 JSON 对象进行排序,如何禁用它?
https://code.google.com/p/chromium/issues/detail?id=37404

于 2013-09-09T08:00:20.730 回答
0

您在线路上缺少 ASC 或 DESC12

->order("city ASC");
于 2013-09-09T06:08:55.017 回答