title: Msql-Codeigniter :How to Fetch the data based on Current location/city followed by the other cities from same country and so on
body: <p>i have search Box where user will enter the Job/skills ,Locations or company name so , my o/p should be from the current city first ,followed by other cities from same country and so on .
  here is my query in model
   </p>
  
  <pre><code> public function searchIn($keyWord,$curAccId,$offset = "0") {
          try{
              $ipaddress=$this-&gt;input-&gt;ip_address();
                   $details = json_decode(file_get_contents("http://ipinfo.io/{$ipaddress}"));
   $city=$details-&gt;city;
  $countrynamefromip=$details-&gt;country;
  if($countrynamefromip=="IN"){
  $countrynamefromip="INDIA"  ;
  }
  else{
  $countrynamefromip="United States"  ;
  }
              $str_array = array();
              $str_array2 = array();
              foreach($keyWord as $k=&gt;$key)
              {
                  $str_array[] =  'jp.post_title like "%'.$key .'%" and (l.cityname="%'.$city.'%" or l.countryname="%'.$countrynamefromip.'%")';
                  $str_array2[] =  'acnt.accountname like "%'.$key .'%" or (l.cityname="%'.$city.'%" or l.countryname="%'.$countrynamefromip.'%")';
                  $str_array3[] =  'jp.extra_skill like "%'.$key .'%"and (l.cityname="%'.$city.'%" or l.countryname="%'.$countrynamefromip.'%")';
                  //$str_array4[] =  'l.cityname like "%'.$key .'%"and (l.cityname="'.$city.'" and l.countryname="'.$countrynamefromip.'")';
              }
              $str = array_merge($str_array, $str_array2, $str_array3);
              $str = join(' or ', $str );
              //print $str; exit;
              $this-&gt;db-&gt;select('jp.post_id, jp.post_title, jp.post_description, jp.ctc_from, jp.ctc_to, jp.extra_skill,
                  jp.experience_from, jp.experience_to, jp.createddate, jp.createdtime, jp.currency, jp.rate, skills.skillname,
                  l.latitude, l.longitude, l.locationname,l.cityname,l.countryname, acnt.accountname, acnt.accountid as accid, con.first_name, con.last_name')
                  -&gt;from('accounts as acnt')
                  -&gt;limit(10,$offset)
                  //-&gt;join('accounts as acnt', 'acnt.accountid = jp.accountid and acnt.accountid != '.$curAccId.'', 'left')
                  -&gt;join('job_posts as jp', 'acnt.accountid = jp.accountid and acnt.accountid != '.$curAccId.'', 'left')
                  -&gt;join('job_post_locations as jpl', 'jpl.jobpost_id = jp.post_id and jpl.status=1', 'left')
                  -&gt;join('locations as l', 'jpl.location_id = l.locationid and l.status = 1', 'left')
                  -&gt;join('job_post_skills', 'job_post_skills.post_id = jp.post_id', 'left')
                  -&gt;join('contacts AS con','con.contactid = jp.rec_id','left')
                  -&gt;join('skills', 'skills.skillid = job_post_skills.skill_id', 'left')
                  -&gt;join('relation_requests', 'relation_requests.requestfrom = job_post_skills.skill_id', 'left')
                  -&gt;where('jp.accountid !=',$curAccId)
                  -&gt;where($str)
                  -&gt;or_where_in("l.cityname",$keyWord)
                  -&gt;or_where_in("skills.skillname",$keyWord)
                  -&gt;group_by('jp.post_id')
                  -&gt;order_by('jp.createddate', 'desc');
              $query = $this-&gt;db-&gt;get();
              $list = $query-&gt;result();
             // print'&lt;pre&gt;';print_r($list); exit;
             //cho $this-&gt;db-&gt;last_query(); exit;
              return $list;
          } catch (Exception $ex){
              echo $ex-&gt;getMessage();
              exit;
          }
      }?&gt;
  </code></pre>