Jump to content

[SOLVED] Foreach problem.


MikeDXUNL

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.