Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: skrypt do formularza rejestracyjnego
Forum PHP.pl > Forum > PHP
uczeń php
Szukam skryptu formularza rejestracyjnego, w którym po podaniu kodu pocztowego automatycznie by się wypełniały takie pola jak województwo, powiat i gmina oraz nazwa miejscowości. Dodatkowo chciałbym aby kod pocztowy był w formie dwa pola input myślnik trzy pola input. Proszę o szybko odpowiedź.
konole
Nikt ci za darmo takiego skryptu nie udostępni.

http://www.poczta-polska.pl/spispna/spispna.pdf
uczeń php
znalazłem rozwiązanie dane z google maps api są zwracane w postaci json teraz te dane chciałbym zapisać do bazy danych. Czy ktoś wie jak to zrobić zapisać dane z json do bd ?
nospor
http://pl.php.net/manual/en/function.json-decode.php
I masz już tablicę. A dane z tablicy to do bazy już chyba umiesz zapisać?
uczeń php
tylko, że json wykonuje się po stronie klienta a nie po stronie serwera.
nospor
No to jak masz po stronie klienta swojego jsona to teraz przekazuj te dane ajaxem na serwer i zapisuj do bazy.

Albo odpalaj google api z poziomiu serwera. Przecież google też ma specjalne api i na tę okazję
uczeń php
  1. $dane = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address={$_GET['kod_pocztowy']},+{$_GET['kraj']}&sensor=true");
  2.  
  3. echo $dane.'<br />';
  4.  
  5. var_dump(json_decode($dane, true));

w przeglądarce wyświetliło mi się coś takiego:

{ "status": "OK", "results": [ { "types": [ "postal_code" ], "formatted_address": "02-999 Warsaw, Poland", "address_components": [ { "long_name": "02-999", "short_name": "02-999", "types": [ "postal_code" ] }, { "long_name": "Wilanów", "short_name": "Wilanów", "types": [ "sublocality", "political" ] }, { "long_name": "Warsaw", "short_name": "Warsaw", "types": [ "locality", "political" ] }, { "long_name": "Warsaw", "short_name": "Warsaw", "types": [ "administrative_area_level_2", "political" ] }, { "long_name": "Mazovia", "short_name": "Mazovia", "types": [ "administrative_area_level_1", "political" ] }, { "long_name": "Poland", "short_name": "PL", "types": [ "country", "political" ] } ], "geometry": { "location": { "lat": 52.1608648, "lng": 21.0795830 }, "location_type": "APPROXIMATE", "viewport": { "southwest": { "lat": 52.1577172, "lng": 21.0764354 }, "northeast": { "lat": 52.1640125, "lng": 21.0827306 } }, "bounds": { "southwest": { "lat": 52.1592883, "lng": 21.0777915 }, "northeast": { "lat": 52.1624414, "lng": 21.0813745 } } }, "partial_match": true } ] }

array
'status' => string 'OK' (length=2)
'results' =>
array
0 =>
array
'types' =>
array
...
'formatted_address' => string '02-999 Warsaw, Poland' (length=21)
'address_components' =>
array
...
'geometry' =>
array
...
'partial_match' => boolean true

jak dane które są zawarte pomiędzy nawiasami klamrowymi zapisać do bazy danych ?
nospor
Jakie nawiasy klamrowe... nie widzisz, że json_decode zwraca ci już tablicę? Mając tablicę robisz z nią co chcesz, wyciągasz bez problemu dane jakie chcesz.
uczeń php
ok tylko, że w nawiasach klamrowych podane są współrzędne geograficzne a już w tablicy ich nie widać w nawiasach klamrowych podana jest nazwa dzielnicy nawet a w tablicy jej nie widać
nospor
mission imposible 15 wkrótce w kinach.... wink.gif

