3 @version v5.20.1 06-Dec-2015
4 @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
5 @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
6 Released under both BSD license and Lesser GPL library license.
7 Whenever there is any discrepancy between the two licenses,
8 the BSD license will take precedence.
10 Some pretty-printing by Chris Oxenreider <oxenreid@state.net>
13 // specific code for tohtml
14 GLOBAL $gSQLMaxRows,$gSQLBlockRows,$ADODB_ROUND;
16 $ADODB_ROUND=4; // rounding
17 $gSQLMaxRows = 1000; // max no of rows to download
18 $gSQLBlockRows=20; // max no of rows per table block
20 // RecordSet to HTML Table
21 //------------------------------------------------------------
22 // Convert a recordset to a html table. Multiple tables are generated
23 // if the number of rows is > $gSQLBlockRows. This is because
24 // web browsers normally require the whole table to be downloaded
25 // before it can be rendered, so we break the output into several
26 // smaller faster rendering tables.
29 // $ztabhtml: the table tag attributes (optional)
30 // $zheaderarray: contains the replacement strings for the headers (optional)
33 // include('adodb.inc.php');
34 // $db = ADONewConnection('mysql');
35 // $db->Connect('mysql','userid','password','database');
36 // $rs = $db->Execute('select col1,col2,col3 from table');
37 // rs2html($rs, 'BORDER=2', array('Title1', 'Title2', 'Title3'));
40 // RETURNS: number of rows displayed
43 function rs2html(&$rs,$ztabhtml=false,$zheaderarray=false,$htmlspecialchars=true,$echo = true)
45 $s ='';$rows=0;$docnt = false;
46 GLOBAL $gSQLMaxRows,$gSQLBlockRows,$ADODB_ROUND;
49 printf(ADODB_BAD_RS
,'rs2html');
53 if (! $ztabhtml) $ztabhtml = "BORDER='1' WIDTH='98%'";
56 $ncols = $rs->FieldCount();
57 $hdr = "<TABLE COLS=$ncols $ztabhtml><tr>\n\n";
58 for ($i=0; $i < $ncols; $i++
) {
59 $field = $rs->FetchField($i);
61 if ($zheaderarray) $fname = $zheaderarray[$i];
62 else $fname = htmlspecialchars($field->name
);
63 $typearr[$i] = $rs->MetaType($field->type
,$field->max_length
);
64 //print " $field->name $field->type $typearr[$i] ";
66 $fname = 'Field '.($i+
1);
69 if (strlen($fname)==0) $fname = ' ';
70 $hdr .= "<TH>$fname</TH>";
73 if ($echo) print $hdr."\n\n";
76 // smart algorithm - handles ADODB_FETCH_MODE's correctly by probing...
77 $numoffset = isset($rs->fields
[0]) ||
isset($rs->fields
[1]) ||
isset($rs->fields
[2]);
80 $s .= "<TR valign=top>\n";
82 for ($i=0; $i < $ncols; $i++
) {
83 if ($i===0) $v=($numoffset) ?
$rs->fields
[0] : reset($rs->fields
);
84 else $v = ($numoffset) ?
$rs->fields
[$i] : next($rs->fields
);
89 if (strpos($v,':') !== false);
92 $s .= "<TD> </TD>\n";
94 $s .= " <TD>".$rs->UserDate($v,"D d, M Y") ."</TD>\n";
99 if (empty($v)) $s .= "<TD> </TD>\n";
100 else $s .= " <TD>".$rs->UserTimeStamp($v,"D d, M Y, H:i:s") ."</TD>\n";
104 if (abs(abs($v) - round($v,0)) < 0.00000001)
107 $v = round($v,$ADODB_ROUND);
109 $vv = stripslashes((trim($v)));
110 if (strlen($vv) == 0) $vv .= ' ';
111 $s .= " <TD align=right>".$vv ."</TD>\n";
116 if (substr($v,8,2)=="BM" ) $v = substr($v,8);
117 $mtime = substr(str_replace(' ','_',microtime()),2);
118 $tmpname = "tmp/".uniqid($mtime).getmypid();
119 $fd = @fopen($tmpname,'a');
123 if (!function_exists ("mime_content_type")) {
124 function mime_content_type ($file) {
125 return exec("file -bi ".escapeshellarg($file));
128 $t = mime_content_type($tmpname);
129 $s .= (substr($t,0,5)=="image") ? " <td><img src='$tmpname' alt='$t'></td>\\n" : " <td><a
130 href='$tmpname'>$t</a></td>\\n";
135 if ($htmlspecialchars) $v = htmlspecialchars(trim($v));
137 if (strlen($v) == 0) $v = ' ';
138 $s .= " <TD>". str_replace("\n",'<br>',stripslashes($v)) ."</TD>\n";
145 if ($rows >= $gSQLMaxRows) {
146 $rows = "<p>Truncated at $gSQLMaxRows</p>";
152 // additional EOF check to prevent a widow header
153 if (!$rs->EOF
&& $rows %
$gSQLBlockRows == 0) {
155 //if (connection_aborted()) break;// not needed as PHP aborts script, unlike ASP
156 if ($echo) print $s . "</TABLE>\n\n";
157 else $html .= $s ."</TABLE>\n\n";
162 if ($echo) print $s."</TABLE>\n\n";
163 else $html .= $s."</TABLE>\n\n";
165 if ($docnt) if ($echo) print "<H2>".$rows." Rows</H2>";
167 return ($echo) ?
$rows : $html;
170 // pass in 2 dimensional array
171 function arr2html(&$arr,$ztabhtml='',$zheaderarray='')
173 if (!$ztabhtml) $ztabhtml = 'BORDER=1';
175 $s = "<TABLE $ztabhtml>";//';print_r($arr);
179 for ($i=0; $i<sizeof($zheaderarray); $i++
) {
180 $s .= " <TH>{$zheaderarray[$i]}</TH>\n";
185 for ($i=0; $i<sizeof($arr); $i++
) {
189 for ($j=0; $j<sizeof($a); $j++
) {
191 if (empty($val)) $val = ' ';
192 $s .= " <TD>$val</TD>\n";
195 $s .= ' <TD>'.$a."</TD>\n";
196 } else $s .= " <TD> </TD>\n";