Witam, mam mały problem z zapisaniem wielu rekordów z pliku xml jednocześnie , poniżej jest mój skrypt zapisuje on jeden rekord i tylko ostatni z pliku xml , co muszę poprawić aby zapisywał wszystkie rekordy do bazy mysql ?
//Parsal
$markers = simplexml_load_file('http://www.meteo.gr/WFSail/templates/Gdata2.xml');
foreach ($markers->GROUND_STATIONS as $pogoda) {
$tmp = $pogoda->attributes();
$tmp['station_name'];
$tmp['station_id'];
$tmp['od_date'];
$tmp['od_time'];
$tmp['lat'];
$tmp['long'];
}
// odbieramy dane z xml dane stacji
$station_name = $tmp['station_name'];
$station_id = $tmp['station_id'];
$od_date=$tmp['od_date'];
$od_time=$tmp['od_time'];
$lat = $tmp['lat'];
$lng = $tmp['long'];
foreach ($pogoda->OBSERVATIONS_GREECE as $rozne){
$tmp = $rozne->attributes();
$tmp['wind_dir'];
$tmp['wind_force'];
$tmp['temperature'];
$tmp['pressure'];
}
//Odbieramy dane z pliku xml dane pomiaru
$wind_dir=$tmp['wind_dir'];
$wind_force=$tmp['wind_force'];
$temperature=$tmp['temperature'];
$pressure=$tmp['pressure'];
foreach ($rozne->OBSERVATIONS_PHENOMENA as $tekst){
$tmp = $tekst->attributes();
$tmp['DIRTEXT'];
$tmp['descr'];
}
//Odbieramy dane z pliku xml dane pomiaru czesc druga
$DIRTEXT=$tmp['DIRTEXT'];
$descr=$tmp['descr'];
if($station_name and $station_id and $od_date and $od_time and $lat and $lng and $wind_dir and $wind_force and $temperature and $pressure and $DIRTEXT
and $descr) {
// łączymy się z bazą danych
or
die('Brak połączenia z serwerem MySQL'); or
die('Nie mogę połączyć się z bazą danych');
// dodaje dane stacji do bazy
station_name='$station_name',station_id='$station_id',dzien='$od_date',czas='$od_time',lat='$lat',lng='$lng',wind_dir='$wind_dir',wind_force='$wind_forc
e',temperature='$temperature',pressure='$pressure',DIRTEXT='$DIRTEXT',descr='$descr'");
if($ins) echo "Rekord został dodany poprawnie"; else echo "Błąd nie udało się dodać nowego rekordu";
}
Problem prawie rozwiązany, tylko teraz dziwnie zapisują mi się dane w tabeli a mianowicie w trzech rekordach są dane z jednej stacji to jest kod
// łšczymy się z bazš danych
or
die('Brak połšczenia z serwerem MySQL'); or
die('Nie mogę połšczyć się z bazš danych');
//Parsal
$markers = simplexml_load_file('http://www.meteo.gr/WFSail/templates/Gdata2.xml');
foreach ($markers->GROUND_STATIONS as $pogoda) {
$tmp = $pogoda->attributes();
$tmp['station_name'];
$tmp['station_id'];
$tmp['od_date'];
$tmp['od_time'];
$tmp['lat'];
$tmp['long'];
$ins = mysql_query("INSERT INTO nazwa tabeli SET station_name='$station_name',station_id='$station_id',dzien='$od_date',czas='$od_time',lat='$lat',lng='$lng'");
// odbieramy dane z xml dane stacji
$station_name = $tmp['station_name'];
$station_id = $tmp['station_id'];
$od_date=$tmp['od_date'];
$od_time=$tmp['od_time'];
$lat = $tmp['lat'];
$lng = $tmp['long'];
foreach ($pogoda->OBSERVATIONS_GREECE as $rozne){
$tmp = $rozne->attributes();
$tmp['wind_dir'];
$tmp['wind_force'];
$tmp['temperature'];
$tmp['pressure'];
$ins = mysql_query("INSERT INTO nazwa tabeli SET wind_dir='$wind_dir',wind_force='$wind_force',temperature='$temperature',pressure='$pressure'"); //Odbieramy dane z pliku xml dane pomiaru
$wind_dir=$tmp['wind_dir'];
$wind_force=$tmp['wind_force'];
$temperature=$tmp['temperature'];
$pressure=$tmp['pressure'];
foreach ($rozne->OBSERVATIONS_PHENOMENA as $tekst){
$tmp = $tekst->attributes();
$tmp['DIRTEXT'];
$tmp['descr'];
}
}
}
//Odbieramy dane z pliku xml dane pomiaru czesc druga
$DIRTEXT=$tmp['DIRTEXT'];
$descr=$tmp['descr'];
$ins = mysql_query("INSERT INTO nazwa tabeli SET DIRTEXT='$DIRTEXT',descr='$descr'");
if($station_name and $station_id and $od_date and $od_time and $lat and $lng and $wind_dir and $wind_force and $temperature and $pressure and $DIRTEXT and $descr) {
if($ins) echo "Rekord został dodany poprawnie"; else echo "Błšd nie udało się dodać nowego rekordu"; }
Ten post edytował slawekxx 5.09.2010, 10:26:14