Advertisement
Guest User

Untitled

a guest
Jan 25th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. <?
  2.  
  3. $dbconn = pg_connect("host=192.168.0.19 port=5432 dbname=taxi2 user=postgres password=root")
  4. or die('Could not connect: ' . pg_last_error());
  5.  
  6. $streets = get_ss();
  7. foreach($streets AS $street){
  8.  
  9. $streetalien = get_sa($street['idx_streetaliens']);
  10.  
  11. if(stripos($streetalien['name'],'(САМАРА)') === false){
  12. continue;
  13. }
  14.  
  15. $builds = get_bl($streetalien['idx']);
  16.  
  17. $streetalien['name'] = str_replace('(САМАРА)','(САМАРА для НСК)',$streetalien['name']);
  18. $idx_sa = insert_sa($streetalien);//$street['idx_streetaliens'];//
  19.  
  20. echo "idx_sa: $idx_sa\n";
  21.  
  22. $street['idx_streetaliens'] = $idx_sa;
  23. $idx_ss = $street['idx'];
  24. update_ss($idx_ss,$idx_sa);
  25.  
  26. echo "idx_ss: $idx_ss\n";
  27.  
  28. $idx_bl = 0; $first = true;
  29. foreach($builds AS $build){
  30. $build['idx_street'] = $idx_sa;
  31. $idx_bl = insert_bl($build);
  32. if($first){
  33. echo "idx_bl: $idx_bl";
  34. $first = false;
  35. }
  36. }
  37. echo " - $idx_bl\n";
  38.  
  39. }
  40.  
  41. pg_close($dbconn);
  42.  
  43. function get_ss(){
  44.  
  45. $res = array();
  46.  
  47. $query = "SELECT * FROM ulica WHERE cityid = 4";// LIMIT 5 OFFSET 10
  48. $result = pg_query($query) or die('Ошибка запроса: ' . pg_last_error());
  49. while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
  50. $res[] = $line;
  51. }
  52. pg_free_result($result);
  53.  
  54. return $res;
  55.  
  56. }
  57. function get_sa($id){
  58.  
  59. $res = array();
  60.  
  61. $query = "SELECT * FROM streetaliens WHERE idx = $id LIMIT 1";
  62. $result = pg_query($query) or die('Ошибка запроса: ' . pg_last_error());
  63. while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
  64.  
  65. $res = $line;
  66.  
  67. $line_utf = $line;
  68. $line_utf['name'] = iconv('cp1251','utf8',$line_utf['name']);
  69. echo $line_utf['name']."\n";//print_r($line_utf);
  70.  
  71. }
  72. pg_free_result($result);
  73.  
  74. return $res;
  75.  
  76. }
  77. function get_bl($id){
  78.  
  79. $res = array();
  80.  
  81. $query = "SELECT * FROM buildinglocation WHERE idx_street = $id";
  82. $result = pg_query($query) or die('Ошибка запроса: ' . pg_last_error());
  83. while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
  84. $res[] = $line;
  85. }
  86. pg_free_result($result);
  87.  
  88. return $res;
  89.  
  90. }
  91.  
  92. function update_ss($idx_ss,$idx_sa){
  93.  
  94. $result = pg_query("
  95. UPDATE ulica SET idx_streetaliens = $idx_sa WHERE idx = $idx_ss
  96. ") or die('Ошибка запроса: ' . pg_last_error());
  97.  
  98. }
  99.  
  100. function insert_sa($row){
  101.  
  102. $result = pg_query("
  103. INSERT INTO streetaliens
  104. (
  105. name
  106. ) values
  107. (
  108. '$row[name]'
  109. ) RETURNING idx;
  110. ") or die('Ошибка запроса: ' . pg_last_error());
  111. $insert_id = pg_fetch_array($result);
  112.  
  113. return $insert_id[0];
  114.  
  115. }
  116. function insert_ss($row){
  117.  
  118. $row['rayonid'] = intval($row['rayonid']);
  119.  
  120. $result = pg_query("
  121. INSERT INTO ulica
  122. (
  123. ulica,rayonid,comment,deleted,tolist,
  124. name,idx_streetaliens,canreject,zagorod,specnotice,
  125. ulicacategoryid,cityid,displaypriority,kladr_code,fts
  126. ) values
  127. (
  128. '$row[ulica]',$row[rayonid],'$row[comment]','$row[deleted]','$row[tolist]',
  129. '$row[name]','$row[idx_streetaliens]','$row[canreject]','$row[zagorod]','$row[specnotice]',
  130. '$row[ulicacategoryid]','$row[cityid]','$row[displaypriority]','$row[kladr_code]','$row[fts]'
  131. ) RETURNING idx;
  132. ") or die('Ошибка запроса: ' . pg_last_error());
  133. $insert_id = pg_fetch_array($result);
  134.  
  135. return $insert_id[0];
  136.  
  137. }
  138. function insert_bl($row){
  139.  
  140. $result = pg_query("
  141. INSERT INTO buildinglocation
  142. (
  143. idx_street,house,latitude,longitude,name
  144. ) values
  145. (
  146. '$row[idx_street]','$row[house]','$row[latitude]','$row[longitude]','$row[name]'
  147. ) RETURNING idx;
  148. ") or die('Ошибка запроса: ' . pg_last_error());
  149. $insert_id = pg_fetch_array($result);
  150.  
  151. return $insert_id[0];
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement