Advertisement
demetriusPop

insert and display

Dec 14th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  
  5. Template Name: myTemp
  6.  
  7. */
  8.  
  9. get_header();
  10.  
  11. /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>> INSERT//Update address Table */
  12. function add_address() {
  13.  
  14. global $wpdb;
  15.  
  16.  
  17.  
  18. if ( isset( $_POST['address'] ) && $_POST['address'] != '' && isset( $_POST['city']) && $_POST['city'] != '' ) {
  19. $address = $_POST['address'];
  20. $city = $_POST['city'];
  21. }
  22.  
  23. // JSON
  24. $address_results = gmap_geocode($address);
  25. $encoded_address_results = json_encode($address_results);
  26. // Customize Query \\ send coordinates to coords column // send address to address column
  27.  
  28. // Insert
  29. $address_table = $wpdb->prefix . "address_table";
  30.  
  31. $wpdb->insert($address_table, array('address' => $address, 'json' => $encoded_address_results, 'city' => $city) );
  32.  
  33. }
  34. add_address();
  35.  
  36.  
  37.  
  38.  
  39. /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>> Read and Get From Table */
  40. function get_address() {
  41. global $wpdb;
  42.  
  43. $address_table = $wpdb->prefix . "address_table";
  44.  
  45. $query = $wpdb->get_results('SELECT * FROM ' . $address_table);
  46.  
  47. global $single_query;
  48.  
  49. foreach($query as $single_query) {
  50.  
  51. global $single_query;
  52.  
  53. }
  54. //Put the lat , lng results into array, return array and assign to var. Then use array keys where necessary
  55. $data = json_decode($single_query->json);
  56. $result = array();
  57. $result['lat'] = $data->results[0]->geometry->location->lat;
  58. $result['long'] = $data->results[0]->geometry->location->lng;
  59. return $result;
  60. }
  61. $latlong = get_address();
  62.  
  63.  
  64.  
  65. ?>
  66.  
  67. <!--/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>> Form and Map Script */-->
  68. <div id="map" style="width:300px; height:300px;"></div>
  69.  
  70. <!--Have to get lat and lng from database-->
  71. <script>
  72. var geocoder;
  73. var map;
  74. function initialize() {
  75. var mapOptions = {
  76. center: new google.maps.LatLng(<?php echo $latlong['lat'] ?>, <?php echo $latlong['long'] ?>),
  77. zoom: 8,
  78. mapTypeId: google.maps.MapTypeId.ROADMAP
  79. };
  80. var map = new google.maps.Map(document.getElementById("map"),
  81. mapOptions);
  82. }
  83. google.maps.event.addDomListener(window, 'load', initialize);
  84. </script>
  85.  
  86.  
  87. <form method="post" action="" id="insertForm" name="insertForm">
  88. <input type="text" value="" placeholder="enter city" id="city" name="city">
  89. <input type="text" value="" placeholder="enter full address" id="address" name="address">
  90. <input type="submit" value="submit">
  91. </form>
  92.  
  93.  
  94. <?php
  95.  
  96. get_footer();
  97.  
  98. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement