3 V4.20 22 Feb 2004 (c) 2000-2004 John Lim (jlim@natsoft.com.my). All rights reserved.
4 Released under both BSD license and Lesser GPL library license.
5 Whenever there is any discrepancy between the two licenses,
6 the BSD license will take precedence.
8 Some pretty-printing by Chris Oxenreider <oxenreid@state.net>
11 // specific code for tohtml
12 GLOBAL $gSQLMaxRows,$gSQLBlockRows;
14 $gSQLMaxRows = 1000; // max no of rows to download
15 $gSQLBlockRows=20; // max no of rows per table block
17 // RecordSet to HTML Table
18 //------------------------------------------------------------
19 // Convert a recordset to a html table. Multiple tables are generated
20 // if the number of rows is > $gSQLBlockRows. This is because
21 // web browsers normally require the whole table to be downloaded
22 // before it can be rendered, so we break the output into several
23 // smaller faster rendering tables.
26 // $ztabhtml: the table tag attributes (optional)
27 // $zheaderarray: contains the replacement strings for the headers (optional)
30 // include('adodb.inc.php');
31 // $db = ADONewConnection('mysql');
32 // $db->Connect('mysql','userid','password','database');
33 // $rs = $db->Execute('select col1,col2,col3 from table');
34 // rs2html($rs, 'BORDER=2', array('Title1', 'Title2', 'Title3'));
37 // RETURNS: number of rows displayed
38 function rs2html(&$rs,$ztabhtml=false,$zheaderarray=false,$htmlspecialchars=true,$echo = true)
40 $s ='';$rows=0;$docnt = false;
41 GLOBAL $gSQLMaxRows,$gSQLBlockRows;
44 printf(ADODB_BAD_RS
,'rs2html');
48 if (! $ztabhtml) $ztabhtml = "BORDER='1' WIDTH='98%'";
51 $ncols = $rs->FieldCount();
52 $hdr = "<TABLE COLS=$ncols $ztabhtml><tr>\n\n";
53 for ($i=0; $i < $ncols; $i++
) {
54 $field = $rs->FetchField($i);
55 if ($zheaderarray) $fname = $zheaderarray[$i];
56 else $fname = htmlspecialchars($field->name
);
57 $typearr[$i] = $rs->MetaType($field->type
,$field->max_length
);
58 //print " $field->name $field->type $typearr[$i] ";
60 if (strlen($fname)==0) $fname = ' ';
61 $hdr .= "<TH>$fname</TH>";
64 if ($echo) print $hdr."\n\n";
67 // smart algorithm - handles ADODB_FETCH_MODE's correctly by probing...
68 $numoffset = isset($rs->fields
[0]) ||
isset($rs->fields
[1]) ||
isset($rs->fields
[2]);
71 $s .= "<TR valign=top>\n";
73 for ($i=0; $i < $ncols; $i++
) {
74 if ($i===0) $v=($numoffset) ?
$rs->fields
[0] : reset($rs->fields
);
75 else $v = ($numoffset) ?
$rs->fields
[$i] : next($rs->fields
);
80 $s .= " <TD>".$rs->UserTimeStamp($v,"D d, M Y, h:i:s") ." </TD>\n";
83 $s .= " <TD>".$rs->UserDate($v,"D d, M Y") ." </TD>\n";
87 $s .= " <TD align=right>".stripslashes((trim($v))) ." </TD>\n";
91 if ($htmlspecialchars) $v = htmlspecialchars(trim($v));
93 if (strlen($v) == 0) $v = ' ';
94 $s .= " <TD>". str_replace("\n",'<br>',stripslashes($v)) ."</TD>\n";
101 if ($rows >= $gSQLMaxRows) {
102 $rows = "<p>Truncated at $gSQLMaxRows</p>";
108 // additional EOF check to prevent a widow header
109 if (!$rs->EOF
&& $rows %
$gSQLBlockRows == 0) {
111 //if (connection_aborted()) break;// not needed as PHP aborts script, unlike ASP
112 if ($echo) print $s . "</TABLE>\n\n";
113 else $html .= $s ."</TABLE>\n\n";
118 if ($echo) print $s."</TABLE>\n\n";
119 else $html .= $s."</TABLE>\n\n";
121 if ($docnt) if ($echo) print "<H2>".$rows." Rows</H2>";
123 return ($echo) ?
$rows : $html;
126 // pass in 2 dimensional array
127 function arr2html(&$arr,$ztabhtml='',$zheaderarray='')
129 if (!$ztabhtml) $ztabhtml = 'BORDER=1';
131 $s = "<TABLE $ztabhtml>";//';print_r($arr);
135 for ($i=0; $i<sizeof($zheaderarray); $i++
) {
136 $s .= " <TH>{$zheaderarray[$i]}</TH>\n";
141 for ($i=0; $i<sizeof($arr); $i++
) {
145 for ($j=0; $j<sizeof($a); $j++
) {
147 if (empty($val)) $val = ' ';
148 $s .= " <TD>$val</TD>\n";
151 $s .= ' <TD>'.$a."</TD>\n";
152 } else $s .= " <TD> </TD>\n";