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. Quote 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) { Quote 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! Quote Link to comment https://forums.phpfreaks.com/topic/162076-solved-foreach-problem/#findComment-855264 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.