【问题标题】:Php class parse xml to html form like joomlaphp 类将 xml 解析为 html 形式,如 joomla
【发布时间】:2014-06-08 21:21:32
【问题描述】:

任何人都知道 php 类将 xml 解析为 html 形式,如 joomla

Xml 写入

<field id="city" name="title" type="City" label="Choice you city"
    description="this is city field"
    required="true" />

html 输出

Choice you City
<select name="title" id="city">
      <option>....</option>
      ..................

</select>

【问题讨论】:

  • 如果 Joomla 可以做到这一点,为什么不直接从 Joomla 获取代码?

标签: php html xml joomla


【解决方案1】:

正如 Gordon 所说 - 为什么不使用 Joomla 方法呢?

但是,我不知道 Joomla 是如何实现它的,也不知道每个选项条目的 value 属性或数据来自哪里,但是下面的示例应该会给您一个想法。数据被设置为 XML 节点“字段”的内容。

$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<form>
<field id="city" name="title" type="select" label="Choice you city" description="this is city field" required="true">City1,City2,City3</field>
<field id="postcode" name="title" type="text" label="Choice your Postcode" description="this is postcode field" required="false" />
</form>
XML;

$form = new SimpleXMLElement ( $xmlstr );


foreach ( $form->children () as $field ) 
{
    $required = ( $field['required'] == 'true' ) ? 'required' : null;
    $label = ( $field['label'] ) ? $field['label'] : null;

    switch( $field['type'] ) {
        case 'select':

            if (isset ( $field ) && $field != '') 
            {
                echo $label;
                echo '<select name="'.$field['name'].'" id="'.$field['id'].'" '.$required.'>';

                $data = explode ( ',', $field );
                foreach ( $data as $value )
                {
                    echo '<option value="'.$value.'">'.$value.'</option>';
                }
                echo "</select>";
            }
        break;

        case 'text':
            echo $label;
            echo '<input type="text" name="'.$field['name'].'" id="'.$field['id'].'" '.$required.' />';
        break;
    }
}

输出:

Choice you city
<select name="title" id="city" required><option value="City1">City1</option>
  <option value="City2">City2</option>
  <option value="City3">City3</option>
</select>

Choice your Postcode
<input type="text" name="title" id="postcode"  />

Sandbox 中的示例

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2018-06-14
    • 1970-01-01
    • 2012-04-10
    • 2016-08-05
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    相关资源
    最近更新 更多