Location: PHPKode > scripts > HTML/XHTML and XML Writer > class_HTML.php
<?php
class HTML {
	
	static public	$unclosed = array('input','br','hr','img','link','meta','base','frame');
	
	function __construct($encoding = 'UTF-8',$asXML=false,$dtd='Transitional',$mainTag='html') {
		$this->mt = $mainTag;
		if($asXML)header('content-type:text/xml; charset='.$encoding);
		
		echo '<?xml version="1.0" encoding="' . $encoding . '" ?>'."\n";
		echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 '.ucfirst($dtd).'//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-'.strtolower($dtd).'.dtd">'."\n";
		echo '<'.$mainTag.''.($asXML?'':' xmlns="http://www.w3.org/1999/xhtml"').'>'."\n";
		
		foreach($_GET as $k=>$v) {
			if(is_string($v)) {
			$Data = htmlspecialchars(mysql_real_escape_string($v));
			$this->getData[$k]=$Data;
			}
		}
		
		foreach($_POST as $k=>$v) {
			if(is_string($v)) {
			$Data = htmlspecialchars(mysql_real_escape_string($v));
			$this->postData[$k]=$Data;
			}
		}
		
	}
	
	public function _GET($key) {
		return $this->getData[$key];
	}
	
	public function _POST($key) {
		return $this->postData[$key];
	}
	
	static function QS() {
		return htmlspecialchars(mysql_real_escape_string($_SERVER['QUERY_STRING']));
	}
	
	static public function htmlAttr($arr) {
		if(is_array($arr)) {
		if($arr['name']!=''&&!$arr['id'])$arr['id']=$arr['name'];
		foreach ($arr as $attr=>$val) {
			$txt.=join("=",array(urlencode($attr),self::wrapText($val)))." ";
		}
		return " ".trim($txt);
		} else if(is_string($arr)) {
			return $arr;
		}
	}
	
	static public function Tag($tagName,$args=NULL) {
		
		$inners = func_get_args();
	
		unset($inners[0]);
		unset($inners[1]);
		
		$ret.= "\n".'<'.$tagName.self::htmlAttr($args).(in_array($tagName,self::$unclosed)?' /':'').'>';
		foreach($inners as $inner) {
			$t.= $inner;
		}
		if(!in_array($tagName,self::$unclosed))$t.= (strlen($t)>50?"\n":''). '</'.$tagName.'>'.(strlen($t)>50?"\n":'');
		return $ret.$t;
	}
	
	static public function wrapText($txt) {
		return "\"".htmlspecialchars(addslashes($txt))."\"";
	}
	
	public function createHiddens() {
		parse_str($_SERVER['QUERY_STRING'],$arr);
		if(count($arr)>0) {
			foreach($arr as $key=>$value) {
				$ret.=$this->Input('hidden',array('name'=>$key,'value'=>$value,'id'=>md5(rand(0,999999))));
			}
		}
		return $ret;
	}
	
	static public function wrapTextQ($txt) {
		return "'".$txt."'";
	}
	
	public function Tagy($tag,$text) {
		return '<'.urlencode($tag).'>'.$text.'</'.urlencode($tag).'>';
	}
	
	public function OptionizeArray($arr,$name=NULL) {
		foreach($arr as $val=>$text) {
			$Attrs = array('value'=>$val);
			if($_POST[$name]==$val||$_GET[$name]==$val)$Attrs['selected']='selected';
			$ret.=$this->Tag('option',$Attrs,$text);
		}
		return $ret;
	}
	
	public function ImplodeTags($arr) {
		foreach($arr as $val) {
			$ret.=$val;
		}
		return $ret;
	}
	
	public function Tagize($tag,$arr,$attrs=NULL) {
		foreach($arr as $val) {
			$ret.=$this->Tag($tag,NULL,$val);
		}
		return $ret;
	}
	
