Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何仅渲染一次字段集和图例标记(对于3个单选按钮输出)_Php_Html_Forms_Oop_Radio Button - Fatal编程技术网

Php 如何仅渲染一次字段集和图例标记(对于3个单选按钮输出)

Php 如何仅渲染一次字段集和图例标记(对于3个单选按钮输出),php,html,forms,oop,radio-button,Php,Html,Forms,Oop,Radio Button,我有一个配置php文件,其中我为3个单选按钮设置了一个带有属性的数组。在一个单选类中,我有一个名为“render”的方法,它应该输出单选按钮。单选按钮应放置在html字段集标签内。 问题是,我得到了每个单选按钮的字段集和图例标记。但我希望单选按钮嵌入一个字段集和一个图例中。我卡住了 这是阵列配置: <?php $formConf = [ 'kraken' => [ 'type' => 'radio', 'id' => 'kraken', 'nam

我有一个配置php文件,其中我为3个单选按钮设置了一个带有属性的数组。在一个单选类中,我有一个名为“render”的方法,它应该输出单选按钮。单选按钮应放置在html字段集标签内。 问题是,我得到了每个单选按钮的字段集和图例标记。但我希望单选按钮嵌入一个字段集和一个图例中。我卡住了

这是阵列配置:

<?php
 $formConf = [
'kraken' => [
    'type' => 'radio',
    'id' => 'kraken',
    'name' => 'monster',
    'label' => 'Kraken',
    'radio' => 'checked'
],

'sasquatch' => [
    'type' => 'radio',
    'id' => 'sasquatch',
    'name' => 'monster',
    'label' => 'Sasquatch',
    'radio' => ''
],

'mothman' => [
    'type' => 'radio',
    'id' => 'mothman',
    'name' => 'monster',
    'label' => 'Mothman',
    'radio' => ''
]];
此方法应生成html标记:

public function render() : string
{        
    $out = '';
    $out .= '<fieldset>';
    $out .= '<legend>Choose your monster</legend>';
    $out .= '<input type="' . $this->type . '"';
    $out .= $this->renderRadioChecked();
    $out .= '<label for="' . $this->id . '"' . '>';
    $out .= $this->label . '</label>';
    $out .= '<br/>';
    $out .= '</fieldset>';

    return $out;        
}

protected function renderRadioChecked() 
{
    $out = '';
    if ($this->radio == '') {
    $out .= 'id="' . $this->id . '"' . 'name="' . $this->name . '"' . '>';
        } else {
            $out .= 'id="' . $this->id . '"' . 'name="' . $this->name . '"' . ' checked' . '>';
        }
        return $out;
    }
公共函数render():字符串
{        
$out='';
$out.='';
$out.='选择你的怪物';
$out.='';
$out.=$this->label'';
$out.='
'; $out.=''; 退回$out; } 受保护函数renderRadioChecked() { $out=''; 如果($this->radio==''){ $out.='id=“”。$this->id。“”。'name=“”。$this->name。“”。“>”; }否则{ $out.='id=“”。$this->id。“”。'name=“”。$this->name。“”。'checked'.>; } 退回$out; }
预期结果应为:

<fieldset>
<legend>Choose your favorite monster</legend>

<input type="radio" id="kraken" name="monster">
<label for="kraken">Kraken</label><br/>

<input type="radio" id="sasquatch" name="monster" checked>
<label for="sasquatch">Sasquatch</label><br/>

<input type="radio" id="mothman" name="monster">
<label for="mothman">Mothman</label>
</fieldset>

选择你最喜欢的怪物
海怪
萨斯夸奇
天蛾人
带有单选按钮的完整版本可在此处看到:

注:
我在索引页面上已经有了一个表单标签和其他html标签。这就是为什么我只能生成这里给出的字段集。

我会删除字段集html,因为它不是单选按钮元素。我可能会考虑上一堂形式课,然后把收音机插入其中。因此,要获得多个单选按钮,在删除
字段集
html后,循环单选按钮类,或者您可以允许单选按钮类一次创建多个单选按钮:

class Radio extends Input
{
    protected $data;

    public function __construct(array $opts)
    {
        parent::__construct($opts);
        # The type is irrelevant since this class is a radio, the type is already known.
        # I would just assign the array to a storage variable
        if(empty($opts)) {
            $this->data = false;
        }
        else {
            # I would make sure the array is going to be consistent
            $this->data = (isset($opts['radio']))? [$opts] : $opts;
        } 
    }

    public function render() : string
    {
        # May as well stop here if empty
        if(empty($this->data))
            return false;
        # Loop your data array
        foreach($this->data as $radio) {
            # Build the input
            $out[] = '<input type="radio" '.$this->createAttrElem($radio).' />';
            # I would check to see if the label is set (you never know)
            if(!empty($radio['label']))
                $out[] = '<label for="'.$radio['id'].'">'.$radio['label'].'</label>';
        }
        # Send back the string
        return implode(PHP_EOL, $out);
    }

    protected function createAttrElem($radio) 
    {
        # See if all these attributes are set first
        if(!empty($radio['id']))
            $attr[] = 'id="'.$radio['id'].'"';
        if(!empty($radio['name']))
            $attr[] = 'name="'.$radio['name'].'"';
        if(!empty($radio['checked']))
            $attr[] = 'checked';
        # Return a string
        return (isset($attr))? implode(' ', $attr) : '';
    }
    /**
     *    @description    I might maybe make this method just run the render method.
     *                    Saves on having to write it out.
     */
    public  function __toString()
    {
        return $this->render();
    }
}
这将产生:

<fieldset name="test">
<input type="radio" id="kraken" name="monster" />
<label for="kraken">Kraken</label>
<input type="radio" id="sasquatch" name="monster" />
<label for="sasquatch">Sasquatch</label>
<input type="radio" id="mothman" name="monster" />
<label for="mothman">Mothman</label>
</fieldset>

海怪
鼠尾草
天蛾人
class Form
{
    protected   $layout,
                $type;
    /**
     *    @description    I would allow the form class to be flexible but set default to "form"
     */
    public  function    __construct($type = 'form')
    {
        $this->type     = $type;
    }
    /**
     *    @description    Create the open tag
     */
    public function open(array $attr)
    {
        $open[] = '<'.$this->type;
        foreach($attr as $key => $value) {
            $open[] = $key.'="'.$value.'"';
        }
        $this->layout[] = implode(' ', $open).'>';
        return $this;
    }
    /**
     *    @description    Here you would add the children of the parent element
     */
    public function addChild($Object)
    {
        $this->layout[] = $Object->render();
        return $this;
    }
    /**
     *    @description    Close the element
     */
    public function close()
    {
        $this->layout[] = '</'.$this->type.'>';
        return $this;
    }
    /**
     *    @description    Create a string of the layout
     */
    public function render()
    {
        return implode(PHP_EOL, $this->layout);
    }
    /**
     *    @description    Shortcut the render process
     **/
    public  function __toString()
    {
        return $this->render();
    }
}
$formConf = [
    'kraken' => [
        'type' => 'radio',
        'id' => 'kraken',
        'name' => 'monster',
        'label' => 'Kraken',
        'radio' => 'checked'
    ],

    'sasquatch' => [
        'type' => 'radio',
        'id' => 'sasquatch',
        'name' => 'monster',
        'label' => 'Sasquatch',
        'radio' => ''
    ],

    'mothman' => [
        'type' => 'radio',
        'id' => 'mothman',
        'name' => 'monster',
        'label' => 'Mothman',
        'radio' => ''
    ]
];

$Fieldset   =   new Form('fieldset');

echo $Fieldset->open(['name' => 'test'])
    ->addChild(new Radio($formConf))
    ->close();
<fieldset name="test">
<input type="radio" id="kraken" name="monster" />
<label for="kraken">Kraken</label>
<input type="radio" id="sasquatch" name="monster" />
<label for="sasquatch">Sasquatch</label>
<input type="radio" id="mothman" name="monster" />
<label for="mothman">Mothman</label>
</fieldset>