3

I have a function which is returning the list of city names associated to city_id ordered by city name.

I want to show them in a select box. In Firefox (23.0.1) it is working fine even with order. But in case of IE (10) and chrome (29.0.1547.66 m) the order is not correct. I am using PHP and zend framework. My code:

$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) {                                
            }
        });

The returned value is like the following:

{

    "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"
    }

}

Please help me, how to resolve this issue?

Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
Mausumi
  • 431
  • 2
  • 6
  • 20

2 Answers2

2

can you update your code to this

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>";
}

Seems like chrome is auto sorting the json object with the index.

REF:
Chrome and IE sorts JSON Object automatically, how to disable this?
https://code.google.com/p/chromium/issues/detail?id=37404

Community
  • 1
  • 1
Nandakumar V
  • 4,317
  • 4
  • 27
  • 47
  • Thanks a lot its perfect. Only I need to change from $resultArrary[$key]['id'] = $city->city_id; $resultArrary[$key]['city'] = $city->city; to $resultArrary[$key]['id'] = $city['city_id']; $resultArrary[$key]['city'] = $city['city']; – Mausumi Sep 09 '13 at 08:56
0

You are missing ASC or DESC at line 12

->order("city ASC");
Moeed Farooqui
  • 3,604
  • 1
  • 18
  • 23