<?php
//example ( why doesnt this work ? )
/* output should be:
<html>
<head>
<title>Title of this Document</title>
</head>
<body>
<a href="http://www.google.com">Click here</a>
<a href="http://www.google.com">Click here</a>
<a href="http://www.google.com">Click here</a>
<a href="http://www.google.com">Click here</a>
<a href="http://www.google.com">Click here</a>
</body>
</html>
*/
/* output is:
<html>
<head>
</head>
<body>
</body>
</html>
Conclusion: only 2nd depth is reached ( why ?)
code below:
*/
$html = new HTMLtag("html",array());
$title = new HTMLtag("title",array());
$head = new HTMLtag("head",array());
$body = new HTMLtag("body",array());
$a = new HTMLtag("a",array("href" => "http://www.google.com"));
$html->addContent($head);
$html->addContent($body);
$body->addContent($a);
$body->addContent($a);
$body->addContent($a);
$body->addContent($a);
$body->addContent($a);
$head->addContent($title);
$a->addContent("Click here");
$title->addContent("Title of this Document");
$html->printit(0);
class HTMLtag{
var $tag = ""; //what tag?
var $content = array();
var $params = array(); //params of the begin tag.
function HTMLtag($tagName,$params){
$this->tag = $tagName;
$this->params = $params;
}
function addContent($c){
$this->content[] = $c;
}
function printit($depth){
$go = true;
$print = "\n";
for($i = 0;$i < $depth;$i++){
$print .= " ";
}
$print .= "<".$this->tag;
while(list($param,$value) = each($this->params)){
$print .= " $param=\"$value\"";
}
$print .= ">";
print($print);
for($i = 0; $i < count($this->content);$i++){
$c = $this->content[$i];
if(is_object($c)){
$c->printit(($depth + 1));
}
else{
$c .= "</".$this->tag.">";
print($c);
$go = false;
}
}
if($go){
$print = "\n";
for($i = 0; $i < $depth; $i++){
$print .= " ";
}
$print .= "</".$this->tag.">";
print($print);
}
}
}
?>