MikeDXUNL Posted June 13, 2009 Share Posted June 13, 2009 My script runs like this: <?php $parents = 'overview,brands,specs'; $parents = explode(",", $parents); if (file_exists($filename)) { $xml = simplexml_load_file($filename, 'SimpleXMLElement', LIBXML_NOCDATA); } else { exit('Failed to open test.xml.'); } echo '<form name="editXML" method="POST" action="#">'; foreach($parents as $parent) { foreach($xml->$parent->children() as $name => $node) { if(trim($node) == "") { foreach($xml->$parent->$name->children() as $child => $body) { echo '<b>'.$parent.' - '.$name.' - '.$child.'</b><br />'; echo "\n"; echo '<textarea name="'.$parent.'-'.$name.'-'.$child.'" style="width: 500px; height: 200px;">'.trim($body).'</textarea><br /><br />'; echo "\n"; } } else { echo '<b>'.$parent.' - '.$name.'</b><br />'; echo "\n"; echo '<textarea name="'.$parent.'-'.$name.'" style="width: 500px; height: 200px;">'.trim($node).'</textarea><br /><br />'; echo "\n"; } } } echo '<input type="submit" name="editXML" />'; echo '</form>'; ?> The problem lies with: foreach($xml->$parent->children() as $name => $node) { Not every .xml file has "overview, brands, specs" .. in this case, this file is missing "brands" is there a way I can argue if on of the $parents does not exist? Thanks in advance for help. Link to comment https://forums.phpfreaks.com/topic/162076-solved-foreach-problem/ Share on other sites More sharing options...
jacksonmj Posted June 13, 2009 Share Posted June 13, 2009 Might the following work? foreach($parents as $parent) { if (!$xml->$parent || ($xml->$parent == '') ) continue; foreach($xml->$parent->children() as $name => $node) { Link to comment https://forums.phpfreaks.com/topic/162076-solved-foreach-problem/#findComment-855250 Share on other sites More sharing options...
MikeDXUNL Posted June 13, 2009 Author Share Posted June 13, 2009 BEAUTIFUL! Thanks a ton man! Link to comment https://forums.phpfreaks.com/topic/162076-solved-foreach-problem/#findComment-855264 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.