Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nadeem-khan/c8c8256567e667c9430c to your computer and use it in GitHub Desktop.
Save nadeem-khan/c8c8256567e667c9430c to your computer and use it in GitHub Desktop.
Select similar (autofill) city names from maxmind cities list as compared to the input string in CodeIgniter

Select similar (autofill) city names from maxmind cities list as compared to the input string in CodeIgniter

Model

     class Maxmind_model extends CI_Model {

     private $table = "maxmind";

     public function __construct() {
         parent::__construct();
     }

    public function getCities($string) {
    $string = str_replace('%20', ' ', $string);
    //echo "<br>" . "string is " . $string . "<br>";
    //where $string is the input string
    //and "maxmind" is the table with all the city names in a column named 'city' 
    //and country codes are present in another column of the same table with the name of 'country' 
    $query = "SELECT DISTINCT 
              city,
              country,
              (city REGEXP " . "'" . '^' . $string . '$' . "'" . ") AS whole_string,
              (city REGEXP " . "'" . '^' . $string . "'" . ") AS word_at_start,
              (city REGEXP " . "'" . '/' . $string . '/' . "'" . ") AS word_at_middle,
              (city REGEXP " . "'" . $string . '$' . "'" . ") AS word_at_end 
              FROM
              maxmind
              WHERE (CONCAT(city) LIKE " . "'" . $string . "'" . ") 
              OR (CONCAT(city) LIKE " . "'" . $string . '%' . "'" . ") 
              OR (CONCAT(city) LIKE " . "'" . '%' . $string . '%' . "'" . ") 
              OR (CONCAT(city) LIKE " . "'" . ' %' . $string . '% ' . "'" . ") 
              OR (CONCAT(city) LIKE " . "'" . '%' . $string . "'" . ") 
              GROUP BY city 
              ORDER BY whole_string DESC,
              word_at_start DESC,
              word_at_middle DESC,
              word_at_end DESC 
              LIMIT 15";

   $cities_names = $this->db->query($query);
   if ($cities_names->num_rows() > 0) {
   return $cities_names;}
   }

   } 

Controller

class Search extends CI_Controller {

public function __construct() {
    parent::__construct();
}

//function to get similar city names w.r.t the input string
public function loadSuggestions($string) {
    $string = urldecode($string);
    $this->load->model('Maxmind_model');
    $output = $this->Maxmind_model->getCities($string);
    $data = array();
    $count = 1;
    if (!empty($output)) {
        $max = $output->num_rows();
        $data['result']['version'] = $this->config->item('version');
        $output = $output->result();
        foreach ($output as $row) {
            $data['result']['items'][] = $this->singleItem($row);
            $count++;
        }

        $this->load->view('output/json', $data);
    }
}

    //function to echo json that contains similar city names w.r.t the input string
    private function singleItem($row) {
    $item = array();
    $item['item']['country'] = $row->country;
    $item['item']['city'] = $row->city;
    return $item;
    }

}

View

    <?php
	if(!$profiler) {
		header('Content-type: text/json');
		header('Content-type: application/json');
	}
	$result = json_encode($result);
	$result = str_replace('\/', '/', $result);
	echo $result;
	exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment