added ending dates of service
[openemr.git] / library / adodb / tohtml.inc.php
blobdac81ddb756b874cf8777bd4673c959ea2e60c01
1 <?php
2 /*
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>
9 */
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.
25 // $rs: the recordset
26 // $ztabhtml: the table tag attributes (optional)
27 // $zheaderarray: contains the replacement strings for the headers (optional)
29 // USAGE:
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'));
35 // $rs->Close();
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;
43 if (!$rs) {
44 printf(ADODB_BAD_RS,'rs2html');
45 return false;
48 if (! $ztabhtml) $ztabhtml = "BORDER='1' WIDTH='98%'";
49 //else $docnt = true;
50 $typearr = array();
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 = '&nbsp;';
61 $hdr .= "<TH>$fname</TH>";
63 $hdr .= "\n</tr>";
64 if ($echo) print $hdr."\n\n";
65 else $html = $hdr;
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]);
69 while (!$rs->EOF) {
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);
77 $type = $typearr[$i];
78 switch($type) {
79 case 'T':
80 $s .= " <TD>".$rs->UserTimeStamp($v,"D d, M Y, h:i:s") ."&nbsp;</TD>\n";
81 break;
82 case 'D':
83 $s .= " <TD>".$rs->UserDate($v,"D d, M Y") ."&nbsp;</TD>\n";
84 break;
85 case 'I':
86 case 'N':
87 $s .= " <TD align=right>".stripslashes((trim($v))) ."&nbsp;</TD>\n";
89 break;
90 default:
91 if ($htmlspecialchars) $v = htmlspecialchars(trim($v));
92 $v = trim($v);
93 if (strlen($v) == 0) $v = '&nbsp;';
94 $s .= " <TD>". str_replace("\n",'<br>',stripslashes($v)) ."</TD>\n";
97 } // for
98 $s .= "</TR>\n\n";
100 $rows += 1;
101 if ($rows >= $gSQLMaxRows) {
102 $rows = "<p>Truncated at $gSQLMaxRows</p>";
103 break;
104 } // switch
106 $rs->MoveNext();
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";
114 $s = $hdr;
116 } // while
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);
133 if ($zheaderarray) {
134 $s .= '<TR>';
135 for ($i=0; $i<sizeof($zheaderarray); $i++) {
136 $s .= " <TH>{$zheaderarray[$i]}</TH>\n";
138 $s .= "\n</TR>";
141 for ($i=0; $i<sizeof($arr); $i++) {
142 $s .= '<TR>';
143 $a = &$arr[$i];
144 if (is_array($a))
145 for ($j=0; $j<sizeof($a); $j++) {
146 $val = $a[$j];
147 if (empty($val)) $val = '&nbsp;';
148 $s .= " <TD>$val</TD>\n";
150 else if ($a) {
151 $s .= ' <TD>'.$a."</TD>\n";
152 } else $s .= " <TD>&nbsp;</TD>\n";
153 $s .= "\n</TR>\n";
155 $s .= '</TABLE>';
156 print $s;