Jump to content

Help editing an XML file from an array


adbertram

Recommended Posts

I've been working with PHP for only a few weeks now.  I have learned a significant amount by creating a custom web app.  I can usually figure things out by massive Googling and trial and error but this one has got me stumped.

 

I have a form for this web app that I'd like to use as a configuration page.  It houses all of the settings that the web app needs.  On the backend, I'm housing all of this data in a XML file.  So far, I've been able to parse the XML file to populate all of the appropriate fields in the HTML form.  However, I'm having trouble taking the user's input and saving it back to the same XML.

 

To do this, I have a processconfig.php page as the form's action.  In this process.php page, I'm using the elements from the $_POST array to bring over all of the data in the form's fields.  I want to take these $_POST array elements and insert them into the same XML that originally populated the form in the first place.

 

With the code below I've been able to get the SimpleXML object to add data to the XML file but it is simply appended to the end with no formatting.  As you can see, I've tried to name the form fields the same name as it's POST array element so that I can do the loop but it's not working.

 

Is there an easier way to do this or is there another recommended way to retrieve and store configuration items for a web app?

 

Here's my code:

 

config.xml snippet

 


<Config>
<General>
	<Logging>
		<Enabled>No</Enabled>
	</Logging>
	<QueryBuybackSites>
		<Enabled>Yes</Enabled>
	</QueryBuybackSites>
	<BuildFBA>
		<Enabled>No</Enabled>
	</BuildFBA>
	<BuildBB>
		<Enabled>No</Enabled>
	</BuildBB>
	<FBARankCutoff>
		<Value>1000000</Value>
	</FBARankCutoff>
	<Amazon0Offer>
		<DefaultPrice>
			<Value>47.77</Value>
		</DefaultPrice>
		<AutoKeep>
			<Enabled>Yes</Enabled>
		</AutoKeep>
	</Amazon0Offer>
</General>
</Config>


 

configview.php snippet

 


<?php
//Load all configuration data
$configxml = simplexml_load_file('config.xml');

echo '<html>
<head>
	<title>Easy Receive Configuration</title>
</head>
<body>
	<form action="processconfig.php" enctype="multipart/form-data" id="emf-form" method="post">
		<table bgcolor="#FFFFFF" border="0" cellpadding="2" cellspacing="0" style="text-align: left;">
			<tbody>
				<tr>
					<td colspan="2" style="">
						<font color="#000000" face="Verdana" size="2"><b style="font-size: 20px;">Easy Receive Configuration Settings</b><br />
						<label style="font-size: 15px;">This is where you can customize Easy Receive to your personal preferences</label></font>
					</td>
				</tr>
				<tr valign="top">
					<td colspan="2" style="">
						<hr />
						<h3 id="element_0" name="element_0" style="font-size: 14px; padding: 10px 0pt 6px; margin: 14px 0pt; position: static;">
							<span style="font-size: 24px;"><font color="#000000" face="Verdana">General Configuration Options</font></span></h3>
					</td>
				</tr>
				<tr valign="top">
					<td align="" style="">
						<p>
							<strong>Enable Logging</strong> 
							<input name="General->Logging->Enabled" type="checkbox" ' . htmlify($configxml->General->Logging->Enabled) . ' /></p>
					</td>
				</tr>
				<tr valign="top">
					<td align="" style="">
						<p>
							<font color="#000000" face="Verdana" size="2"><strong>Build FBA shipment on the fly</strong>
							<input name="General->BuildFBA->Enabled" type="checkbox" ' . htmlify($configxml->General->BuildFBA->Enabled) . ' /></font></p>
					</td>
				</tr>

</tbody>
		</table>
	</form></body>
</html>';

function htmlify($input)
{
	if ($input == "Yes") 
		return "checked";
	else return "";
}
?>

 

processconfig.php snippet

 

<?php
$xmlstring = file_get_contents('config.xml');
$configxml = new SimpleXMLElement($xmlstring);

foreach ($_POST as $attrib=>$value){
             $configxml->$attrib = $value;
}
$configxml->asXML('config.xml');
?>

Link to comment
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.