No bo żeś wykropkował pół tablicy to tych danych nie widzisz. Tablica jest generowana całkowicie w pełni
  1. $zm = '{ "status": "OK", "results": [ { "types": [ "postal_code" ], "formatted_address": "02-999 Warsaw, Poland", "address_components": [ { "long_name": "02-999", "short_name": "02-999", "types": [ "postal_code" ] }, { "long_name": "Wilanów", "short_name": "Wilanów", "types": [ "sublocality", "political" ] }, { "long_name": "Warsaw", "short_name": "Warsaw", "types": [ "locality", "political" ] }, { "long_name": "Warsaw", "short_name": "Warsaw", "types": [ "administrative_area_level_2", "political" ] }, { "long_name": "Mazovia", "short_name": "Mazovia", "types": [ "administrative_area_level_1", "political" ] }, { "long_name": "Poland", "short_name": "PL", "types": [ "country", "political" ] } ], "geometry": { "location": { "lat": 52.1608648, "lng": 21.0795830 }, "location_type": "APPROXIMATE", "viewport": { "southwest": { "lat": 52.1577172, "lng": 21.0764354 }, "northeast": { "lat": 52.1640125, "lng": 21.0827306 } }, "bounds": { "southwest": { "lat": 52.1592883, "lng": 21.0777915 }, "northeast": { "lat": 52.1624414, "lng": 21.0813745 } } }, "partial_match": true } ] }';
  2. $tab = json_decode($zm,true);
  3. print_r($tab);


Kod
Array
(
    [status] => OK
    [results] => Array
        (
            [0] => Array
                (
                    [types] => Array
                        (
                            [0] => postal_code
                        )

                    [formatted_address] => 02-999 Warsaw, Poland
                    [address_components] => Array
                        (
                            [0] => Array
                                (
                                    [long_name] => 02-999
                                    [short_name] => 02-999
                                    [types] => Array
                                        (
                                            [0] => postal_code
                                        )

                                )

                            [1] => Array
                                (
                                    [long_name] => WilanĂłw
                                    [short_name] => WilanĂłw
                                    [types] => Array
                                        (
                                            [0] => sublocality
                                            [1] => political
                                        )

                                )

                            [2] => Array
                                (
                                    [long_name] => Warsaw
                                    [short_name] => Warsaw
                                    [types] => Array
                                        (
                                            [0] => locality
                                            [1] => political
                                        )

                                )

                            [3] => Array
                                (
                                    [long_name] => Warsaw
                                    [short_name] => Warsaw
                                    [types] => Array
                                        (
                                            [0] => administrative_area_level_2
                                            [1] => political
                                        )

                                )

                            [4] => Array
                                (
                                    [long_name] => Mazovia
                                    [short_name] => Mazovia
                                    [types] => Array
                                        (
                                            [0] => administrative_area_level_1
                                            [1] => political
                                        )

                                )

                            [5] => Array
                                (
                                    [long_name] => Poland
                                    [short_name] => PL
                                    [types] => Array
                                        (
                                            [0] => country
                                            [1] => political
                                        )

                                )

                        )

                    [geometry] => Array
                        (
                            [location] => Array
                                (
                                    [lat] => 52.1608648
                                    [lng] => 21.079583
                                )

                            [location_type] => APPROXIMATE
                            [viewport] => Array
                                (
                                    [southwest] => Array
                                        (
                                            [lat] => 52.1577172
                                            [lng] => 21.0764354
                                        )

                                    [northeast] => Array
                                        (
                                            [lat] => 52.1640125
                                            [lng] => 21.0827306
                                        )

                                )

                            [bounds] => Array
                                (
                                    [southwest] => Array
                                        (
                                            [lat] => 52.1592883
                                            [lng] => 21.0777915
                                        )

                                    [northeast] => Array
                                        (
                                            [lat] => 52.1624414
                                            [lng] => 21.0813745
                                        )

                                )

                        )

                    [partial_match] => 1
                )

        )

)
uczeń php
ok tylko mam jeszcze jeden problem jak się wrzuci do adresu url http://maps.googleapis.com/maps/api/geocod...amp;sensor=true to następuje pobranie pliku z danymi formacie json tylko te dane wyświetlane w tym pobranym pliku są po polsku natomiast
  1. $dane = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address={$_GET['kod_pocztowy']},+{$_GET['kraj']}&sensor=true");
