mysql - Using current lat/lng to query database and return data to user in php -
so have issue want able query data user's current location still new json , trying figure out how send data main page.
here hidden inputs lat/lng values on index.php page
<input type="hidden" id="latitude"> <input type="hidden" id="longitude">
here code send lat/lng tables.php index.php page
$.getjson(geocoding).done(function(location) { $('#latitude').html(position.coords.latitude); $('#longitude').html(position.coords.longitude); $.ajax({ url:'table.php', method: 'post', datatype: 'json', data:{ lat: $('#latitude').val(), lng: $('#longitude').val() }, success: function(data){ console.log(); } }); });
i using datatables display data on index.php page.
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.12/css/jquery.datatables.css"> <script> $(document).ready(function(){ var datatable = $('#searchtable').datatable(); $("#searchbox").keyup(function() { datatable.fnfilter(this.value); }); }); </script> echo'<table id="searchtable" class="display"> <thead> <tr> <th>latitude</th> <th>longitude</th> </tr> </thead> <tbody> '; (data display tables.php) echo'</tbody> </table> ';
and on tables.php page pulling data mysql database, have figure out how send data index.php page through json , display in table.
<?php require_once("connect.php"); $lat = isset($_post['lat']) ? $_post['lat'] : null; $lng = isset($_post['lng']) ? $_post['lng'] : null; if($lat && $lng){ $locations = array(); $stmt = "select * location limit 500"; //query data table $stmt = $conn->prepare($stmt); $stmt->execute(); if($stmt->rowcount() > 0){ $result = $stmt->fetchall(); foreach($result $row){ echo'<tr> <td>'; $locations['latitude'][] = $row; echo'</td> <td>'; $locations['longitude'][] = $row; echo'</td> </tr>'; } } return json_encode($locations); } ?>
what looking update table data database based on user's current location, when user lands on index.php page, or user can search location using search bar provided on index.php page.
thank in advance time.
you need first send json correctly php file, , receive @ ajax response , parse use further show in data tables. see below codes help.
client side code:
$.getjson(geocoding).done(function(location) { $('#latitude').html(position.coords.latitude); $('#longitude').html(position.coords.longitude); $.ajax({ url:'table.php', method: 'post', datatype: 'json', data:{ lat: $('#latitude').val(), lng: $('#longitude').val() }, success: function(data){ // here need parse json data , put in data table rows looping on it, see example code #e1 console.log(); } }); });
php code:
<?php require_once("connect.php"); $lat = isset($_post['lat']) ? $_post['lat'] : null; $lng = isset($_post['lng']) ? $_post['lng'] : null; if($lat && $lng){ $locations = array(); $stmt = "select * location limit 500"; //query data table $stmt = $conn->prepare($stmt);$stmt->execute(); if($stmt->rowcount() > 0){ $result = $stmt->fetchall(); foreach($result $row){ $result[] = ['col1'=>$row['col1'],'col2'=>$row['col2']]; // here need add many columns want show in data table } } print json_encode($result);die; } ?>
example codes:-
_#e1_ console.log(json.stringify(json.topics)); $.each(json.topics, function(idx, topic){ $("#nav").html('<a href="' + topic.link_src + '">' + topic.link_text + "</a>"); });
apply code per need , resolve problem. assuming getting proper data database query.
hope helps.
thanks
Comments
Post a Comment