Documentation TYPO3 par Ameos

tohtml.inc.php

00001 <?php 
00002 /*
00003   V4.80 8 Mar 2006  (c) 2000-2006 John Lim (jlim@natsoft.com.my). All rights reserved.
00004   Released under both BSD license and Lesser GPL library license. 
00005   Whenever there is any discrepancy between the two licenses, 
00006   the BSD license will take precedence.
00007   
00008   Some pretty-printing by Chris Oxenreider <oxenreid@state.net>
00009 */ 
00010   
00011 // specific code for tohtml
00012 GLOBAL $gSQLMaxRows,$gSQLBlockRows,$ADODB_ROUND;
00013 
00014 $ADODB_ROUND=4; // rounding
00015 $gSQLMaxRows = 1000; // max no of rows to download
00016 $gSQLBlockRows=20; // max no of rows per table block
00017 
00018 // RecordSet to HTML Table
00019 //------------------------------------------------------------
00020 // Convert a recordset to a html table. Multiple tables are generated
00021 // if the number of rows is > $gSQLBlockRows. This is because
00022 // web browsers normally require the whole table to be downloaded
00023 // before it can be rendered, so we break the output into several
00024 // smaller faster rendering tables.
00025 //
00026 // $rs: the recordset
00027 // $ztabhtml: the table tag attributes (optional)
00028 // $zheaderarray: contains the replacement strings for the headers (optional)
00029 //
00030 //  USAGE:
00031 //      include('adodb.inc.php');
00032 //      $db = ADONewConnection('mysql');
00033 //      $db->Connect('mysql','userid','password','database');
00034 //      $rs = $db->Execute('select col1,col2,col3 from table');
00035 //      rs2html($rs, 'BORDER=2', array('Title1', 'Title2', 'Title3'));
00036 //      $rs->Close();
00037 //
00038 // RETURNS: number of rows displayed
00039 
00040 
00041 function rs2html(&$rs,$ztabhtml=false,$zheaderarray=false,$htmlspecialchars=true,$echo = true)
00042 {
00043 $s ='';$rows=0;$docnt = false;
00044 GLOBAL $gSQLMaxRows,$gSQLBlockRows,$ADODB_ROUND;
00045 
00046         if (!$rs) {
00047                 printf(ADODB_BAD_RS,'rs2html');
00048                 return false;
00049         }
00050         
00051         if (! $ztabhtml) $ztabhtml = "BORDER='1' WIDTH='98%'";
00052         //else $docnt = true;
00053         $typearr = array();
00054         $ncols = $rs->FieldCount();
00055         $hdr = "<TABLE COLS=$ncols $ztabhtml><tr>\n\n";
00056         for ($i=0; $i < $ncols; $i++) { 
00057                 $field = $rs->FetchField($i);
00058                 if ($field) {
00059                         if ($zheaderarray) $fname = $zheaderarray[$i];
00060                         else $fname = htmlspecialchars($field->name);   
00061                         $typearr[$i] = $rs->MetaType($field->type,$field->max_length);
00062                         //print " $field->name $field->type $typearr[$i] ";
00063                 } else {
00064                         $fname = 'Field '.($i+1);
00065                         $typearr[$i] = 'C';
00066                 }
00067                 if (strlen($fname)==0) $fname = '&nbsp;';
00068                 $hdr .= "<TH>$fname</TH>";
00069         }
00070         $hdr .= "\n</tr>";
00071         if ($echo) print $hdr."\n\n";
00072         else $html = $hdr;
00073         
00074         // smart algorithm - handles ADODB_FETCH_MODE's correctly by probing...
00075         $numoffset = isset($rs->fields[0]) ||isset($rs->fields[1]) || isset($rs->fields[2]);
00076         while (!$rs->EOF) {
00077                 
00078                 $s .= "<TR valign=top>\n";
00079                 
00080                 for ($i=0; $i < $ncols; $i++) {
00081                         if ($i===0) $v=($numoffset) ? $rs->fields[0] : reset($rs->fields);
00082                         else $v = ($numoffset) ? $rs->fields[$i] : next($rs->fields);
00083                         
00084                         $type = $typearr[$i];
00085                         switch($type) {
00086                         case 'D':
00087                                 if (empty($v)) $s .= "<TD> &nbsp; </TD>\n";
00088                                 else if (!strpos($v,':')) {
00089                                         $s .= " <TD>".$rs->UserDate($v,"D d, M Y") ."&nbsp;</TD>\n";
00090                                 }
00091                                 break;
00092                         case 'T':
00093                                 if (empty($v)) $s .= "<TD> &nbsp; </TD>\n";
00094                                 else $s .= "    <TD>".$rs->UserTimeStamp($v,"D d, M Y, h:i:s") ."&nbsp;</TD>\n";
00095                         break;
00096                         
00097                         case 'N':
00098                                 if (abs($v) - round($v,0) < 0.00000001)
00099                                         $v = round($v);
00100                                 else
00101                                         $v = round($v,$ADODB_ROUND);
00102                         case 'I':
00103                                 $s .= " <TD align=right>".stripslashes((trim($v))) ."&nbsp;</TD>\n";
00104                                 
00105                         break;
00106                         /*
00107                         case 'B':
00108                                 if (substr($v,8,2)=="BM" ) $v = substr($v,8);
00109                                 $mtime = substr(str_replace(' ','_',microtime()),2);
00110                                 $tmpname = "tmp/".uniqid($mtime).getmypid();
00111                                 $fd = @fopen($tmpname,'a');
00112                                 @ftruncate($fd,0);
00113                                 @fwrite($fd,$v);
00114                                 @fclose($fd);
00115                                 if (!function_exists ("mime_content_type")) {
00116                                   function mime_content_type ($file) {
00117                                     return exec("file -bi ".escapeshellarg($file));
00118                                   }
00119                                 }
00120                                 $t = mime_content_type($tmpname);
00121                                 $s .= (substr($t,0,5)=="image") ? " <td><img src='$tmpname' alt='$t'></td>\\n" : " <td><a
00122                                 href='$tmpname'>$t</a></td>\\n";
00123                                 break;
00124                         */
00125 
00126                         default:
00127                                 if ($htmlspecialchars) $v = htmlspecialchars(trim($v));
00128                                 $v = trim($v);
00129                                 if (strlen($v) == 0) $v = '&nbsp;';
00130                                 $s .= " <TD>". str_replace("\n",'<br>',stripslashes($v)) ."</TD>\n";
00131                           
00132                         }
00133                 } // for
00134                 $s .= "</TR>\n\n";
00135                           
00136                 $rows += 1;
00137                 if ($rows >= $gSQLMaxRows) {
00138                         $rows = "<p>Truncated at $gSQLMaxRows</p>";
00139                         break;
00140                 } // switch
00141 
00142                 $rs->MoveNext();
00143         
00144         // additional EOF check to prevent a widow header
00145                 if (!$rs->EOF && $rows % $gSQLBlockRows == 0) {
00146         
00147                 //if (connection_aborted()) break;// not needed as PHP aborts script, unlike ASP
00148                         if ($echo) print $s . "</TABLE>\n\n";
00149                         else $html .= $s ."</TABLE>\n\n";
00150                         $s = $hdr;
00151                 }
00152         } // while
00153 
00154         if ($echo) print $s."</TABLE>\n\n";
00155         else $html .= $s."</TABLE>\n\n";
00156         
00157         if ($docnt) if ($echo) print "<H2>".$rows." Rows</H2>";
00158         
00159         return ($echo) ? $rows : $html;
00160  }
00161  
00162 // pass in 2 dimensional array
00163 function arr2html(&$arr,$ztabhtml='',$zheaderarray='')
00164 {
00165         if (!$ztabhtml) $ztabhtml = 'BORDER=1';
00166         
00167         $s = "<TABLE $ztabhtml>";//';print_r($arr);
00168 
00169         if ($zheaderarray) {
00170                 $s .= '<TR>';
00171                 for ($i=0; $i<sizeof($zheaderarray); $i++) {
00172                         $s .= " <TH>{$zheaderarray[$i]}</TH>\n";
00173                 }
00174                 $s .= "\n</TR>";
00175         }
00176         
00177         for ($i=0; $i<sizeof($arr); $i++) {
00178                 $s .= '<TR>';
00179                 $a = &$arr[$i];
00180                 if (is_array($a)) 
00181                         for ($j=0; $j<sizeof($a); $j++) {
00182                                 $val = $a[$j];
00183                                 if (empty($val)) $val = '&nbsp;';
00184                                 $s .= " <TD>$val</TD>\n";
00185                         }
00186                 else if ($a) {
00187                         $s .=  '        <TD>'.$a."</TD>\n";
00188                 } else $s .= "  <TD>&nbsp;</TD>\n";
00189                 $s .= "\n</TR>\n";
00190         }
00191         $s .= '</TABLE>';
00192         print $s;
00193 }
00194 
00195 ?>


Généré par TYPO3 Ameos avec  doxygen 1.4.6