$doc = new DOMDocument();
$doc->load( 'model.xml' );
$fields = $doc->getElementsByTagName( "field" );
$i = 0 ;
foreach($fields as $field)
{
$name = $field->getAttribute('name') ;
$type = $field->getAttribute('type') ;
/* constraints */
$con = $field->getElementsByTagName('constraints') ;
foreach($con as $c)
{
$min_length = $c->getElementsByTagName('min_length')->item(0)->nodeValue ;
$max_length = $c->getElementsByTagName('max_length')->item(0)->nodeValue ;
if ($c->getElementsByTagName('required')->item(0))
{
$required = 1 ;
}
}
//check input types
if($type == 'text' || $type == 'password')
{
echo $field->getElementsByTagName('label')->item(0)->nodeValue.' : ' ;
//create input tag
echo '<input type="'.$type.'" name="'.$name.'" min_length="'.$min_length.'" max_length="'.$min_length.'"' ;
//add required field to input
($required == 1) ? print('class="required" /><span style="color:red">*</span><br />') : ('/><br />');
}elseif($type == 'radio'){
echo $field->getElementsByTagName('label')->item(0)->nodeValue.' : <br /> ' ;
$items = $field->getElementsByTagName('item') ;
foreach($items as $item)
{
$labels[] = $item->getElementsByTagName('label')->item(0)->nodeValue ;
$values[] = $item->getElementsByTagName('value')->item(0)->nodeValue ;
}
for($i=0 ; $i<=(count($labels)-1);$i++)
{
//create inputs
echo $labels[$i].' : <input type="radio" name="'.$name.'" value="'.$values[$i].'" ' ;
($required == 1 ) ? print('checked="checked" /><span style="color:red">*</span><br />') : ('/><br />');
}
}elseif($type == 'select'){
echo $field->getElementsByTagName('label')->item(0)->nodeValue.' : <br /> ' ;
$items = $field->getElementsByTagName('item') ;
foreach($items as $item)
{
$labels[] = $item->getElementsByTagName('label')->item(0)->nodeValue ;
$values[] = $item->getElementsByTagName('value')->item(0)->nodeValue ;
}
echo '<select name="'.$name.'">' ;
for($i=0 ; $i<=(count($labels)-1);$i++)
{
//create inputs
echo '<option value="'.$values[$i].'" >'.$labels[$i].'</option>' ;
}
echo '</select>';
}
这是我的xml:
<?xml version="1.0" encoding="utf-8" ?>
<fields>
<field name="first_name" type="text">
<label>First Name</label>
<constraints>
<required/>
<min_length>1</min_length>
<max_length>255</max_length>
</constraints>
</field>
<field name="password" type="password">
<label>Password</label>
<constraints>
<required/>
<min_length>6</min_length>
<max_length>8</max_length>
</constraints>
</field>
<field name="age" type="text">
<label>Age</label>
<constraints>
<min>1</min>
<max>99</max>
</constraints>
</field>
<field name="gender" type="radio">
<label>Gender</label>
<item>
<label>Male</label>
<value>1</value>
</item>
<item>
<label>Female</label>
<value>2</value>
</item>
<constraints>
<required/>
</constraints>
</field>
<field name="language" type="select">
<label>Language(s)</label>
<item>
<label>English</label>
<value>1</value>
</item>
<item>
<label>French</label>
<value>2</value>
</item>
<item>
<label>Persian</label>
<value>4</value>
</item>
<constraints/>
</field>
</fields>
如果您运行此代码,您可以在选择的HTML视图中看到无线电项重复如何避免这种情况发生。
答案 0 :(得分:1)
你没有忘记$ labels&amp; $ values,所以一旦循环完成,它仍然会记住之前的值,在foreach的顶部添加类似下面的内容($ fields ... loop。
$labels = null;
$values = null;