Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [XML][PHP] Problem z wczytaniem danych
slupek
post 26.04.2009, 10:24:52
Post #1





Grupa: Zarejestrowani
Postów: 36
Pomógł: 0
Dołączył: 27.05.2007
Skąd: EPGD

Ostrzeżenie: (0%)
-----


Przyznaję się bez bicia, że jestem zielony w xmlu, ale proszę chociaż o małą podpowiedź, może uda mi się coś wydłubać z tego.

Mam to:

  1. <?php
  2. $xmlstr = <<<XML
  3.  
  4.  
  5. <bookings>
  6.   <timestamp>2009-04-25 22:39:25</timestamp>
  7.   <atcs>
  8.     <booking>
  9.       <id>485150969</id>
  10.       <callsign>EPWA_APP</callsign>
  11.       <name>Andrzej Stachlewski</name>
  12.       <time_start>2009-04-25 14:45:00</time_start>
  13.       <time_end>2009-04-25 15:45:00</time_end>
  14.       <cid>1048484</cid>
  15.       <src_ip>83.24.34.143</src_ip>
  16.       <added>2009-04-25 16:25:02</added>
  17.     </booking>
  18.     <booking>
  19.       <id>84751297</id>
  20.       <callsign>EPGD_APP</callsign>
  21.       <name>Andrzej Stachlewski</name>
  22.       <time_start>2009-04-25 18:00:00</time_start>
  23.       <time_end>2009-04-25 20:00:00</time_end>
  24.       <cid>1048484</cid>
  25.       <src_ip>83.24.34.143</src_ip>
  26.       <added>2009-04-25 20:25:03</added>
  27.     </booking>
  28.     <booking>
  29.       <id>954074007</id>
  30.       <callsign>EPGD_TWR</callsign>
  31.       <name>Jakub Aldas</name>
  32.       <time_start>2009-04-25 18:00:00</time_start>
  33.       <time_end>2009-04-25 20:00:00</time_end>
  34.       <cid>1010013</cid>
  35.       <src_ip>89.151.23.99</src_ip>
  36.       <added>2009-04-25 20:25:03</added>
  37.     </booking>
  38.     <booking>
  39.       <id>799466355</id>
  40.       <callsign>EPKK_TWR</callsign>
  41.       <name>Artur Dermanowski</name>
  42.       <time_start>2009-04-25 18:00:00</time_start>
  43.       <time_end>2009-04-25 20:00:00</time_end>
  44.       <cid>972766</cid>
  45.       <src_ip>83.6.129.210</src_ip>
  46.       <added>2009-04-25 20:25:03</added>
  47.     </booking>
  48.     <booking>
  49.       <id>297866008</id>
  50.       <callsign>EPKK_APP</callsign>
  51.       <name>Marcin Blitek</name>
  52.       <time_start>2009-04-25 18:00:00</time_start>
  53.       <time_end>2009-04-25 20:00:00</time_end>
  54.       <cid>1023507</cid>
  55.       <src_ip>79.97.111.112</src_ip>
  56.       <added>2009-04-25 20:25:03</added>
  57.     
  58.  
  59. XML;
  60. ?>


gdzie pozycji booking mam znacznie więcej, tutaj tylko poglądowo


oraz to:

  1. <?php
  2.  
  3.   include 'atcbok.php';
  4.  
  5.   $xml = new SimpleXMLElement($xmlstr);
  6.  
  7.   foreach ( $xml->bookings->atcs->booking as $booking )  
  8.   {
  9.       echo $booking,  '<br />';
  10.   }
  11.  
  12. ?>


A otrzymuję to:

Warning: Invalid argument supplied for foreach() in /home/fazi/public_html/atctab.php on line 7


Będę wdzięczny za jakąkolwiek pomoc.

Ten post edytował slupek 9.05.2009, 12:45:56
Go to the top of the page
+Quote Post
c3zi
post 26.04.2009, 10:53:06
Post #2





Grupa: Zarejestrowani
Postów: 266
Pomógł: 20
Dołączył: 15.11.2006
Skąd: Koszalin

Ostrzeżenie: (0%)
-----


W Twoim xml'u masz niedomknięte tagi..

Masz tutaj poprawiony kod:

Wczytywanie xml


--------------------
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Go to the top of the page
+Quote Post
slupek
post 26.04.2009, 13:29:09
Post #3