	public function Scripts() {
		$scripts = func_get_args();
		foreach ($scripts as $src) {
		$ret.= $this->Tag('script',array('type'=>'text/javascript', 'src'=>$src, 'language'=>'javascript'));
		}
		return $ret;
	}
	
	public function Script($script) {
		return $this->Tag('script',array('type'=>'text/javascript', 'language'=>'javascript'),$this->Comment($script));
	}
	
	public function Styles($src) {
		$styles = func_get_args();
		foreach ($styles as $src) {
		$ret.= $this->Tag('link',array('rel'=>'stylesheet', 'href'=>$src, 'type'=>'text/css'));
		}
		return $ret;
	}
	
	public function autoCompleteData($sql,$field,$dbobj) {
		$arr = $dbobj->getRows($sql);
		foreach($arr as $Row) {
			$Ref[] = $Row[$field];
		}
		return $Ref;
	}
	
	public function comboBoxData($sql,$id,$field,$dbobj) {
		$arr = $dbobj->getRows($sql);
		$Ref[NULL] = function_exists('__')?__('PLEASE_CHOOSE'):'[ Seçiniz ]';
		foreach($arr as $Row) {
			$Ref[$Row[$id]] = $Row[$field];
		}
		return $Ref;
	}
	
	public function Input($type,$attrs) {
		$attrs['type'] = $type;
		$postval = ($_POST[$attrs['name']]!=''?$_POST[$attrs['name']]:$_GET[$attrs['name']]);
		if($attrs['value']==''&&$postval!='')$attrs['value']=stripslashes($postval);
		$attrs = array_reverse($attrs);
		return $this->Tag('input',$attrs); 
	}
	
	public function InputAutocomplete($type,$attrs,$arr) {
		$id = md5(rand(0,999));
		$attrs['type'] = $type;
		$attrs['id'] = $id;
		$postval = ($_POST[$attrs['name']]!=''?$_POST[$attrs['name']]:$_GET[$attrs['name']]);
		if($attrs['value']==''&&$postval!='')$attrs['value']=stripslashes($postval);
		$attrs = array_reverse($attrs);
		return $this->Tag('input',$attrs).((count($arr)>0)?$this->Script(
			'var a'.$id.' = "'.addslashes(implode('|',$arr)).'".split("|");'."\n".
			$this->JQueryFunction("input#$id",'autocomplete','a'.$id)
		):NULL); 
	}
	
	public function Comment($txt) {
		return "\n".'<!-- '."\n".$txt."\n".' -->';
	}
	
	public function Title($title) {
		return self::Tag('title',NULL,$title);
	}
	
	public function ComboBox($attrs,$arr) {
		return $this->Tag('select',$attrs,
		$this->OptionizeArray($arr,$attrs['name'])
		);
	}
	
	public function Submit($text,$attr=array()) {
		return $this->Tag('button',array('name'=>'submit','type'=>'submit','value'=>$text),$text);
	}
	
	public function Form($method,$action,$attrs) {
		$attrs['method']=$method;
		$attrs['action']=$action;
		$inners = func_get_args();
	
		unset($inners[0]);
		unset($inners[1]);
		unset($inners[2]);
		
		return $this->Tag('form',$attrs,$this->ImplodeTags($inners));
	}

	public function Checkbox($attrs,$label=NULL) {
		if($attrs['value']) {
		if($_POST[$attrs['name']]==$attrs['value']||$_GET[$attrs['name']]==$attrs['value'])$attrs['checked']='checked';
		} else {
		if($_POST[$attrs['name']]=='on'||$_GET[$attrs['name']]=='on')$attrs['checked']='checked';
		}
		return $this->Input('checkbox',$attrs).(!is_null($label)?'&nbsp;'.$this->Tag('label',array('for'=>$attrs['name']),$label):'');
	}
	
