Don't know if this will work but have you tried
Code:if ($values[$index[type][$i]][value]){ $type = $values[$index[type][$i]][value]; }
im not too up on the whole xml / php parser type stuff but cobbled this together to parse some XML fro ma variable ($xml)
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.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;
}
?>
I dont actually know what:
does so if anyone fancied explaining that it'd be nice!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);
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...
sorry if ive made this a bit lengthy but would be ever so greatful if anyone could help!Code:<xmldata> <data> <desc></desc> <url></url> <type></type> (not always there) </data> <data> <desc></desc> <url></url> (see gone) </data> </xmldata>![]()
![]()
Dan Morley
alpharooms.com
daniel at alpharooms dot com - Hotels, Flights, Airport Transfers, Care Hire + More! sign up
My Blog | Cheap Holidays
Don't know if this will work but have you tried
Code:if ($values[$index[type][$i]][value]){ $type = $values[$index[type][$i]][value]; }
Never argue with idiots. They just drag you down to their level and then beat you with their experience.
If ignorance is bliss then some of the people I know must be orgasmic.
ill give it a shot cheers, but I have a sneeky suspicion that it wont...
what I thin kthe script is doing is just going throug hall the data until it finds the type tag and using it, doesnt matter which <data></data> it comes from, so...
would be true, as it finds a type tag somewhere i nthe data and then it would use that againPHP Code:if ($values[$index[type][$i]][value])
might be wrong like I said ill try it but thats my sneeking suspicion
edit: cant check it now, changed some stuff earlier and dont have access from home to get them back. grrr ill be back tomorrow no doubt!
Dan Morley
alpharooms.com
daniel at alpharooms dot com - Hotels, Flights, Airport Transfers, Care Hire + More! sign up
My Blog | Cheap Holidays
Hi Dan,
When I first started using PHP's XML functions a few years ago I had a look at parse_into_struct and my head nearly blew up!
I tend to use the event handler method of parsing; as you have complete control over the data structure that you are creating. Here's something that should do what you want - a quick tested knock up! (hooray for "php -a" !!)
You could then go on to traverse the $records array using foreach, and the print out each of the expected parameters after testing to see if they are set, just as you are doing now:PHP Code:<?php
// this is your sample XML with some fields missing in each record
$xml = "
<xmldata>
<data><desc>Foo</desc><url>someurl</url><type>TEST</type></data>
<data><desc>Bar</desc><url>someotherurl</url></data>
</xmldata>
";
// start an array to parse the records into
$records = array();
// create a function to handle the opening tags in the XML
function xmlStartTag($parser, $name, $attribs)
{
global $currentRecord;
global $currentTag;
global $inRecord;
if ($name == "DATA") // each of your records starts with <data> so start a new $currentRecord
{
$inRecord = true;
$currentRecord = array();
}
if ($inRecord)
{
$currentTag = $name;
}
}
// create a function to handle the data between tags
function xmlCDATA($parser, $cdata)
{
global $currentRecord;
global $currentTag;
$currentRecord[$currentTag] .= $cdata;
}
// create a function to handle the closing tags in the XML
function xmlEndTag($parser, $name)
{
global $records;
global $currentRecord;
global $inRecord;
if ($name == "DATA") // ditto, this means we've reached the end, so add the current record to the $record array :)
{
$inRecord = false;
$records[] = $currentRecord;
}
}
// create an XML parser resource
$parser = xml_parser_create();
// register the functions to handle the beginning and end of each tag
xml_set_element_handler($parser, "xmlStartTag", "xmlEndTag");
// register a function to handle the data between tags (cdata)
xml_set_character_data_handler($parser, "xmlCDATA");
// parse your string, the true value means there's no more data to come
xml_parse($parser,$xml,true);
// free it up
xml_parser_free($parser);
// now you've parsed the records into $records have a look using print_r()
print_r($records);
?>
Hope this helps! (i've lived and breathed XML for the past 2 years as you know!PHP Code:foreach($records as $data)
{
print $data["DESC"]."<br>";
print $data["URL"]."<br>";
if ($data["TYPE"]) print $data["TYPE"]."<br>";
}
)
Developer of the Price Tapestry Price Comparison Script now with full WordPress Plugin!
thank you loads!![]()
Dan Morley
alpharooms.com
daniel at alpharooms dot com - Hotels, Flights, Airport Transfers, Care Hire + More! sign up
My Blog | Cheap Holidays
I think the problem is that $type is set from the previous run through the loop.Originally Posted by kbudden
Try this - should set $type to nothing if it doesn't exist for this item.
Code:if ($values[$index[type][$i]][value]){ $type = $values[$index[type][$i]][value]; } else { $type=""; }
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks