Transformation between XML & Form -PHP
my code is working for a certain level of xml but my logic needs to get modified as i want it to work for everytype of xml file whatever element i add in the xml is readable and can be edited . Please amend my code edit.php
This Code Is reading xml file Nd generating a form attached
<!DOCTYPE html>
<html>
<head> INSERT VALUES OF THE GIVEN PHRASE IDS :</head>
<!-- <head> <link rel="stylesheet" type="text/css" href="style.css" /></head> -->
<body>
<form name="form" action="edit.php" method="post">
<?php
$file = <<<XML
<?xml version="1.0"?>
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
<product description="Cardigan Sweater" id="123" value="" product_image="cardigan.jpg">
<catalog_item gender="Men's">
<size description="Medium">
<color_swatch image="red_cardigan.jpg" id="color" value="Red"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="burgundy"/>
</size>
<size description="Large">
<color_swatch image="red_cardigan.jpg" id="color" value="Red"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="burgundy"/>
</size>
</catalog_item>
</product>
</catalog>
XML;
/**
* @param SimpleXMLElement $node current node to process
* @param int $level Depth in the XML tree
* @param string $fieldPrefix
*/
function makeNodeField(SimpleXMLElement $node, $level, $fieldPrefix = "")
{
// Indentation with the node level
$indent = str_repeat(" ", $level);
echo $indent, $node->getName(), PHP_EOL;echo "<br/>";
$fieldName = $fieldPrefix;
// If there's an ID on the node, display it and show a text field
if (isset($node['id'])) {
echo $indent, strval($node['id']), ' ';
if (!empty($fieldPrefix)) {
$fieldName .= '__';
}
$fieldName .= $node->getName() . '__' . strval($node['id']);
printf('<input type="text" name="%s" value="%s" /><br/>', $fieldName, strval($node['value']));echo "<br/>";
echo PHP_EOL;
}
$count = 0;
foreach ($node as $childNode) {
// put a prefix in the children field name to get a valid field name for POST
$childPrefix = $fieldName;
if (!empty($fieldPrefix)) {
$childPrefix = $fieldName . '__' . $count++;
}
makeNodeField($childNode, $level + 1, $childPrefix);
}
}
$xml = simplexml_load_string($file);
makeNodeField($xml, 0);
?>
<input type='submit' name='submit'>
</form>
</body>
</html>
it creates a form like this (attached snapshot)
edit.php ()
<?php
if(isset($_POST['submit'])){
function printxml ($array)
{
$xml = new DOMDocument();
$xpath = new DOMXPath($xml);
$xml->formatOutput = true;
$last_index_ids = array();
$i_id=1;
foreach ($array as $key => $value)
{
$key = $key;
$attrvalue= $value;
//split unique input field names
if ($array[$key] == "" || ctype_space($array[$key]) ) {
$array[$key] = null;
}
$expkey = explode("__", $key);
echo "<br/>";
//ids of phrase
array_push($last_index_ids,end($expkey));
echo "<pre>";print_r($expkey);echo "</pre>";
$first_key =($expkey[0]);
//first root element
if ($xml->getElementsByTagName($first_key)->length == 0 && $first_key!='submit') {
$xml_element = $xml-> createElement($first_key,"");
$xml->appendChild($xml_element);
}
foreach($expkey as $key=>$value) {
if(is_numeric($value)) {
$secondtag = $expkey[$key+1];
//secondtag
$xml_tag = $xml->createElement($secondtag,"");
//attributes
$domAttribute = $xml->createAttribute('id');
$domAttribute->value = $last_index_ids[$i_id];
$domAttribute1 = $xml->createAttribute('value');
$domAttribute1->value = $attrvalue ;
$xml_tag->appendChild($domAttribute);
$xml_tag->appendChild($domAttribute1);
$xml_element->appendChild($xml_tag);
$i_id++;
}
}
}
$xml->save("rozeena.xml");
}
printxml($_POST);
}exit();
?>
now this edit.php is working for this xml file
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<gs-shop date="2015/4/23" version="10.0.3" id="gs" name="Garments_Shop">
<catalog>
<color_swatch image="red_cardigan.jpg" id="color" value="Red"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="burgundy"/>
<color_swatch image="red_cardigan.jpg" id="color" value="Red"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="burgundy"/>
</catalog>
</gs-shop>
how to modify this so it can work for this
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
<product description="Cardigan Sweater" id="123" value="" product_image="cardigan.jpg">
<catalog_item gender="Men's">
<size description="Medium">
<color_swatch image="red_cardigan.jpg" id="color" value="blue"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="blue"/>
</size>
<size description="Large">
<color_swatch image="red_cardigan.jpg" id="color" value="Blue"/>
<color_swatch image="burgundy_cardigan.jpg" id="color" value="blue"/>
</size>
</catalog_item>
</product>
</catalog>
and whatever i add to xml.
for Reference Example : http://www.datamech.com/XMLForm/index.html
Answer
Solution:
The
Notice
message already says it:fieldName
is not defined. This means, you usefieldName
, but you haven't defined it. You must assign a value first.Depending on the field you want to process, this must be one of the values shown in the output, e.g.
product_123
product_123_0_0_color_swatch_color
product_123_0_1_color_swatch_color
product_123_1_0_color_swatch_color
product_123_1_1_color_swatch_color
To restore the XML from the input fields, you must parse and split the index values from the
$_POST
variable. Although from what you have shown, this is not possible, because<product description="...">
,<catalog_item gender="...">
or<size description="...">
is missing, for example.