E
E
Elina_212021-10-22 14:55:17
PHP
Elina_21, 2021-10-22 14:55:17

How to add styles to input through an associative array?

Good afternoon! Help to add styles to the input via
the addStyles($arr) method - when it is called, styles will be added to the input field ($_html), which are passed to the function as an associative array: property => value

<?php 
    interface InputFields {
        public function show();
        public function addStyle(array $styles);
    }
class InputEmail implements InputFields {
        private $_html = "<input type = 'email'>";
        public function show() {
            echo $this->_html . '<br>'. '<br>';
        }
        public function addStyle(array $styles) { }
    }

 $input = new InputEmail();
    $input->addStyle ([
        'width' => '300px',
        'border-radius' => '5px',
        'border' => '1px solid blue',
        'background' => '#fcfcfc',
        'padding' => '10px 20px',
    ]);
    $input->show();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
artloveyou, 2021-10-22
@Elina_21

http://sandbox.onlinephpfunctions.com/code/65cb7a...

<?php 
    interface InputFields {
        public function show();
        public function addStyle(array $styles);
    }
class InputEmail implements InputFields {
        private $_styles = "styles=\"";
        private $_start_tag = "<input type = 'email' ";
        private $_end_tag = ">";


        public function addStyle(array $styles) {
            foreach ($styles as $style => $value){
                $this->_styles .= $style . ':' . $value . ';';
            }
            $this->_styles .= '"';
            return $this->_styles;
        }
        
        public function show() {
            echo $this->_start_tag . $this->_styles . $this->_end_tag . '<br>'. '<br>';
        }
    }

 $input = new InputEmail();
    $input->addStyle ([
        'width' => '300px',
        'border-radius' => '5px',
        'border' => '1px solid blue',
        'background' => '#fcfcfc',
        'padding' => '10px 20px',
    ]);
    $input->show();

// Output: <input type = 'email' styles="width:300px;border-radius:5px;border:1px solid blue;background:#fcfcfc;padding:10px 20px;"><br><br>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question