Niestety jestem początkujący i nie potrafię sobie poradzić z pewnym problem, który mnie nęka już od kilku dni. Na wstępie chciałbym zaznaczyć, że jestem po paru godzinach prób i błędów oraz szukania w manualach...
Otóż mam 2 pliki:
<?xml version="1.0" encoding="ISO-8859-2"?> <offers> <offer> <id>1</id> <price>1000</price> <attributes> <attribute> <name>Gatunek</name> <value>Ssak</value> </attribute> <attribute> <name>Nazwa</name> <value>Słoń</value> </attribute> </attributes> </offer> <offer> <id>2</id> <price>200</price> <attributes> <attribute> <name>Gatunek</name> <value>Płaz</value> </attribute> <attribute> <name>Nazwa</name> <value>Ropuszka pomarańczowa</value> </attribute> </attributes> </offer> </offers>
<?php class offer{ var $id; var $cena; var $kod; } $int = 0; $czy_offer = 0; function tag_start($parser, $attr, $params){ if($attr == 'OFFER' && $czy_offer == 1){ '); }elseif($attr == 'OFFER' && $czy_offer == 0){ $offers[$int] = new offer($params['ID']); $czy_offer = 1; } if($czy_offer == 1){ if($attr != 'PERSONALIA'){ $act_tag = $attr; }else{ $offers[$int] -> imie = $params['IMIE']; // to jest przeróbka przykładu z www - nie wiem, czemu po zahashowaniujest błąd w wyświetlaniu danych $offers[$int] -> nazwisko = $params['NAZWISKO']; // tu tak samo } } } function tag_text($parser, $text){ if($czy_offer == 1){ switch($act_tag){ case 'ID': $offers[$int] -> id.= $text; break; case 'PRICE': $offers[$int] -> cena.= $text; break; case 'VALUE': $offers[$int] -> kod.= $text; break; } } } function tag_end($parser, $attr){ if($attr == 'OFFER' && $czy_offer == 1){ $int++; $czy_offer = 0; } } $parser = xml_parser_create(); xml_set_element_handler($parser, 'tag_start', 'tag_end'); xml_set_character_data_handler($parser, 'tag_text'); } xml_error_string(xml_get_error_code($_parser)), xml_get_current_line_number($parser))); } } xml_parser_free($parser); foreach($offers as $offer){ echo ' Id: '.$offer -> id.' Cena: '.$offer -> cena.' Kod: '.$offer -> kod.' '; } ?>
Wyświetla:
Id: 1
Cena: 1000
Kod: Ssak Słoń
Id: 2
Cena: 200
Kod: Płaz Ropuszka pomarańczowa
Moim problemem jest wyświetlanie tylko pierwszej cześci "Kodu" - Ssak, Płaz...
Chciałbym, żeby się wyświetlało:
Id: 1
Cena: 1000
Kod: Ssak
Id: 2
Cena: 200
Kod: Płaz
Czy ktoś jest mi w stanie pomóc?