to te dane są po angielsku. Przykład plik jsona pobrany na komupter zawartość:

{
"status": "OK",
"results": [ {
"types": [ "postal_code" ],
"formatted_address": "02-999 Warszawa, Polska",
"address_components": [ {
"long_name": "02-999",
"short_name": "02-999",
"types": [ "postal_code" ]
}, {
"long_name": "Wilanów",
"short_name": "Wilanów",
"types": [ "sublocality", "political" ]
}, {
"long_name": "Warszawa",
"short_name": "Warszawa",
"types": [ "locality", "political" ]
}, {
"long_name": "Warszawa",
"short_name": "Warszawa",
"types": [ "administrative_area_level_2", "political" ]
}, {
"long_name": "Mazowieckie",
"short_name": "Mazowieckie",
"types": [ "administrative_area_level_1", "political" ]
}, {
"long_name": "Polska",
"short_name": "PL",
"types": [ "country", "political" ]
} ],
"geometry": {
"location": {
"lat": 52.1608648,
"lng": 21.0795830
},
"location_type": "APPROXIMATE",
"viewport": {
"southwest": {
"lat": 52.1577172,
"lng": 21.0764354
},
"northeast": {
"lat": 52.1640125,
"lng": 21.0827306
}
},
"bounds": {
"southwest": {
"lat": 52.1592883,
"lng": 21.0777915
},
"northeast": {
"lat": 52.1624414,
"lng": 21.0813745
}
}
}
} ]
}
kod w php
  1. $dane = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address={$_GET['kod_pocztowy']},+{$_GET['kraj']}&sensor=true");
  2.  
  3.  
  4.  
  5. $tab = json_decode($dane,true);
  6.  
  7. print_r($tab);

gdzie $_GET['kod_pocztowy'] = 02-999 i $_GET['kraj'] = poland
w przeglądarce daje o to taki wynik:

Array ( [status] => OK [results] => Array ( [0] => Array ( [types] => Array ( [0] => postal_code ) [formatted_address] => 02-999 Warsaw, Poland [address_components] => Array ( [0] => Array ( [long_name] => 02-999 [short_name] => 02-999 [types] => Array ( [0] => postal_code ) ) [1] => Array ( [long_name] => Wilanów [short_name] => Wilanów [types] => Array ( [0] => sublocality [1] => political ) ) [2] => Array ( [long_name] => Warsaw [short_name] => Warsaw [types] => Array ( [0] => locality [1] => political ) ) [3] => Array ( [long_name] => Warsaw [short_name] => Warsaw [types] => Array ( [0] => administrative_area_level_2 [1] => political ) ) [4] => Array ( [long_name] => Mazovia [short_name] => Mazovia [types] => Array ( [0] => administrative_area_level_1 [1] => political ) ) [5] => Array ( [long_name] => Poland [short_name] => PL [types] => Array ( [0] => country [1] => political ) ) ) [geometry] => Array ( [location] => Array ( [lat] => 52.1608648 [lng] => 21.079583 ) [location_type] => APPROXIMATE [viewport] => Array ( [southwest] => Array ( [lat] => 52.1577172 [lng] => 21.0764354 ) [northeast] => Array ( [lat] => 52.1640125 [lng] => 21.0827306 ) ) [bounds] => Array ( [southwest] => Array ( [lat] => 52.1592883 [lng] => 21.0777915 ) [northeast] => Array ( [lat] => 52.1624414 [lng] => 21.0813745 ) ) ) ) ) )

jak zrobić, żeby w przeglądarce też się wyświetliło Warszawa zamiast Warsaw ?
nospor
Zrobiły strzał. Dosłownie strzał...
dopisałem do twojego adresu: &language=pl