	public function RadioGroup($attrs,$arr) {
		foreach($arr as $value=>$label) {
			$attrs['value']=$value;
			$attrs['id']=$value;
			if($_POST[$attrs['name']]==$value||$_GET[$attrs['name']]==$value)$attrs['checked']='checked';
			$ret.=$this->Input('radio',$attrs)."&nbsp;".$this->Tag('label',array('for'=>$attrs['id']),$label);
		}
		return $ret;
	}
	
	public function Head() {
		$arrs = func_get_args();
		return $this->Tag('head',NULL,
		$this->ImplodeTags($arrs)
		);
	}
	
	public function Body() {
		$arrs = func_get_args();
		return $this->Tag('body',NULL,
		$this->ImplodeTags($arrs)
		);
	}
	
	public function TabMenu($ajax,$labels=array(),$arr=array()) {
		foreach($labels as $key=>$label) {
		$links.= $this->Tag('li',NULL,$this->Tag('a',array('href'=>(is_integer($key)?'#fragment-'.$key:$key)),$this->Tagy('span',htmlspecialchars($label))));	
		}
		foreach($labels as $key=>$label) {
		$divs.= $this->Tag('div',array('id'=>'fragment-'.$key),$arr[$key]);
		}
		$ret = $this->Tag('div',array('id'=>'container'),
		$this->Tag('ul',NULL,
				$links
			),
			($ajax!==true)?$divs:''
		);
		$Opts = array('fxFade'=>true,'fxSpeed'=>'fast');
		if($ajax)$Opts['remote'] = true;
		return $ret.$this->Script($this->JQueryFunction('#container','tabs',$Opts));
	}
	
	public function SlideMenu() {
		$arr = func_get_args();
		return 
			$this->Styles('css/leftMenu.css').
			$this->Tag('div',array('class'=>'navBar'),
			$this->ImplodeTags($arr),
			$this->Scripts('js/leftMenu.js')
		);
	}
		
	public function AddMenu($title,$icon=NULL,$links=array()) {
		if(count($links)>0) {
		foreach($links as $label=>$link) {
			$linkcreated.=$this->Tag('div',array('class'=>'subItem'),
							$this->Tag('img',array('src'=>'img/arrow_r.gif','align'=>'absmiddle')),
							$this->Tag('a',array('href'=>$link),$label)
						  );
		}
		}
		return $this->Tag('div',array('class'=>'mainDiv'),
					$this->Tag('div',array('class'=>'topItem'),
						($icon)?$this->Tag('img',array('src'=>$icon,'align'=>'absmiddle','vspace'=>4,'hspace'=>3)):'',
						$title
					),
					$this->Tag('div',array('class'=>'dropMenu'),
						$this->Tag('div',array('class'=>'subMenu','style'=>'display:inline'),
						$linkcreated
						)
					)
		).$this->Tag('br');
		
	}
	
	public function dateText($time) {
		$time = strtotime($time);
		$m = date('m',$time);
		$d = date('d',$time);
		$y = date('y',$time);
		$H = date('H',$time);
		$i = date('i',$time);
		$s = date('s',$time);
		$today 		= mktime(0,0,0,$m,$d,$y);
		$yesterday 	= mktime(0,0,0,$m,($d+1),$y);
		$tomorrow 	= mktime(0,0,0,$m,($d-1),$y);
		
		$r = date('Y-m-d',$time);
		if(date('y-m-d',$today)==date('y-m-d')){$r 		= __('TODAY');}
		if(date('y-m-d',$yesterday)==date('y-m-d')){$r 	= __('YESTERDAY');}
		if(date('y-m-d',$tomorrow)==date('y-m-d')){$r	= __('TOMORROW');}
		
		return $r.' '.date('H:i:s',$time);
	}
	
	public function turkishPrefix($number) {
		$num = floatval($number);
		$pref = array('da','de','de','te','te','te','da','de','de','da','da');
		$pref[20]='de';$pref[30]='da';$pref[40]='ta';$pref[50]='de';$pref[60]='ta';
		if($num<=10||$num%10==0) {
			return $number."'".$pref[$num];
		} else {
			return $number."'".$pref[substr($num,1,1)];
		}
	}
	
