Click here to Skip to main content
15,911,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a class Form, which has` write(), attr($key,$value) and addClass($array) methods. The write() method print an input element` type="$this->type", the attr($key,$value) method gives attribute to that input element, and the addClass($array) method gives style to that input element. I have written the write() and attr($key,$value) methods, everything is normal, it's work, but i have some problems with addClass($array) method.
For example`
$f = new TextElement();
$f->attr("value","#97BEF0");
$f->addStyle(array("color"=>"red", "font-size"=>"20px"));
$f->write();


Btw, i also have a class TextElement, which is inherited from class Form.

What I have tried:

class Form{
	public $type;
	public $attr = "";
	public $arr = array();
       //print input
	function write(){
		print "<input type='$this->type' $this->attr style='$this->arr'>";
	}
        //add attribute
	function attr($key,$value){
		$this->attr .= "$key='$value'";
	}
        //add style
	function addStyle($array){
		for($i = 0; $i < count($array); $i++){
			$this->arr = $array[$i];
		}
	}
}

class TextElement extends Form{
	function __construct(){
		$this->type = "text";
	}
}

$p = new TextElement();
$p->attr("value","#97BEF0");
$p->addStyle(array("color"=>"red", "font-size"=>"20px"));
$p->write();


So my problem is with addStyle($array) method, How must i give parameters to an array? It gives me error` Nitice: Array to string conversion.
Posted
Updated 13-Sep-18 8:51am
v3

1 solution

A style class is part of CSS which operates on HTML.
The class your referring to, in PHP, is altogether different, describing an 'object'.

If you wish to add a class via php, the simplest way is, when rendering the code, put a class=... attribute inside it.

For example:
PHP
<?php
  ECHO "<div class='cssStyle'> some content </div>";
?>
You must have the class 'cssStyle' defined in php file that's sent to the browser, either directly, or as a part of a style sheet.

Study up: CSS3[^] You may very likely need to study HTML and php, as well.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900