I recently created a "HTML Element" object which I can use to create any object while setting basically any attribute. I then made a "Table Creator" object which I used for creating tables since I realized that would still be messy with all those weird items.
One of old bookmarks recently came into play again which was phpQuery which let's you do some pretty neat things with HTML on the server side.
What is your opinion on these types of things? Know any tricks? See any downfalls?
<?PHP
// This would look a ton neater without ugly HTML clogging up it's arteries.
class userData {
private $id;
private $link;
public function __construct($link) {
$this->id = mt_rand(0,9);
$this->link = $link;
}
public function displayUser($uid) {
$rows = getUserInfo($uid);
if($rows===false) {
?>
<div class="banner error">
<h3>Error!</h3>
<p>This user does not exist!</p>
</div>
<?PHP
} else {
?>
<div class="banner user">
<div class="el">
<div class="key">Username</div>
<div class="value"><?=$row['username']?></div>
</div>
</div>
<?PHP
}
}
private function getUserInfo($uid) {
$query = mysql_query("SELECT * FROM `users` WHERE `id`={$uid} LIMIT 1", $this->link);
if(mysql_num_rows($query)==0) {
return false;
}
return mysql_fetch_assoc($query);
}
}<?PHP
/*
Generates HTML characters
Example #1:
$link = new HTMLElement('a'); //set name using constructor
$span = new HTMLElement(); //just declare a new element
$link->href = "index.html"; //set an attribute
$link->class = "ref"; //set a second attribute
$link->innerHTML = $span; //PHP passes objects by reference
$span->name = "span"; //set name since not declared earlier
$span->innerHTML = "Hello world"; //declare an attribute
echo $link->generate(); //generate my link
Output #1:
<a href="index.html" class="ref"><span>Hello world</span></a>
Example #2:
$table = new HTMLElement('table');
$row = new HTMLElement('tr');
$cell = new HTMLElement('td');
$cell->innerHTML = "1";
$table->innerHTML = $row;
$row->innerHTML = $cell;
$cell = new HTMLElement('td'); //disowns last object and creates new one
//I cannot edit the first cell anymore
$cell->innerHTML = "2";
$row->innerHTML = $cell;
echo $table->generate();
Output #2: (I Added formatting)
<table>
<tr>
<td>1</td>
<td>2</td>
</tr>
</table>
Functions:
public:
__construct([Element Name]);
Sets up the elemnt, can be used later using set("name",...)
__set([$key], [$value]), $obj->key = value;
Sets attribute keys and values
NOTE:
This always appends. Edit the object you have, but you can NEVER edit this data.
generate()
Generates the HTML output.
selfEnding(boolean)
If you want to over write the existing selfEnding for a tag that may be both
private:
setName($name)
Sets the name and attempts to figure out if this is a self ending tag or not
*/
class HTMLElement {
private $name;
private $attr = array();
private $selfEndingList = array('area', 'base', 'basefront', 'br', 'col', 'frame', 'hr', 'img', 'input', 'link', 'meta', 'param', 'script');
private $selfEnding;
private $innerHTML = array();
public function __construct($name = false, $data = false) {
/*
<{name} {attrs} [/>] OR [>{innerHTML}</{name}>]
*/
if($name !== false) {$this->setName($name);}
if($data !== false) {$this->__set('innerHTML', $data);}
}
private function setName($name) {
$this->name = $name;
$this->selfEnding = false;
if(in_array($this->name, $this->selfEndingList)) {
//automatically assigned
$this->selfEnding = true;
}
}
public function selfEnding($bool) {
/*
Some are self ending and non-self ending. So a way to over write this automatic feature is included
Example:
<script src="scripts.js" type="text/javascript" />
<script type="text/javascript">alert('Hello World');</script>
*/
$this->selfEnding = $bool;
}
public function __set($key, $value) {
/*
Setting function to set attributes
Example
$obj->src = "image.jpg";
*/
if($key=="innerHTML") {
$this->innerHTML[] = $value;
return;
} else if($key=="name") {
$this->setName($value);
return;
}
$this->attr[$key] = $value;
}
public function generate() {
/*
Creates the tag and returns it
*/
$attrs = "";
foreach($this->attr AS $key=>$value) {
//generates the attributes
$value = str_replace('"', '\"', $value);
$attrs .= " {$key}=\"{$value}\"";
}
if(in_array($this->name, $this->selfEndingList)) {
//self ending tag, no innerHTML
return "<{$this->name}{$attrs}/>";
} else {
//non self ending tags
foreach($this->innerHTML as $data) {
if(is_object($data)) {
//it is an HTMLElement object generate the data
//print_r($data);
$ex .= $data->generate();
} else {
$ex .= $data;
}
}
return "<{$this->name}{$attrs}>{$ex}</{$this->name}>";
}
}
}<?PHP
/*
Even with the HTMLElement Class it can still get messy making tables. This is the solution.
Example:
$table = new tableBuilder(array('cellpadding'=>'0', 'class'=>'calendar'));
$table->addRow();
$table->addCell('1', array('colspan'=>'2'));
$table->addRow();
$table->addCell('2');
$table->addCell('3');
echo $table->getTable();
Output: (Manually Formatted)
<table cellpadding="0" class="calendar">
<tr>
<td colspan="2">1</td>
</tr>
<tr>
<td>2</td>
<td>3</td>
</tr>
</table>
*/
include_once('HTMLElement.obj.php');
class tableBuilder {
private $table;
private $row;
private $cell;
public function __construct($extra = array()) {
$this->startTable($extra);
}
public function startTable($extra = array()) {
$this->table = new HTMLElement('table');
foreach($extra as $key=>$value) {
$this->table->$key = $value;
}
}
public function addRow($extra = array()) {
//starts a new row
$this->row = new HTMLElement('tr');
$this->table->innerHTML = $this->row;
foreach($extra as $key=>$value) {
$this->row->$key = $value;
}
}
public function addCell($data, $extra = array()) {
$this->cell = new HTMLElement('td');
$this->cell->innerHTML = $data;
foreach($extra as $key=>$value) {
$this->cell->$key = $value;
}
$this->row->innerHTML = $this->cell;
}
public function getTable() {
return $this->table->generate();
}
}


Sign In
Create Account



Back to top










