Source for file tohtml.inc.php
Documentation is available at tohtml.inc.php
V4.93 10 Oct 2006 (c) 2000-2008 John Lim (jlim#natsoft.com.my). All rights reserved.
Released under both BSD license and Lesser GPL library license.
Whenever there is any discrepancy between the two licenses,
the BSD license will take precedence.
Some pretty-printing by Chris Oxenreider <oxenreid@state.net>
// specific code for tohtml
GLOBAL $gSQLMaxRows,$gSQLBlockRows,$ADODB_ROUND;
$ADODB_ROUND=
4; // rounding
$gSQLMaxRows =
1000; // max no of rows to download
$gSQLBlockRows=
20; // max no of rows per table block
// RecordSet to HTML Table
//------------------------------------------------------------
// Convert a recordset to a html table. Multiple tables are generated
// if the number of rows is > $gSQLBlockRows. This is because
// web browsers normally require the whole table to be downloaded
// before it can be rendered, so we break the output into several
// smaller faster rendering tables.
// $ztabhtml: the table tag attributes (optional)
// $zheaderarray: contains the replacement strings for the headers (optional)
// include('adodb.inc.php');
// $db = ADONewConnection('mysql');
// $db->Connect('mysql','userid','password','database');
// $rs = $db->Execute('select col1,col2,col3 from table');
// rs2html($rs, 'BORDER=2', array('Title1', 'Title2', 'Title3'));
// RETURNS: number of rows displayed
function rs2html(&$rs,$ztabhtml=
false,$zheaderarray=
false,$htmlspecialchars=
true,$echo =
true)
$s =
'';$rows=
0;$docnt =
false;
GLOBAL $gSQLMaxRows,$gSQLBlockRows,$ADODB_ROUND;
printf(ADODB_BAD_RS,'rs2html');
if (! $ztabhtml) $ztabhtml =
"BORDER='1' WIDTH='98%'";
$ncols =
$rs->FieldCount();
$hdr =
"<TABLE COLS=$ncols $ztabhtml><tr>\n\n";
for ($i=
0; $i <
$ncols; $i++
) {
$field =
$rs->FetchField($i);
if ($zheaderarray) $fname =
$zheaderarray[$i];
$typearr[$i] =
$rs->MetaType($field->type,$field->max_length);
//print " $field->name $field->type $typearr[$i] ";
$fname =
'Field '.
($i+
1);
if (strlen($fname)==
0) $fname =
' ';
$hdr .=
"<TH>$fname</TH>";
if ($echo) print
$hdr.
"\n\n";
// smart algorithm - handles ADODB_FETCH_MODE's correctly by probing...
$numoffset = isset
($rs->fields[0]) ||isset
($rs->fields[1]) || isset
($rs->fields[2]);
$s .=
"<TR valign=top>\n";
for ($i=
0; $i <
$ncols; $i++
) {
if ($i===
0) $v=
($numoffset) ?
$rs->fields[0] :
reset($rs->fields);
else $v =
($numoffset) ?
$rs->fields[$i] :
next($rs->fields);
if (empty($v)) $s .=
"<TD> </TD>\n";
$s .=
" <TD>".
$rs->UserDate($v,"D d, M Y") .
" </TD>\n";
if (empty($v)) $s .=
"<TD> </TD>\n";
else $s .=
" <TD>".
$rs->UserTimeStamp($v,"D d, M Y, h:i:s") .
" </TD>\n";
$v =
round($v,$ADODB_ROUND);
if (substr($v,8,2)=="BM" ) $v = substr($v,8);
$mtime = substr(str_replace(' ','_',microtime()),2);
$tmpname = "tmp/".uniqid($mtime).getmypid();
$fd = @fopen($tmpname,'a');
if (!function_exists ("mime_content_type")) {
function mime_content_type ($file) {
return exec("file -bi ".escapeshellarg($file));
$t = mime_content_type($tmpname);
$s .= (substr($t,0,5)=="image") ? " <td><img src='$tmpname' alt='$t'></td>\\n" : " <td><a
href='$tmpname'>$t</a></td>\\n";
if (strlen($v) ==
0) $v =
' ';
if ($rows >=
$gSQLMaxRows) {
$rows =
"<p>Truncated at $gSQLMaxRows</p>";
// additional EOF check to prevent a widow header
if (!$rs->EOF &&
$rows %
$gSQLBlockRows ==
0) {
//if (connection_aborted()) break;// not needed as PHP aborts script, unlike ASP
if ($echo) print
$s .
"</TABLE>\n\n";
else $html .=
$s .
"</TABLE>\n\n";
if ($echo) print
$s.
"</TABLE>\n\n";
else $html .=
$s.
"</TABLE>\n\n";
if ($docnt) if ($echo) print
"<H2>".
$rows.
" Rows</H2>";
return ($echo) ?
$rows :
$html;
// pass in 2 dimensional array
function arr2html(&$arr,$ztabhtml=
'',$zheaderarray=
'')
if (!$ztabhtml) $ztabhtml =
'BORDER=1';
$s =
"<TABLE $ztabhtml>";//';print_r($arr);
for ($i=
0; $i<
sizeof($zheaderarray); $i++
) {
$s .=
" <TH>{$zheaderarray[$i]}</TH>\n";
for ($i=
0; $i<
sizeof($arr); $i++
) {
for ($j=
0; $j<
sizeof($a); $j++
) {
if (empty($val)) $val =
' ';
$s .=
" <TD>$val</TD>\n";
$s .=
' <TD>'.
$a.
"</TD>\n";
} else $s .=
" <TD> </TD>\n";
Documentation generated on Sun, 09 Mar 2008 23:53:36 -0300 by phpDocumentor 1.4.0