hexasoft

Auto Complete for City Search Using PHP and MySQL

Mar 30th, 2018
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.38 KB | None | 0 0
  1. /**************************************************************************************************************/
  2. /* Description: This snippet is to provide you a simple way you can create autocomplete                       */
  3. /*              suggestions for different region, city locations across the country.                          */
  4. /*              There are 2 steps in this snippet. For information, please visit IP2Location tutorial page at:*/
  5. /* https://www.ip2location.com/tutorials/creating-autocomplete-for-city-search-using-php-and-mysql-database   */
  6. /**************************************************************************************************************/
  7. /* You can obtain free IPV6 database at https://www.ip2location.com/free/ipv6                                 */
  8.  
  9. <!DOCTYPE html?>
  10. <html?>
  11. <head?>
  12. <title?>City Search Example Code Auto Complete</title?>
  13. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"?></script?>
  14. <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.2/bootstrap3-typeahead.min.js"?></script?>  
  15. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /?>  
  16. </head?>
  17. <body?>
  18. <br /?><br /?>
  19. <div class="container" style="width:600px;"?>
  20. <h2 align="center"?>City Search Example Code Auto Complete</h2?>
  21. <br /?><br /?>
  22. <form action="" method="post"?>
  23. <label?>Country: </label?>
  24. <?php
  25. //connect to database
  26. $connect = mysqli_connect("localhost", "root", "", "ip2location");
  27.  
  28. //retrieve countryName based on ipaddress
  29.  
  30. //Get the visitor IP address
  31. $ip = $_SERVER['REMOTE_ADDR'];
  32. //In case you are testing locally with 127.0.0.1,
  33. //you can uncomment the below line to assign the IP address
  34. //to 8.8.8.8 (or whatever) for your testing.
  35. //$ip = "8.8.8.8";
  36.  
  37. // Function to convert IP address (xxx.xxx.xxx.xxx) to IP number (0 to 256^4-1)
  38. function Dot2LongIP ($ip) {
  39. if ($ip == ""){
  40. return 0;
  41. }else {
  42. $ips = explode(".", $ip);
  43. return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);
  44. }
  45. }
  46. // Convert IP address to IP number for querying database
  47. $ipno = Dot2LongIP($ip);
  48.  
  49. //start to query from database
  50. $query = 'select DISTINCT country_name,country_code from ip2location_db3 where "'.$ipno.'"<= ip_to AND ip_from<="'.$ipno.'"';
  51. $result = mysqli_query($connect, $query);
  52.  
  53. //check if query is sucesss
  54. if(!empty($result)){
  55. while($row = mysqli_fetch_assoc($result)){
  56. echo '<label id="country_name">' . $row["country_name"] .'</label>';
  57. //store country code in a variable for retrieve the region name and city name
  58. $country_code = $row["country_code"];
  59. }
  60. }
  61. ?>
  62. <br /><br />
  63. <label>Region Name:</label>
  64. <input type="text" name="region" id="region"  class="form-control input-lg" autocomplete="off" placeholder="Type Region Name" />
  65. <br /><br />
  66. <label>City Name:</label>
  67. <input type="text" name="city" id="city" class="form-control input-lg" autocomplete="off" placeholder="Type City Name" />
  68. <br /><br />
  69. <input class="btn btn-default" type="submit" value="submit">
  70. </form>
  71. </div>
  72. </body>
  73. </html>
  74.  
  75. <script>
  76. $(document).ready(function(){
  77. $('#region').typeahead({
  78. source: function(query, result){
  79. //call php variable into javascript by using php print method
  80. var country = "<?php print($country_code); ?>";
  81. $.ajax({
  82. url:"fetch.php",
  83. method:"POST",
  84. data:{country_code:country,query:query},
  85. dataType:"json",
  86. success:function(data){
  87. result($.map(data, function(item){
  88. return item;
  89. }));
  90. }
  91. })
  92. }
  93. });
  94. $('#city').typeahead({
  95. source: function(query1, result){
  96. var name = document.getElementById("region").value;
  97. //call php variable into javascript by using php print method
  98. var country = "<?php print($country_code); ?>";
  99. $.ajax({
  100. url:"fetch.php",
  101. method:"POST",
  102. data:{country_code:country,region_name:name,query1:query1},
  103. dataType:"json",
  104. success:function(data){
  105. result($.map(data, function(item){
  106. return item;
  107. }));
  108. }
  109. })
  110. }
  111. });
  112. });
  113. </script>
  114.  
  115.  
  116. Step 2: Create fetch.php file
  117.  
  118. <?php
  119. //connect to database
  120. $connect = mysqli_connect("localhost", "root", "", "ip2location");
  121.  
  122. //select the region name based on the user input
  123. if(empty($_POST["query1"])) {
  124. //retrieve the country name from index.php
  125. $country_code = $_POST['country_code'];
  126. //retrieve user input to do autocomplete
  127. $request = mysqli_real_escape_string($connect, $_POST["query"]);
  128. $query = "select DISTINCT region_name from ip2location_db3 where country_code = '".$country_code."' AND region_name LIKE '{$request}%' GROUP BY region_name";
  129. $result = mysqli_query($connect, $query);
  130. $data = array();
  131. if(mysqli_num_rows($result) > 0){
  132. while($row = mysqli_fetch_assoc($result)){
  133. $data[] = $row["region_name"];
  134. }
  135. echo json_encode($data);
  136. }
  137. }else{
  138. //select the city name based on the user input
  139.  
  140. //retrieve the country name from index.php
  141. $country_code = $_POST['country_code'];
  142. //retrieve user input to do autocomplete
  143. $request = mysqli_real_escape_string($connect, $_POST["query1"]);
  144. $region_name = mysqli_real_escape_string($connect, $_POST["region_name"]);
  145. $query = "select DISTINCT city_name from ip2location_db3 where country_code = '".$country_code."' AND region_name = '".$region_name."' AND city_name LIKE '{$request}%' GROUP BY city_name";
  146. $result = mysqli_query($connect, $query);
  147. $data = array();
  148. if(mysqli_num_rows($result) > 0){
  149. while($row = mysqli_fetch_assoc($result)){
  150. $data[] = $row["city_name"];
  151. }
  152. echo json_encode($data);
  153. }
  154. }
  155. ?>
Add Comment
Please, Sign In to add comment