コード例 #1
0
ファイル: AjaxController.php プロジェクト: hoaitn/base-zend
 public function searchjsonAction()
 {
     $keyword = strtolower($this->getRequest()->getParam('term'));
     $numOfItem = (int) Zend_Registry::get('Setting')->autocomplete_limit;
     if ($numOfItem > 0) {
         list($Pager, $aRespon['cities']) = City::getAll(array('LOWER(city_id) || LOWER(tags) LIKE ?' => "%{$keyword}%"), 1, $numOfItem);
         $numOfItem = $numOfItem - $Pager->getNumResults();
     }
     if ($numOfItem > 0) {
         //Search countries three
         $aRespon['countries'] = Countries::getAll(array('LOWER(countries_id) LIKE ? OR  LOWER(tags) LIKE ?' => array("%{$keyword}%", "%{$keyword}%")), $numOfItem);
         $numOfItem = $numOfItem - count($aRespon['countries']);
     }
     header('Content-Type: text/html; charset=iso-8859-1');
     $search = ucwords($keyword);
     $keywords = array($keyword, $search, ucfirst(str_replace(' ', '', $keyword)), ucfirst(str_replace(' ', '', My_Plugin_Libs::unUnicode($keyword))));
     $response = array();
     $address = '';
     //rebuil to autocomplete struc
     foreach ($aRespon as $location => $data) {
         foreach ($data as $Obj) {
             $link = '';
             if ($location == 'cities') {
                 $label = $Obj->name . ', ' . $Obj->Countries->name;
                 $link = $Obj->getLink();
                 $country = $Obj->Countries->name;
             } elseif ($location == 'countries') {
                 $label = $Obj->name;
                 $link = $Obj->getLink();
                 $country = $Obj->name;
             } else {
                 $label = $Obj->name;
                 $link = $Obj->getLink();
                 $country = $Obj->Countries->name;
                 $address = $Obj->address;
             }
             $response[] = array('location' => $location, 'link' => $link, 'id' => $Obj->id, 'label' => str_replace($keywords, '<b>' . $search . '</b>', $label), 'value' => $label, 'country' => $country, 'address' => str_replace($keywords, '<b>' . $search . '</b>', $address));
         }
     }
     echo json_encode($response);
 }