RadioButton Group打印标题

时间:2016-10-14 03:16:35

标签: php

我尝试做的就是打印一个单选按钮组以及该组的标题。我想出了如何返回并将组打印为数组,但是如何返回标题,因为我只能返回单选按钮组项的数组?当我创建一个新的RadioButton项目时分配标题,即"您经常访问XYZ Widget Emporium的频率是多少?"打印组标题的代码将在generateHTML()方法中。

XYZSurvey.php

<?php

require_once 'Form.php';
require_once 'RadioButton.php';
require_once 'TextInput.php';
require_once 'Validator.php';

echo "XYZ Wonder Emporium Survey";

$form = new Form();

$form -> addInput(new TextInput("First Name: ", "first_name"));
$form -> addInput(new TextInput("Last Name: ", "last_name"));
$form -> addInput(new TextInput("Age: ", "ice_cream_flavor"));
$radio1 = new RadioButton("Gender:", "gender");
$radio1 -> addOption("Male", "male");
$radio1 -> addOption("Female", "female");
$form -> addInput($radio1);
$form -> addInput(new TextInput("Email: ", "email_address"));

$radio2 = new RadioButton("How often do you typically visit XYZ Widget Emporium?");
$radio2 -> addOption("Almost Never", "visit");
$radio2 -> addOption("Once a year", "visit");
$radio2 -> addOption("Once a month", "visit");
$radio2 -> addOption("Weekly", "visit");
$radio2 -> addOption("Daily", "visit");
$form -> addInput($radio2);
$form -> addInput(new TextInput("How did you hear about us? ", "hear_about_us"));
$radio3 = new RadioButton("Have you ever contacted customer service?");
$radio3 -> addOption("Yes", "customer_service");
$radio3 -> addOption("No", "customer_service");
$form -> addInput($radio3);

$form -> addInput(new TextInput("What did we do well? ", "well"));
$form -> addInput(new TextInput("What could we improve? ", "improve"));
$form -> addInput(new TextInput("Would you recommend XYZ Widget Emporium? ", "recommend"));
$form -> addInput(new TextInput("Additional comments: ", "additional_comments"));

$form -> addInput(new TextInput("", "","Submit","","",false,"submit"));

echo $form -> generateHTML();

RadioButton.php

<?php

require_once 'TextInput.php';

class RadioButton extends TextInput
{
    const TYPE2 = "radio";
    private $options = array();
    private $required = false;
    private $selected = false;
    private $title;
    private $type;
    public $radiobutton = "";

    function __construct($title = "", $name = "", $selected = false, $type = self::TYPE2)
    {
        $this -> title = $title;
        $this -> name = $name;
        $this -> selected = $selected;
        $this -> type = $type;
    }

    function addOption(string $label, string $name)
    {
        $this->options[] = ['label' => $label, 'name' => $name,];
    }

    public function generateHTML():string
    {
        $req = "";

        if ($this -> required == true)
        {
            $req = self::$REQUIRED;
        }

        // How do I print the title? return "<title>$this->title</title>"?

        return join('', array_map(function($item)
        {return sprintf('<input type="radio" name="%s"> <label>%s</label><br/>', $item['name'], $item['label']);}
         , $this->options))));
    }
}

1 个答案:

答案 0 :(得分:0)

您可以将标题和html一起附加到选项中,如下所示。 <title> tag只应在<head>部分使用,而不应在正文中使用:

  

HTML元素定义文档的标题,显示在浏览器的标题栏或页面的选项卡上。它只能包含文本,并且会忽略任何包含的标记。

因此,我建议您使用divspan并应用类似title的类名,以便您可以根据需要将css规则应用于该类。

此外,似乎甚至没有使用req属性,因此您不必为$req分配值,除非您要在返回值中使用它。

public function generateHTML():string
{
    return sprintf('<div class="title">%s</div>',$this->title).' '.join('', array_map(function($item)
    {return sprintf('<input type="radio" name="%s"> <label>%s</label><br/>', $item['name'], $item['label']);}
     , $this->options));
}