Grupa: Zarejestrowani
Postów: 36
Pomógł: 0
Dołączył: 27.05.2007
Skąd: EPGD

Ostrzeżenie: (0%)
-----


Świetnie, mam teraz dane wszystkie wyświetlone. A jeśli chcę wyciągnąć tylko trzy pola z całego kodu dla każdego <booking> z osobna:

np.

  1. <callsign><time_start><time_end>


i to bym sobie powkładał w tabelkę.

Co muszę zmienić w kodzie, by to wydostać z pliku xml?

EDIT:

Dodam, że dane mają być pobierane stąd. Do tej pory pobierałem z bazy sql i wszystko śmigało jak należy. Teraz mm nową jakość i ból głowy. Proszę o pomoc, o ile możliwe.

Ten post edytował slupek 26.04.2009, 13:34:43
Go to the top of the page
+Quote Post
c3zi
post 26.04.2009, 15:58:21
Post #4





Grupa: Zarejestrowani
Postów: 266
Pomógł: 20
Dołączył: 15.11.2006
Skąd: Koszalin

Ostrzeżenie: (0%)
-----


Po prostu odwołaj się do tych pól, przykład:

Wczytywanie xml - konkretne pola


--------------------
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Go to the top of the page
+Quote Post
slupek
post 9.05.2009, 13:15:24
Post #5





Grupa: Zarejestrowani
Postów: 36
Pomógł: 0
Dołączył: 27.05.2007
Skąd: EPGD

Ostrzeżenie: (0%)
-----


Ruszyłem! Jestem ogromnie wdzięczny za pomoc, zaczynam łapać, teraz już wiem w jakim iść kierunku!

Stawiam!

Pojawił się jeszcze jeden problem.

Z podanego xmla dane są pobierane i jet ok:

  1. <?php
  2. $xmlstr = <<<XML
  3. <bookings>
  4.  <timestamp>2009-04-25 22:39:25</timestamp>
  5.  <atcs>
  6.    <booking>
  7.      <id>485150969</id>
  8.      <callsign>EPWA_APP</callsign>
  9.      <name>Andrzej Stachlewski</name>
  10.      <time_start>2009-04-25 14:45:00</time_start>
  11.      <time_end>2009-04-25 15:45:00</time_end>
  12.      <cid>1048484</cid>
  13.      <src_ip>83.24.34.143</src_ip>
  14.      <added>2009-04-25 16:25:02</added>
  15.    </booking>
  16.    <booking>
  17.      <id>84751297</id>
  18.      <callsign>EPGD_APP</callsign>
  19.      <name>Andrzej Stachlewski</name>
  20.      <time_start>2009-04-25 18:00:00</time_start>
  21.      <time_end>2009-04-25 20:00:00</time_end>
  22.      <cid>1048484</cid>
  23.      <src_ip>83.24.34.143</src_ip>
  24.      <added>2009-04-25 20:25:03</added>
  25.    </booking>
  26.    <booking>
  27.      <id>954074007</id>
  28.      <callsign>EPGD_TWR</callsign>
  29.      <name>Jakub Aldas</name>
  30.      <time_start>2009-04-25 18:00:00</time_start>
  31.      <time_end>2009-04-25 20:00:00</time_end>
  32.      <cid>1010013</cid>
  33.      <src_ip>89.151.23.99</src_ip>
  34.      <added>2009-04-25 20:25:03</added>
  35.    </booking>
  36.    <booking>
  37.      <id>799466355</id>
  38.      <callsign>EPKK_TWR</callsign>
  39.      <name>Artur Dermanowski</name>
  40.      <time_start>2009-04-25 18:00:00</time_start>
  41.      <time_end>2009-04-25 20:00:00</time_end>
  42.      <cid>972766</cid>
  43.      <src_ip>83.6.129.210</src_ip>
  44.      <added>2009-04-25 20:25:03</added>
  45.    </booking>
  46.    <booking>
  47.      <id>297866008</id>
  48.      <callsign>EPKK_APP</callsign>
  49.      <name>Marcin Blitek</name>
  50.      <time_start>2009-04-25 18:00:00</time_start>
  51.      <time_end>2009-04-25 20:00:00</time_end>
  52.      <cid>1023507</cid>
  53.      <src_ip>79.97.111.112</src_ip>
  54.      <added>2009-04-25 20:25:03</added>
  55.    </booking>
  56.   </atcs>  
  57. </bookings>
  58. XML;
  59.  
  60.  
  61.  
  62.  $xml = new SimpleXMLElement($xmlstr);
  63.    
  64.  
  65.  foreach ( $xml->atcs->booking as $key => $value )  
  66.  {
  67.      echo ( $value->callsign );
  68. echo '<br>';
  69. echo ( $value->time_start );
  70. echo '<br>';
  71. echo ( $value->time_end );
  72. echo '<br>';
  73.  }
  74. ?>