	public function WriteClock() {
		return 
		
		$this->Tag('div',array('id'=>'saat','class'=>'kucuk','align'=>'center'),date('d.m.Y H:i:s')).
		$this->Script(
		'
		var f=function() {
		var zaman = new Date();
		document.getElementById("saat").innerHTML = ""+ "'.date('d.m.Y'). ' "
		+ zaman.getHours() + ":" 
		+ ( (zaman.getMinutes()<10)?(\'0\'+zaman.getMinutes()):zaman.getMinutes() ) + ":" 
		+ ( (zaman.getSeconds()<10)?(\'0\'+zaman.getSeconds()):zaman.getSeconds() );
		}
		window.onload = function(){setInterval(f,1000);}
		'
		);
	}
	
	public function JQueryFunction($item,$func,$opt=array()) {
		return "$".($item!=''?("('$item')"):'').($func!=''?".$func":'')."(".(is_array($opt)?json_encode($opt):$opt).");\n";
	}
	
	public function JSFunctionWrap($func,$variable='') {
		return (($variable!='')?"\n var $variable = ":'')."function() {\n".
				$func
				."}\n";
	}
	
	public function JSRedirect($link) {
		return 'location.href="'.addslashes($link).'";';
	}
	
	public function ProductHeader($title) {
		return 
		$this->Tag('table',array('align'=>'center','cellpadding'=>0,'cellspacing'=>0,'border'=>0,'width'=>'80%'),
			$this->Tag('tr',NULL,
				$this->Tag('td',array('colspan'=>2,'height'=>8))
			),
			$this->Tag('tr',NULL,
				$this->Tag('td',array('valign'=>'top','width'=>25),
					$this->Tag('img',array('src'=>'img/login_logo1_kucuk.png','align'=>'absmiddle','style'=>'width:24px;height:24px;'))
				),
				$this->Tag('td',array('valign'=>'top'),
					$this->Tag('span',array('class'=>'kucuk'),
						$this->Tag('font',array('color'=>'white'),$this->Tagy('b',$title))
					)
				)
			),
			$this->Tag('tr',NULL,
				$this->Tag('td',array('colspan'=>2,'align'=>'center','height'=>8),
					$this->Tag('hr')
				)
			)
		);
	}
	
	public function requiredVariables() {
		$varsToControl = (is_array(func_get_arg(0)))?func_get_arg(0):func_get_args();
		foreach($varsToControl as $v) {
			if(trim($v)==NULL) {
				return false;
				break;
			}
		}
		return true;
	}
	
	public function doIf($cond,$s) {
		assert_options(ASSERT_QUIET_EVAL,1);
		assert_options(ASSERT_WARNING,0);
		return assert($cond)?$s:false;
	}
	
	public function Pozitim() {
		return 
		$this->Tag('p',array('align'=>'center'),
			$this->Tag('a',array('style'=>'text-decoration:none','href'=>'http://www.pozitim.com','target'=>'_blank'),
				$this->Tag('font',array('color'=>'white','class'=>'kucuk'),
					$this->Tag('img',array('src'=>'img/pozitim.png','align'=>'absmiddle','border'=>0,'vspace'=>3)),
					$this->Tag('br'),
					$this->Tagy('b','Pozitim Technology')
				)
			)
		);
	}
		
	#Temel olmayan fonksiyon. Paginate class gerekli.
	public function createPaginateFromDatabase($table,$perPage,$query,$colspan) {
		global $DBCONN,$contentCountPerPage,$page,$start;
		$page = (floatval($_GET['page']) == 0)?1:floatval($_GET['page']);
		$contentCountPerPage = $perPage;
		$start = ($page-1)*$contentCountPerPage;
		if(false!==$query) {
			$numRows=$DBCONN->getRows(str_ireplace('SELECT *','SELECT count(*) as num',$query));
		} else {
			$numRows=$DBCONN->getRows("SELECT count(*) as num FROM ".$table);
		}
		
		if ($numRows[0]['num'] > $contentCountPerPage)
		{
			$pageObj 	= new Paginate($numRows[0]['num'],'',$contentCountPerPage,$page);
						  $pageObj->SetCurrentPage($page);
			$linkStr  	= $pageObj->GetPageLinks();
			$fromToItem	= substr($pageObj->GetSqlLimit(),7,10);
			$fromToItem	= explode(',',$fromToItem);
			$Paginate 	= $this->Tag('div',array('align'=>'left'),'Görüntülenen: '.$this->Tagy('b',$fromToItem[0]+1).' - '.$this->Tagy('b',($fromToItem[0]+$fromToItem[1])>$numRows[0]['num']?$numRows[0]['num']:($fromToItem[0]+$fromToItem[1])).' / '. $this->Tagy('b',number_format($numRows[0]['num'],0,',','.')));
			if ($pageObj->GetPageCount()==1) $linkStr = '';
			$Paginate	= $this->Tag('tr',NULL,$this->Tag('td',array('align'=>'left','colspan'=>$colspan),$Paginate));
			$Paginate  .= $this->Tag('tr',NULL,$this->Tag('td',array('align'=>'center','colspan'=>$colspan,'class'=>'searchRow'),$linkStr));
		}
		$rows = $DBCONN->getRows($query." LIMIT $start,$contentCountPerPage");
		return array('html'=>$this->Tag('div',NULL,$Paginate),'rows'=>$rows,'totalRows'=>$numRows[0]['num']);
	}
	
	public function __destruct() {
		echo "\n".'</'.$this->mt.'>';
	}
}
/*
 * Documentation
 * 
 * 
 * echo $Document->Body(
	$Document->Tag('div',array('class'=>'div'),
		$Document->Tagy('b','selam')
	),
	
	$Document->Form('GET',$_SERVER['PHP_SELF'],NULL,
		$Document->Tag('table',array('border'=>1),
			$Document->Tag('tr',NULL,
				$Document->Tagy('th','Adi'),
				$Document->Tagy('th','Soyadi'),
				$Document->Tagy('th','Adresi'),
				$Document->Tagy('th','Sehir'),
				$Document->doIf('2==2',$Document->Tagy('th','Kita'))
			),
			$Document->Tag('tr',NULL,
				$Document->Tagize('td',array(
				$Document->ComboBox(array('name'=>'isim'),array('fatih'=>'Fatih','akin'=>'AKIN')),
				'Akin',
				'Beylikduzu',
				'Istanbul',
				$Document->Tagy('b','Avrupa')
				))
			)
		), //end table
		$Document->ComboBox(array('name'=>'a'),array('fatih'=>'Fatih','kadir'=>'Kadir')),
		$Document->Submit('Gönder'),
		$Document->Submit('Ara'),
		$Document->Checkbox(array('name'=>'fatih'),'Evet'),
		$Document->Tag('br'),
		$Document->RadioGroup(array('name'=>'radios'),array('erkek'=>'Erkek','kadin'=>'Kad&#305;n'))
	),
	
	$Document->TabMenu(array('Yeni Kay&#305;t','Düzenle'),array(
			'fatih',
			'ak&#305;n'
	)),
	$Document->SlideMenu(
		$Document->AddMenu('Deneme',NULL,array(
			'Google'=>'http://www.google.com.tr',
			'Googles'=>'http://www.google.com.tr',
			'Googlea'=>'http://www.google.com.tr',
			'Googled'=>'http://www.google.com.tr'
		)),
	$Document->AddMenu('Deneme','img/app.gif',array(
			'Google'=>'http://www.google.com.tr',
			'Googles'=>'http://www.google.com.tr',
			'Googlea'=>'http://www.google.com.tr',
			'Googled'=>'http://www.google.com.tr'
		))
	)
); */
?>
Return current item: HTML/XHTML and XML Writer