1

I have JSON data returned in my action controller:

$results = $repo->getMatchingCityName($searchTerm);

Response that i'm getting:

[{"CityName":"Montreal"},{"CityName":"New york"}]........

But jquery ui autocomplete doesn't show anything

tried

$this->_helper->json(array_values($results));

and

Zend_Json::encode($results);

But no use. How do I convert into

[{"value":"Montreal","label":"Montreal"},{"value":"New york","label":"New york"}]
Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
user430266
  • 43
  • 1
  • 4

2 Answers2

0

Try this code

$results = $repo->getMatchingCityName($searchTerm); // [{"CityName":"Montreal"},{"CityName":"New york"}]........


$data = Zend_Json::decode($results);

$new = array();

foreach ($data as $row) {
   $temp['value'] = $row['CityName'];
   $temp['label'] = $row['CityName'];
   array_push($new, $temp);
}

$newEncode = Zend_Json::decode($new); //[{"value":"Montreal","label":"Montreal"},{"value":"New york","label":"New york"}]
Dinuka Thilanga
  • 4,220
  • 10
  • 56
  • 93
0

I got it working like this

$temp = array();
foreach($results as $row)
{
    $value = $row["CityName"];
    array_push($temp, array(
        "label" => $value,
        "value" => $value
    ));
}

$data = $this->_helper->json($temp);
$this->_helper->autoComplete($data);

Added the view helper in bootstrap

Zend_Controller_Action_HelperBroker::addHelper(
    new ZendX_JQuery_Controller_Action_Helper_AutoComplete()
);
daker
  • 3,430
  • 3
  • 41
  • 55
user430266
  • 43
  • 1
  • 4