Ale jak zrobić, by podany xml był includoway z konkretnego adresu (np. ten podany w kodzie poniżej):

  1. <?php
  2. $xmlstr = <<<XML
  3.  
  4.  
  5.   include('http://vatbook.euroutepro.com/xml2.php?fir=EPWW');
  6.  
  7.  
  8.  
  9.  
  10. XML;
  11.  
  12. $xml = new SimpleXMLElement($xmlstr);
  13.    
  14.  
  15.  foreach ( $xml->atcs->booking as $key => $value )  
  16.  {
  17.    echo ( $value->callsign );
  18. echo '<br>';
  19. echo ( $value->time_start );
  20. echo '<br>';
  21. echo ( $value->time_end );
  22. echo '<br>';
  23.  }
  24.  
  25. ?>


bo ten kod generuję serię błędów niestety.

EDIT:

EUREKA! W końcu coś samemu mi wyszło:

zamiast

  1. <?php
  2. $xml = new SimpleXMLElement($xmlstr);
  3. ?>


dałem

  1. <?php
  2. $xml = simplexml_load_file('http://vatbook.euroutepro.com/xml2.php?fir=EPWW');
  3. ?>


I ostatnie pytanie na koniec:

czy można sformatować datę wyciągniętą z powyższego linku by została tylko H:i ?

EDIT2:

Znowu sam sobie odpowiem winksmiley.jpg :

  1. <?php
  2. $a= date("H-i", strtotime($value->time_start));
  3. $b= date("H-i", strtotime($value->time_end));
  4. ?>


rozwiązało problem.

Wspaniały dzień, bo nie zmarnowany, dzięki.



Rozbudowuję skrypt i mam kolejny problem.

Wprowadzam dane z powyższego pliku XMLa do tabeli SQL skryptem:

  1. <?php
  2. $update_atc = "UPDATE book SET atc='$value->callsign', data = '$c1', od = '$a1', do = '$b1', nazwisko = '$value->name' where atc = '$value->callsign' ";
  3. ?>

(gdzie $c1 itd. o sformatowane do moich potrzeb daty)

gdzie mam rekordy dla każdej instancji EPWW_TWR, EPWW_APP, EPWW_GDN i tak dalej, wszystkiego razem 31 sztuk.

Dane się aktualizują pięknie, ale problem w tym, że aktualizują się do najwyższej daty, czyli np. 29.05 , a ja chciałbym odwrotnie, wyłącznie dzisiejeszą, najwcześniejszą.

Ehh...

  1. <?php
  2. $query = "SELECT * FROM book";
  3. $result = mysql_query($query);
  4.  
  5. $number = mysql_numrows($result);
  6.  
  7.  
  8.   for ($i=0; $i<$number; $i++) {
  9.          
  10.      $dzis = mysql_result($result,$i,"dzis");  
  11.  
  12. $d = date(strtotime($dzis));
  13. $d1 = date("Y-m-d" , $d);
  14.  
  15.  
  16. }
  17.  foreach ( $xml->atcs->booking as $key => $value )  
  18.  {
  19.  
  20.  
  21. $a= date(strtotime($value->time_start));
  22. $c1= date("Y-m-d" , $a);
  23. $b= date(strtotime($value->time_end));
  24. $a1= date("H-i",$a+7200);
  25. $b1= date("H-i",$b+7200);
  26.  
  27.  
  28. $update_atc = "UPDATE book SET atc='$value->callsign', data = '$c1', od = '$a1', do = '$b1', nazwisko = '$value->name' where (atc = '$value->callsign' and '$c1' = '$d1') limit 1 ";
  29.  
  30.  
  31. $result = mysql_query($update_atc);
  32. ?>


gdzie dziś to current_timestamp.

Działa.

Ten post edytował slupek 9.05.2009, 12:32:36
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 27.04.2024 - 09:10