So my wife and I have been looking at house plans with the intent of building one day. And I have been scripting a lot. I don't know exactly what triggered it but I had a dream we were building the house and I was laying out the floor with CSS and Javascript. I made it self cleaning. To bad it can't really be done this way.
<div id="kitchenFloor" class="Pergo Mahogany WideStrips" onwalk="selfClean()"></div>
function selfClean()
{
var floor = house.getElementById("kitchenFloor");
if(floor.mud == true)
{
//remove Mud
floor.mud = false;
}
}
This is a blog for me to keep my scripting notes in, if you like the code go ahead and copy it.
Monday, September 17, 2012
Tuesday, September 11, 2012
HTML form element class object in PHP
Form Elements as easy as
$elem = new FormElement("yourElement", "Hello World Element");
echo $elem->HTML();
This Will print
--------------------------
<div class=''>Hello World Element</div>
<input id="yourElement" name="yourElement" type="text" form="form" onblur="Validate('yourElement')"/>
<div name='err' class='error'></div>
--------------------------
The last div named 'err' can be used to display errors from the validation.
<?php
/**
* Description of FormElement
* Form Element HTML Object
* @author Joseph Tveter
*
* $name is the name and id of the form element.
* $cmd is the HTML tag
* $label is the Label the User will see for the field the default is that it will not be there.
* $pos is the position of the label. The default is "top", but it will also accept "right", "left", and "bottom"
* $formName is the name of the form the element is attached to. The default is "form"
* $type is the type of input tag it is. The default is "text"
* $onblur is the javascript function called onblur. default will call the function called "Validate" with the name of the field. false will not include this field.
* $val is the value the tag has or will return. The default is ""
* $req is weather to make the field required or not. The default is false
* $disabled is weather to make the field disabled or not. The default is false
* $size is the size of the input field. The default is ""
* $maxLength is the Maximum Length of the input field. The default is ""
* $fullclass is the class assigned to the div wrapper around the label and form element. The default is ""
* $titleClass is the class assigned to the label. The default is ""
* $elementClass is the class of the form element. The default is ""
*/
class FormElement extends HTML
{
public function __construct($name, $label = "", $cmd = "input", $pos = "top", $formName = "form", $type = "text", $onblur = "default", $val = "", $req = false, $disabled = false, $size = "", $maxLength = "", $elementClass = "", $fullclass = "", $titleClass = "")
{
parent::__construct("div");
if($fullclass != "")
{
parent::addValue("class", $fullclass);
}
$input = new HTML($cmd);
$input->addValue("id", $name);
$input->addValue("name", $name);
$input->addValue("type", $type);
$input->addValue("form", $formName);
if($onblur != "default")
{
if($onblur != false)
{
$input->addValue("onblur", $onblur);
}
}
else
{
$input->addValue("onblur", "Validate('$name')");
}
if($val != "")
{
$input->addValue('value', $val);
}
if($disabled == true)
{
$input->addValue('disabled', "disabled");
}
if($req == true)
{
$input->addValue("required", "required");
}
if($size != "")
{
$input->addValue("size", $size);
}
if($maxLength != "")
{
$input->addValue("maxlength", $maxLength);
}
if($elementClass != "")
{
$input->addValue("class", $elementClass);
}
if($label != "")
{
switch($pos)
{
case "":
parent::addChild("<div class='$titleClass'>$label</div>");
parent::addChild($input->HTML());
parent::addChild("<div name='err' class='error'></div>");
break;
case "top":
parent::addChild("<div class='$titleClass'>$label</div>");
parent::addChild($input->HTML());
parent::addChild("<div name='err' class='error'></div>");
break;
case "bottom":
parent::addChild($input->HTML());
parent::addChild("<div class='$titleClass'>$label</div>");
parent::addChild("<div name='err' class='error'></div>");
break;
case "right":
$input->addChild("<span class='$titleClass'>$label</span>");
parent::addChild($input->HTML());
parent::addChild("<div name='err' class='error'></div>");
break;
case "left":
parent::addChild("<label for='$name' class='$titleClass'>$label</label>");
parent::addChild($input->HTML());
parent::addChild("<div name='err' class='error'></div>");
break;
}
}
else
{
parent::addChild($input->HTML());
}
}
}
?>
$elem = new FormElement("yourElement", "Hello World Element");
echo $elem->HTML();
This Will print
--------------------------
<div class=''>Hello World Element</div>
<input id="yourElement" name="yourElement" type="text" form="form" onblur="Validate('yourElement')"/>
<div name='err' class='error'></div>
--------------------------
The last div named 'err' can be used to display errors from the validation.
<?php
/**
* Description of FormElement
* Form Element HTML Object
* @author Joseph Tveter
*
* $name is the name and id of the form element.
* $cmd is the HTML tag
* $label is the Label the User will see for the field the default is that it will not be there.
* $pos is the position of the label. The default is "top", but it will also accept "right", "left", and "bottom"
* $formName is the name of the form the element is attached to. The default is "form"
* $type is the type of input tag it is. The default is "text"
* $onblur is the javascript function called onblur. default will call the function called "Validate" with the name of the field. false will not include this field.
* $val is the value the tag has or will return. The default is ""
* $req is weather to make the field required or not. The default is false
* $disabled is weather to make the field disabled or not. The default is false
* $size is the size of the input field. The default is ""
* $maxLength is the Maximum Length of the input field. The default is ""
* $fullclass is the class assigned to the div wrapper around the label and form element. The default is ""
* $titleClass is the class assigned to the label. The default is ""
* $elementClass is the class of the form element. The default is ""
*/
class FormElement extends HTML
{
public function __construct($name, $label = "", $cmd = "input", $pos = "top", $formName = "form", $type = "text", $onblur = "default", $val = "", $req = false, $disabled = false, $size = "", $maxLength = "", $elementClass = "", $fullclass = "", $titleClass = "")
{
parent::__construct("div");
if($fullclass != "")
{
parent::addValue("class", $fullclass);
}
$input = new HTML($cmd);
$input->addValue("id", $name);
$input->addValue("name", $name);
$input->addValue("type", $type);
$input->addValue("form", $formName);
if($onblur != "default")
{
if($onblur != false)
{
$input->addValue("onblur", $onblur);
}
}
else
{
$input->addValue("onblur", "Validate('$name')");
}
if($val != "")
{
$input->addValue('value', $val);
}
if($disabled == true)
{
$input->addValue('disabled', "disabled");
}
if($req == true)
{
$input->addValue("required", "required");
}
if($size != "")
{
$input->addValue("size", $size);
}
if($maxLength != "")
{
$input->addValue("maxlength", $maxLength);
}
if($elementClass != "")
{
$input->addValue("class", $elementClass);
}
if($label != "")
{
switch($pos)
{
case "":
parent::addChild("<div class='$titleClass'>$label</div>");
parent::addChild($input->HTML());
parent::addChild("<div name='err' class='error'></div>");
break;
case "top":
parent::addChild("<div class='$titleClass'>$label</div>");
parent::addChild($input->HTML());
parent::addChild("<div name='err' class='error'></div>");
break;
case "bottom":
parent::addChild($input->HTML());
parent::addChild("<div class='$titleClass'>$label</div>");
parent::addChild("<div name='err' class='error'></div>");
break;
case "right":
$input->addChild("<span class='$titleClass'>$label</span>");
parent::addChild($input->HTML());
parent::addChild("<div name='err' class='error'></div>");
break;
case "left":
parent::addChild("<label for='$name' class='$titleClass'>$label</label>");
parent::addChild($input->HTML());
parent::addChild("<div name='err' class='error'></div>");
break;
}
}
else
{
parent::addChild($input->HTML());
}
}
}
?>
HTML Form Class Object
I got your form right here.
$form = new form("myform");
echo $form->HTML();
This will print
----------------------
<form id="myform" name="myform" action="index.php" method="post">
</form>
---------------------
<?php
/**
* Description of form
* HTML form Object
* @author Joseph Tveter
*/
class form extends HTML
{
public function __construct($name = "form", $action = "index.php", $method = "post")
{
parent::__construct("form");
parent::addValue("id", $name);
parent::addValue("name", $name);
parent::addValue("action", $action);
parent::addValue("method", $method);
}
}
?>
Update to my Object Oriented HTML class
I removed most of the Actionscript like functions because it can not be changed after the server has sent it to the client. I am testing some HTML class extensions and will post them soon.
<?php
/**
* Description of HTML
* -- OO-HTML 1.1 --
* - Removed some methods that it didnt make sense to have.
* - Removed array key name requirement on addChild
*
* @author Joseph Tveter
*
* Class to allow HTML to be eaisly written in native php
* It will also work for building xml
*
* The $cmd string is the tag command such as a,div,html
*
* The $vals array holds a list of value commands for the html tag
*
* The $children array holds a list of children of the tag. They will be placed in decending order, so a tag at [1] will come before a tag at [2]
*
* To print the string call HTML();
* example:
*
* $mydiv = new HTML('div');
* $mydiv->addValue('name', 'mydiv');
* $mydiv->addChild('Hello World');
* echo $mydiv->HTML();
*
* This will print.
* --------------------
* <div name='mydiv'>
* Hello World
* </div>
* ----------------------
*
*/
class HTML
{
public $cmd = "";
public $vals = array();
public $children = array();
public function __construct($cmd)
{
$this->cmd = $cmd;
}
public function HTML()
{
$r = "";
$s = false;
$singleArr = array('input', 'hr', 'meta', 'link');
foreach($singleArr as $i)
{
if($this->cmd == $i)
{
$s = true;
}
}
//values
$val = "";
foreach($this->vals as $k => $v)
{
$val = $val." ".$k."=\"$v\"";
}
//Children
$child = "";
foreach($this->children as $v)
{
$child = $child.$v."\n";
}
//Build and return the HTML string
if($s == false)
{
$r = "<".$this->cmd.$val.">\n$child</".$this->cmd.">";
}
else
{
$r = "<".$this->cmd.$val."/>\n".$child;
}
return $r;
}
public function addChild($child)
{
$this->children[count($this->children) + 1] = $child;
}
public function addValue($cmd, $val)
{
$this->vals[$cmd] = $val;
}
}
?>
Subscribe to:
Comments (Atom)