Location: PHPKode > scripts > dbHtmlTable > dbHtmlTable.class.php
<?php
/*
    dbHtmlTable.class.php
    
    COPYRIGHT INFORMATION
    All code in this file and associated files is copyright
    ©2004-10 tweezy.net.au unless otherwise noted.
    Please read licence.txt
    ___________________________________________________________
    
    For more information, see dbHtmlTable.html
    and http://tweezy.net.au/doco/dbHtmlTable/
    ___________________________________________________________
*/ 
class dbHtmlTable { private $vTWZ1; private $vTWZ2; private $vTWZ3='<table border="1">'; private $vTWZ4='<tr>'; private $vTWZ5='<td>'; private $vTWZ6=array( 'hidefields','showtotal','alignleft','alignright','aligncentre', 'formatasnumber','formatasdate','autoemailfields','cellclass','headclass' ); protected $dbRowCount=null; protected $dbColumnCount=null; protected $dbSql; public function getVersion() { return 'v2.0.2 2010-08-02'; } public function __construct($vTWZ7, $vTWZ8=array(), $vTWZ9=false) { $this->setQuery($vTWZ7); $this->_resetGeneralOptions(); $this->setOptions($vTWZ8); if($vTWZ9) { $this->setOption('echohtml', true); $this->createTable(); } } public function setQuery($vTWZ7) { $this->_resetResultOptions(); $this->dbSql = $vTWZ7; } public function setOptions($vTWZ8=array()) { foreach($vTWZ8 as $vTWZ10=>$vTWZ11) { $this->setOption($vTWZ10, $vTWZ11); } } public function setOption($vTWZ12, $vTWZ13) { $vTWZ12=strtolower($vTWZ12); switch($vTWZ12) { case 'echoit': case 'echo': $vTWZ12='echohtml'; break; case 'aligncenter': case 'center': case 'centre': $vTWZ12='aligncentre'; break; case 'left': $vTWZ12='alignleft'; break; case 'right': $vTWZ12='alignright'; break; case 'oddrow': case 'oddclass': $vTWZ12='oddrowclass'; break; case 'evenrow': case 'evenclass': $vTWZ12='evenrowclass'; break; case 'hide': $vTWZ12='hidefields'; break; case 'rn': $vTWZ12='rownumber'; break; case 'group': case 'grp': $vTWZ12='groupfirst'; break; case 'header': case 'showheader': $vTWZ12='showfieldnames'; break; case 'footer': $vTWZ12='showfooter'; break; case 'empty': $vTWZ12='emptyvalue'; break; case 'null': $vTWZ12='nullvalue'; break; case 'asnum': $vTWZ12='formatasnumber'; break; case 'asdate': $vTWZ12='formatasdate'; break; case 'total': $vTWZ12='showtotal'; break; case 'link': $vTWZ12='linkfields'; break; case 'currencysymbol': case 'money': $vTWZ12='currency'; break; case 'thousandsep': case 'thou': $vTWZ12='thousandssep'; break; case 'decimal': $vTWZ12='decimalchar'; break; } if(isset($this->vTWZ1[$vTWZ12])) { $this->vTWZ1[$vTWZ12]=$vTWZ13; switch($vTWZ12) { case 'rownumber': if($this->vTWZ1[$vTWZ12]===true) $this->vTWZ1[$vTWZ12]='&nbsp;'; break; case 'tableattr': if($vTWZ13<>'') $this->vTWZ3="<table $vTWZ13>"; break; case 'trattr': if($vTWZ13<>'') $this->vTWZ4="<tr $vTWZ13>"; break; case 'tdattr': if($vTWZ13<>'') $this->vTWZ5="<td $vTWZ13>"; break; } return true; } else return false; } public function resetOptions() { $this->_resetGeneralOptions(); $this->_resetResultOptions(); } public function createTable() { $vTWZ14="\r\n"; $vTWZ15 = array('&nbsp;', $this->vTWZ1['nullvalue'], $this->vTWZ1['emptyvalue']); $vTWZ16 = $this->_dbGetResultset(); if(!$vTWZ16) return ''; $this->vTWZ2=$this->_dbGetFieldnames($vTWZ16); $this->_fieldOptionsToArray(); $vTWZ17 = $this->_fieldExtra('formatasdate'); $vTWZ18 = $this->_fieldExtra('formatasnumber'); $vTWZ19 = $this->_fieldExtra('cellclass'); $vTWZ20 = $this->_fieldExtra('headclass'); $this->_setFieldIndexes(); $vTWZ14.=$this->vTWZ3."\r\n"; if($this->vTWZ1['caption']<>'') $vTWZ14.="<caption>".$this->vTWZ1['caption']."</caption>\r\n"; if($this->vTWZ1['showfieldnames']) { $vTWZ14.=$this->_fieldHeadings($vTWZ20); } if(count($this->vTWZ1['showtotal'])>0) { $vTWZ21=array_fill(0, count($this->vTWZ2), 0); } $vTWZ22=0; $vTWZ23=false; $vTWZ24='@#@'; while($vTWZ25=$this->_dbGetRow($vTWZ16)) { $vTWZ22++; $vTWZ23=!$vTWZ23; if ($this->vTWZ1['groupfirst'] and $vTWZ25[0]<>$vTWZ24) { $vTWZ14.="\t".'<tr class="grph">'; if($this->vTWZ1['rownumber']) $vTWZ14.='<td class="rn">&nbsp;</td>'; $vTWZ14.='<td class="grp" colspan="' .($this->dbColumnCount - count($this->vTWZ1['hidefields'])) .'">'.$vTWZ25[0]."</td></tr>\r\n"; } $vTWZ14.="\t".$this->_getTrTag($vTWZ23, $vTWZ22)."\r\n"; $vTWZ26=$this->_getLinkSettings($vTWZ25); if($this->vTWZ1['rownumber']) { $vTWZ14.="\t\t<td class=\"rn\">$vTWZ22</td>\r\n"; } for ($vTWZ27 = 0; $vTWZ27 < $this->dbColumnCount; $vTWZ27++) { if(in_array($vTWZ27, $this->vTWZ1['hidefields'])) continue; $vTWZ14.="\t\t".$this->_getTdTag($vTWZ27, $vTWZ19, $vTWZ25); $vTWZ28=$vTWZ25[$vTWZ27]; if(0==$vTWZ27 and $this->vTWZ1['groupfirst']) $vTWZ28='&nbsp;'; if(is_null($vTWZ28)) $vTWZ28=$this->vTWZ1['nullvalue']; elseif($vTWZ28=='') $vTWZ28=$this->vTWZ1['emptyvalue']; if(is_numeric($vTWZ28) and in_array($vTWZ27, $this->vTWZ1['showtotal'])) { $vTWZ21[$vTWZ27] += $vTWZ28; } if(is_numeric($vTWZ28)) { $vTWZ28=$this->_numFormat($vTWZ27, $vTWZ28, $vTWZ18); } if(in_array($vTWZ27, $this->vTWZ1['formatasdate']) and !in_array($vTWZ28, $vTWZ15)) { $vTWZ29=array_search($vTWZ27, $this->vTWZ1['formatasdate']); $vTWZ30=($vTWZ29!==false and isset($vTWZ17[$vTWZ29])) ? $vTWZ17[$vTWZ29] : $this->vTWZ1['dateformat']; $vTWZ28=date($vTWZ30, strtotime($vTWZ28)); } if(isset($vTWZ26[$this->vTWZ2[$vTWZ27]]) and !in_array($vTWZ28, $vTWZ15)) { $vTWZ31=$vTWZ26[$this->vTWZ2[$vTWZ27]]; $vTWZ32=$vTWZ31['Href']; $vTWZ33=(isset($vTWZ31['Title'])) ? ' title="'.$vTWZ31['Title'].'"' : ''; $vTWZ34=(isset($vTWZ31['LinkAttrib'])) ? ' '.$vTWZ31['LinkAttrib'] : ''; if(''==$vTWZ32) { $vTWZ28="<span$vTWZ33$vTWZ34>$vTWZ28</span>"; } else { $vTWZ28="<a href=\"$vTWZ32\"$vTWZ33$vTWZ34>$vTWZ28</a>"; } } if(($this->vTWZ1['autoemaillink'] or in_array($vTWZ27, $this->vTWZ1['autoemailfields'])) and $this->_emailIsValid($vTWZ28)) { $vTWZ32="mailto:$vTWZ28"; if($this->vTWZ1['autoemailsubject']<>'') $vTWZ32.='?subject='.urlencode($this->vTWZ1['autoemailsubject']); $vTWZ28="<a href=\"$vTWZ32\">$vTWZ28</a>"; } $vTWZ14.="$vTWZ28</td>\r\n"; } $vTWZ24=$vTWZ25[0]; $vTWZ14.="\t</tr>\r\n"; } if(count($this->vTWZ1['showtotal'])>0) { $vTWZ14.=$this->_totalsRow($vTWZ21, $vTWZ18); } if($this->vTWZ1['showfooter']) $vTWZ14.=$this->_fieldHeadings($vTWZ20); $vTWZ14.="</table>\r\n"; if($this->vTWZ1['echohtml']) echo $vTWZ14; else return $vTWZ14; } private function _totalsRow($vTWZ21, $vTWZ18) { $vTWZ35="\t".str_replace('>', ' class="total">', $this->vTWZ4)."\r\n"; if($this->vTWZ1['rownumber']) { $vTWZ35.="\t\t<td class=\"rn\">&nbsp;</td>\r\n"; } foreach($this->vTWZ2 as $vTWZ27=>$vTWZ36) { if(in_array($vTWZ27, $this->vTWZ1['hidefields'])) continue; $vTWZ37=(in_array($vTWZ27, $this->vTWZ1['showtotal'])) ? $vTWZ21[$vTWZ27] : '&nbsp;'; if(is_numeric($vTWZ37)) { $vTWZ37=$this->_numFormat($vTWZ27, $vTWZ37, $vTWZ18); } $vTWZ35.="\t\t".$this->_addAlign($vTWZ27, $this->vTWZ5).$vTWZ37."</td>\r\n"; } $vTWZ35.="\t</tr>\r\n"; return $vTWZ35; } public function getRowCount() { return $this->dbRowCount; } public function getColumnCount() { return $this->dbColumnCount; } private function _emailIsValid($vTWZ38,$vTWZ39=1) { $vTWZ40='/^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i'; $vTWZ41=explode(',',$vTWZ38); if(count($vTWZ41)>$vTWZ39) return false; foreach($vTWZ41 as $vTWZ42) { if(!preg_match($vTWZ40, $vTWZ42)) return false; } return true; } private function _setFieldIndexes() { foreach($this->vTWZ6 as $vTWZ43) { $vTWZ44=array(); foreach($this->vTWZ1[$vTWZ43] as $vTWZ45=>$vTWZ46) { $vTWZ47=array_search($vTWZ46, $this->vTWZ2, true); if($vTWZ47!==false) { $this->vTWZ1[$vTWZ43][$vTWZ45]=$vTWZ47; } elseif(!is_numeric($vTWZ46)) { if('first'==strtolower($vTWZ46)) { $this->vTWZ1[$vTWZ43][$vTWZ45]=0; } elseif('last'==strtolower($vTWZ46)) { $this->vTWZ1[$vTWZ43][$vTWZ45]=count($this->vTWZ2)-1; } else { $vTWZ44[]=$vTWZ45; } } } foreach($vTWZ44 as $vTWZ48) { unset($this->vTWZ1[$vTWZ43][$vTWZ48]); } } } private function _fieldHeadings($vTWZ20) { if($this->vTWZ1['groupfirst'] and !is_numeric($this->vTWZ1['groupheadcount'])) { $this->vTWZ1['groupheadcount']=$this->dbColumnCount-1; } $vTWZ14="\t<tr>\r\n"; if($this->vTWZ1['rownumber']) { $vTWZ14.="\t\t<th class=\"rn\">".$this->vTWZ1['rownumber']."</th>\r\n"; } foreach($this->vTWZ2 as $vTWZ49=>$vTWZ36) { if(in_array($vTWZ49, $this->vTWZ1['hidefields'])) continue; $vTWZ50=$this->_addAlign($vTWZ49, '<th>'); if(in_array($vTWZ49, $this->vTWZ1['headclass'])) { $vTWZ51=array_search($vTWZ49, $this->vTWZ1['headclass']); if($vTWZ51!==false and isset($vTWZ20[$vTWZ51])) { $vTWZ50=$this->_addClass($vTWZ20[$vTWZ51], $vTWZ50); } } if($this->vTWZ1['groupfirst'] and $vTWZ49<($this->dbColumnCount - $this->vTWZ1['groupheadcount']) ) { $vTWZ36='&nbsp;'; } $vTWZ14.="\t\t$vTWZ50$vTWZ36</th>\r\n"; } $vTWZ14.="\t</tr>\r\n"; return $vTWZ14; } private function _resetGeneralOptions() { $this->vTWZ1['autoemaillink']=false; $this->vTWZ1['autoemailsubject']=''; $this->vTWZ1['echohtml']=true; $this->vTWZ1['tableattr']=''; $this->vTWZ1['trattr']=''; $this->vTWZ1['tdattr']=''; $this->vTWZ1['showfieldnames']=true; $this->vTWZ1['showfooter']=false; $this->vTWZ1['oddrowclass']=''; $this->vTWZ1['evenrowclass']=''; $this->vTWZ1['numberformat']=1; $this->vTWZ1['dateformat']='j M Y'; $this->vTWZ1['currency']='$'; $this->vTWZ1['decimalchar']='.'; $this->vTWZ1['thousandssep']=','; $this->vTWZ1['rownumber']=false; $this->vTWZ1['trevery']=''; $this->vTWZ1['emptyvalue']='&nbsp;'; $this->vTWZ1['nullvalue']='&nbsp;'; } private function _resetResultOptions() { $this->vTWZ1['caption']=''; $this->vTWZ1['autoemailfields']=''; $this->vTWZ1['linkfields']=array(); $this->vTWZ1['hidefields']=''; $this->vTWZ1['showtotal']=''; $this->vTWZ1['alignleft']=''; $this->vTWZ1['alignright']=''; $this->vTWZ1['aligncentre']=''; $this->vTWZ1['formatasnumber']=''; $this->vTWZ1['formatasdate']=''; $this->vTWZ1['cellclass']=''; $this->vTWZ1['headclass']=''; $this->vTWZ1['groupfirst']=false; $this->vTWZ1['groupheadcount']=false; } private function _fieldOptionsToArray() { foreach($this->vTWZ6 as $vTWZ43) { if(is_string($this->vTWZ1[$vTWZ43])) $this->vTWZ1[$vTWZ43]=explode(',', $this->vTWZ1[$vTWZ43]); } } private function _fieldExtra($vTWZ12) { $vTWZ52=array(); foreach($this->vTWZ1[$vTWZ12] as $vTWZ45=>$vTWZ53) { if($vTWZ54=strpos($vTWZ53, '=')) { $this->vTWZ1[$vTWZ12][$vTWZ45]=substr($vTWZ53,0,$vTWZ54); $vTWZ52[$vTWZ45]=substr($vTWZ53,$vTWZ54+1); } } return $vTWZ52; } private function _getTrTag($vTWZ55, $vTWZ22) { $vTWZ56=$this->vTWZ4; if($vTWZ55 and $this->vTWZ1['oddrowclass']<>'') { $vTWZ56=$this->_addClass($this->vTWZ1['oddrowclass'], $vTWZ56); } elseif(!$vTWZ55 and $this->vTWZ1['evenrowclass']<>'') { $vTWZ56=$this->_addClass($this->vTWZ1['evenrowclass'], $vTWZ56); } if(strpos($this->vTWZ1['trevery'], '@') > 0) { list($vTWZ57, $vTWZ58)=explode('@', $this->vTWZ1['trevery']); if($vTWZ58>0 and $vTWZ22%$vTWZ58==0) $vTWZ56=$this->_addClass($vTWZ57, $vTWZ56); } return $vTWZ56; } private function _getTdTag($vTWZ27, $vTWZ19, $vTWZ59) { $vTWZ60=$this->vTWZ5; if(in_array($vTWZ27, $this->vTWZ1['cellclass'])) { $vTWZ51=array_search($vTWZ27, $this->vTWZ1['cellclass']); if($vTWZ51!==false and isset($vTWZ19[$vTWZ51])) { $vTWZ61=$this->_getConditionalClasses($vTWZ19[$vTWZ51], $vTWZ59[$vTWZ27], $vTWZ59); if($vTWZ61 != '') $vTWZ60=$this->_addClass($vTWZ61, $vTWZ60); } } $vTWZ60=$this->_addAlign($vTWZ27, $vTWZ60); return $vTWZ60; } private function _addAlign($vTWZ27, $vTWZ62) { if(in_array($vTWZ27, $this->vTWZ1['alignleft'])) { $vTWZ62=str_replace('>', ' align="left">', $vTWZ62); } elseif(in_array($vTWZ27, $this->vTWZ1['alignright'])) { $vTWZ62=str_replace('>', ' align="right">', $vTWZ62); } elseif(in_array($vTWZ27, $this->vTWZ1['aligncentre'])) { $vTWZ62=str_replace('>', ' align="center">', $vTWZ62); } return $vTWZ62; } private function _numFormat($vTWZ27, $vTWZ37, $vTWZ18) { if(in_array($vTWZ27, $this->vTWZ1['formatasnumber'])) { $vTWZ63=array_search($vTWZ27, $this->vTWZ1['formatasnumber']); $vTWZ64=($vTWZ63!==false and isset($vTWZ18[$vTWZ63])) ? $vTWZ18[$vTWZ63] : $this->vTWZ1['numberformat']; if('1'==$vTWZ64) $vTWZ64='0020'; elseif('2'==$vTWZ64) $vTWZ64='1120'; elseif(3==strlen($vTWZ64)) $vTWZ64.='0'; if(4==strlen($vTWZ64)) { $vTWZ65=''; if('1'==$vTWZ64{0}) { $vTWZ65.=$this->vTWZ1['currency']; } $vTWZ66=('1'==$vTWZ64{1}) ? $this->vTWZ1['thousandssep'] : ''; $vTWZ67=(int)$vTWZ64{2}; $vTWZ65.=number_format($vTWZ37, $vTWZ67, $this->vTWZ1['decimalchar'], $vTWZ66); if('1'==$vTWZ64{3}) { $vTWZ65.=$this->vTWZ1['currency']; } $vTWZ37=$vTWZ65; } } return $vTWZ37; } private function _getConditionalClasses($vTWZ68, $twzVal, $vTWZ59) { $twzLst=''; if(preg_match_all('/'.'([a-z 0-9]+)'.'([@><!]{1,2})'.'([^|@><!]*)'.'/i', $vTWZ68, $vTWZ69)) { foreach($vTWZ69[1] as $vTWZ70=>$twzCls) { $vTWZ71=str_replace('@', '=', $vTWZ69[2][$vTWZ70]); if('='==$vTWZ71) $vTWZ71='=='; if('!'==$vTWZ71) $vTWZ71='!='; $vTWZ72=$vTWZ69[3][$vTWZ70]; if(preg_match('/^\[(.+)\]$/', $vTWZ72, $vTWZ73)) { $vTWZ74=array_search($vTWZ73[1], $this->vTWZ2, true); if($vTWZ74!==false and isset($vTWZ59[$vTWZ74])) $vTWZ72=$vTWZ59[$vTWZ74]; } $vTWZ75='if($twzVal '.$vTWZ71.' \''.$vTWZ72.'\') $twzLst.=$twzCls.\' \';'; @eval($vTWZ75); } } else { $twzLst=$vTWZ68; } return trim($twzLst); } private function _getLinkSettings($vTWZ59) { $vTWZ26=array(); foreach($this->vTWZ1['linkfields'] as $vTWZ36=>$vTWZ76) { if(preg_match_all('/\[(.+)\]/U', $vTWZ76, $vTWZ77)) { foreach($vTWZ77[1] as $vTWZ78) { $vTWZ49=array_search($vTWZ78, $this->vTWZ2); if($vTWZ49!==false) { $vTWZ76=str_replace("[$vTWZ78]", $vTWZ59[$vTWZ49], $vTWZ76); } } } $vTWZ79=explode('~||~', $vTWZ76); $vTWZ26[$vTWZ36]['Href']=$vTWZ79[0]; if(isset($vTWZ79[1])) $vTWZ26[$vTWZ36]['Title']=$vTWZ79[1]; if(isset($vTWZ79[2])) $vTWZ26[$vTWZ36]['LinkAttrib']=$vTWZ79[2]; } return $vTWZ26; } private function _addClass($vTWZ80, $vTWZ81) { if(strpos($vTWZ81, 'class=')===false) { $vTWZ81 = str_replace('>', ' class="'.$vTWZ80.'">', $vTWZ81); } else { $vTWZ81 = str_replace('class="', 'class="'.$vTWZ80.' ', $vTWZ81); } return $vTWZ81; } protected function _dbGetResultset() { $vTWZ16=mysql_query($this->dbSql); $this->dbRowCount=mysql_num_rows($vTWZ16); if(0==$this->dbRowCount) return false; $this->dbColumnCount=mysql_num_fields($vTWZ16); return $vTWZ16; } protected function _dbGetRow($vTWZ82) { return mysql_fetch_row($vTWZ82); } protected function _dbGetFieldnames($vTWZ82) { $vTWZ2=array(); for ($vTWZ83 = 0; $vTWZ83 < $this->dbColumnCount; $vTWZ83++) { $vTWZ2[]=mysql_field_name($vTWZ82, $vTWZ83); } return $vTWZ2; } } ?>
Return current item: dbHtmlTable