im not too up on the whole xml / php parser type stuff but cobbled this together to parse some XML fro ma variable ($xml)
PHP Code:
<?
$xml = "all my xml data here - it comes from a database";
$parser = xml_parser_create( );
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse_into_struct($parser,$xml,$values,$index);
xml_parser_free($parser);
for($i = 0; $i <= 200; $i++) {
$desc = $values[$index[desc][$i]][value];
$title = $values[$index[title][$i]][value];
$url = $values[$index[infourl][$i]][value];
// $type = $values[$index[type][$i]][value];
if ($title): ?>
I can then print out my content here
<? echo $desc; ?> etc etc from above.
<?
endif;
}
?>
Quick explanation behind it, the whole $i up to 200 thing, its because I know there wont ever be more than 200 items, the if($title) makes sure I dont echo blank entries onto the page.
I dont actually know what:
PHP Code:
$parser = xml_parser_create( );
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse_into_struct($parser,$xml,$values,$index);
xml_parser_free($parser);
does so if anyone fancied explaining that it'd be nice!
the problem...
the $type (commented out i nthe above script) messes it all up. Some of the xml entries have a <type>something</type> but others dont have any <type> tags at all. So when the script runs through it it seems to move the types up. So if the 12th item has a type of say "green" it moves it up and the first item shows as having a type "green" and if no other tpyes are i nthe xml then item 12 whic hshould be green has no type with it.
kind of complicated, I know how to fix it but dont know how to FIX it.
I need the script to check each section of data for the tag and if it doesnt exist leave it blank or something like that?
sample xml data for this...
Code:
<xmldata>
<data>
<desc></desc>
<url></url>
<type></type> (not always there)
</data>
<data>
<desc></desc>
<url></url>
(see gone)
</data>
</xmldata>
sorry if ive made this a bit lengthy but would be ever so greatful if anyone could help!
