Guest User

Untitled

a guest
Apr 9th, 2018
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. <?php
  2. $servername = "";
  3. $username = "";
  4. $password = "";
  5. $dbname = "";
  6.  
  7. // Crea conexion
  8. $conn = new mysqli($servername, $username, $password, $dbname);
  9. if ($conn->connect_error) {
  10. die("Connection failed: " . $conn->connect_error);
  11. }
  12.  
  13. lista_entidad( $conn, "tabla", "geojson");
  14.  
  15. $conn->close();
  16. /*
  17. Crea un archivo geojson por cada entidad que se le envie
  18. */
  19. function crea_json( $file, $tabla, $contenido){
  20. $myfile = fopen($file, "w") or die("Unable to open file!");
  21. fwrite($myfile, '{ "type": "MultiPolygon", "coordinates": [[['.$contenido.']]]}');
  22. fclose($myfile);
  23. }
  24.  
  25.  
  26. /*
  27. Recorre la tabla por cada entidad
  28. */
  29. function lista_entidad( $conn, $tabla, $campo){
  30. $sql = "SELECT * FROM $tabla";
  31. $result = $conn->query($sql);
  32.  
  33. if ($result->num_rows > 0) {
  34. while($row = $result->fetch_assoc()) {
  35. crea_json( $row["id"].".json", "", $row[$campo] );
  36. }
  37. } else {
  38. echo "0 results";
  39. }
  40. }
  41. ?>
Add Comment
Please, Sign In to add comment