Jump to content

Recommended Posts

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.