Także na przyszłość: najpierw strzelaj, potem pisz na forum wink.gif
uczeń php
w końcu wyszło exclamation.gif!!
  1. <?php
  2.  
  3.  
  4. echo '<form action="">
  5. <input type="text" name="kod_pocztowy" />
  6. <input type="text" name="kraj" />
  7. <input type="submit" name="generowanie_danych" value="generowanie" />
  8. </form>';
  9.  
  10.  
  11. if(isset($_GET['generowanie_danych'])) {
  12. $dane = file_get_contents("http://maps.googleapis.com/maps/api/geocode/json?address={$_GET['kod_pocztowy']},+{$_GET['kraj']}&sensor=true&language=pl");
  13.  
  14.  
  15.  
  16. $tab = json_decode($dane,true);
  17.  
  18. print_r($tab);
  19.  
  20. echo '<br />/////////////////////////////////////////////<br />';
  21.  
  22. $dane_klienta = array();
  23.  
  24. $i = 0;
  25.  
  26. foreach($tab as $dane_miejsca)
  27. if(is_array($dane_miejsca))
  28. foreach($dane_miejsca as $dane_m)
  29. if(is_array($dane_m))
  30. foreach($dane_m as $m)
  31. if(is_array($m))
  32. foreach($m as $szczegolowe_dane)
  33. if(is_array($szczegolowe_dane))
  34. foreach($szczegolowe_dane as $dane_z_koncowych_tabel)
  35. {
  36. // if(is_array($dane_z_koncowych_tabel))
  37. // foreach($dane_z_koncowych_tabel as $dane_do_bd)
  38. if(!is_array($dane_z_koncowych_tabel))
  39. {
  40. // if($dane_klienta[$i-1]!=$dane_z_koncowych_tabel)
  41. $dane_klienta[$i] = $dane_z_koncowych_tabel;
  42. echo $dane_klienta[$i];
  43. $i++;
  44. }
  45. }
  46.  
  47. }
  48. echo '<br />sprawdzenie poprawności danych do zapisu w bazie danych<br />';
  49. for($j=0; $j < count($dane_klienta); $j++){
  50. if($j>0) {
  51. if($dane_klienta[$j-1]!=$dane_klienta[$j])
  52. echo $dane_klienta[$j].' '; }
  53. else
  54. echo $dane_klienta[0].' ';
  55. }
  56.  
  57. ?>

wyświetlenie z przeglądarki:

Array ( [status] => OK [results] => Array ( [0] => Array ( [types] => Array ( [0] => postal_code ) [formatted_address] => 03-126 Warszawa, Polska [address_components] => Array ( [0] => Array ( [long_name] => 03-126 [short_name] => 03-126 [types] => Array ( [0] => postal_code ) ) [1] => Array ( [long_name] => Białołęka [short_name] => Białołęka [types] => Array ( [0] => sublocality [1] => political ) ) [2] => Array ( [long_name] => Warszawa [short_name] => Warszawa [types] => Array ( [0] => locality [1] => political ) ) [3] => Array ( [long_name] => Warszawa [short_name] => Warszawa [types] => Array ( [0] => administrative_area_level_2 [1] => political ) ) [4] => Array ( [long_name] => Mazowieckie [short_name] => Mazowieckie [types] => Array ( [0] => administrative_area_level_1 [1] => political ) ) [5] => Array ( [long_name] => Polska [short_name] => PL [types] => Array ( [0] => country [1] => political ) ) ) [geometry] => Array ( [location] => Array ( [lat] => 52.321777 [lng] => 20.9657874 ) [location_type] => APPROXIMATE [viewport] => Array ( [southwest] => Array ( [lat] => 52.31121 [lng] => 20.9507683 ) [northeast] => Array ( [lat] => 52.32349 [lng] => 20.9683775 ) ) [bounds] => Array ( [southwest] => Array ( [lat] => 52.31121 [lng] => 20.9507683 ) [northeast] => Array ( [lat] => 52.32349 [lng] => 20.9683775 ) ) ) ) ) )
/////////////////////////////////////////////
03-12603-126BiałołękaBiałołękaWarszawaWarszawaWarszawaWarszawaMazowieckieMazowieckiePolsk
aPL52.32177720.9657874
sprawdzenie poprawności danych do zapisu w bazie danych
03-126 Białołęka Warszawa Mazowieckie Polska PL 52.321777 20.9657874
Dziękuję wszystkim za pomoc exclamation.gif!
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.