Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>PHP 1. Задание 3</title>
- <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
- </head>
- <body>
- <?php
- $elements = [
- [
- "element" => "input",
- "htmltype" => "text",
- "name" => "firstname",
- "type" => "string",
- "placeholder" => "Введите имя"
- ],
- [
- "element" => "input",
- "htmltype" => "text",
- "name" => "age",
- "type" => "integer",
- "placeholder" => "Введите возраст"
- ],
- [
- "element" => "input",
- "htmltype" => "radio",
- "name" => "gender",
- "type" => "boolean",
- "value" => "male",
- "label" => "мужской"
- ],
- [
- "element" => "input",
- "htmltype" => "radio",
- "name" => "gender",
- "type" => "boolean",
- "value" => "female",
- "label" => "женский"
- ],
- [
- "element" => "input",
- "htmltype" => "submit",
- "value" => "Отправить"
- ],
- ];
- // Cоздаем функцию которая выводит атрибуты тэгов и их значения
- function atr($attribute, $attrValue)
- { echo " $attribute = '$attrValue' " ;};
- //Создаем функцию, которая принимает массив с различными свойствами HTML-элемента , (включая его название
- //и атрибуты) и создает html-разметку этого тэга :
- function constructorForElement(array $OneHtmlElementInArray){
- foreach ($OneHtmlElementInArray as $arrKey => $arrValue) {
- $endTag = "/>";
- switch ($arrKey) {
- case "element": echo "<$arrValue ";
- break;
- case "htmltype" : $htmltype = $arrValue;
- echo " type = $arrValue ";
- break;
- case "label" : echo " /><label for='$id'> $arrValue ";
- $endTag = "</$arrKey>";
- break;
- case "type" : $type = $arrValue;
- break;
- case "value" : $id = $arrValue;
- atr($arrKey, $arrValue);
- if ($htmltype == "radio"){
- atr($arrKey, $arrValue);
- echo " id='$id' ";
- }
- break;
- case "name" : if($htmltype == "text"){
- atr($arrKey, $arrValue);
- echo "value='$_POST[$arrValue]' ";
- $name = $arrValue;
- } else {
- atr($arrKey, $arrValue);
- }
- break;
- default: atr($arrKey, $arrValue);
- break;
- }
- }
- echo $endTag;
- }
- //Создаем функцию htmlConstructor которая принимает многомерный массив HTML элементов и с помощью функции
- //constructorForElement создает разметку на основании многомерного массива
- function htmlConstructor(array $htmlElementsArray) {
- foreach($htmlElementsArray as $htmlElement) {
- echo "<div class = 'row'>";
- constructorForElement($htmlElement);
- echo "</div>";
- }
- }
- ?>
- <div class="container">
- <form method = "post">
- <?php htmlConstructor($elements); ?>
- </form>
- </div>
- </body>
- </html>
RAW Paste Data