1 | <?php |
---|
2 | /* |
---|
3 | V4.51 29 July 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. |
---|
7 | |
---|
8 | Some pretty-printing by Chris Oxenreider <oxenreid@state.net> |
---|
9 | */ |
---|
10 | |
---|
11 | // specific code for tohtml |
---|
12 | GLOBAL $gSQLMaxRows,$gSQLBlockRows; |
---|
13 | |
---|
14 | $gSQLMaxRows = 1000; // max no of rows to download |
---|
15 | $gSQLBlockRows=20; // max no of rows per table block |
---|
16 | |
---|
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. |
---|
24 | // |
---|
25 | // $rs: the recordset |
---|
26 | // $ztabhtml: the table tag attributes (optional) |
---|
27 | // $zheaderarray: contains the replacement strings for the headers (optional) |
---|
28 | // |
---|
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(); |
---|
36 | // |
---|
37 | // RETURNS: number of rows displayed |
---|
38 | function rs2html(&$rs,$ztabhtml=false,$zheaderarray=false,$htmlspecialchars=true,$echo = true) |
---|
39 | { |
---|
40 | $s ='';$rows=0;$docnt = false; |
---|
41 | GLOBAL $gSQLMaxRows,$gSQLBlockRows; |
---|
42 | |
---|
43 | if (!$rs) { |
---|
44 | printf(ADODB_BAD_RS,'rs2html'); |
---|
45 | return false; |
---|
46 | } |
---|
47 | |
---|
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] "; |
---|
59 | |
---|
60 | if (strlen($fname)==0) $fname = ' '; |
---|
61 | $hdr .= "<TH>$fname</TH>"; |
---|
62 | } |
---|
63 | $hdr .= "\n</tr>"; |
---|
64 | if ($echo) print $hdr."\n\n"; |
---|
65 | else $html = $hdr; |
---|
66 | |
---|
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) { |
---|
70 | |
---|
71 | $s .= "<TR valign=top>\n"; |
---|
72 | |
---|
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); |
---|
76 | |
---|
77 | $type = $typearr[$i]; |
---|
78 | switch($type) { |
---|
79 | case 'D': |
---|
80 | if (!strpos($v,':')) { |
---|
81 | $s .= " <TD>".$rs->UserDate($v,"D d, M Y") ." </TD>\n"; |
---|
82 | break; |
---|
83 | } |
---|
84 | case 'T': |
---|
85 | $s .= " <TD>".$rs->UserTimeStamp($v,"D d, M Y, h:i:s") ." </TD>\n"; |
---|
86 | break; |
---|
87 | case 'I': |
---|
88 | case 'N': |
---|
89 | $s .= " <TD align=right>".stripslashes((trim($v))) ." </TD>\n"; |
---|
90 | |
---|
91 | break; |
---|
92 | /* |
---|
93 | case 'B': |
---|
94 | if (substr($v,8,2)=="BM" ) $v = substr($v,8); |
---|
95 | $mtime = substr(str_replace(' ','_',microtime()),2); |
---|
96 | $tmpname = "tmp/".uniqid($mtime).getmypid(); |
---|
97 | $fd = @fopen($tmpname,'a'); |
---|
98 | @ftruncate($fd,0); |
---|
99 | @fwrite($fd,$v); |
---|
100 | @fclose($fd); |
---|
101 | if (!function_exists ("mime_content_type")) { |
---|
102 | function mime_content_type ($file) { |
---|
103 | return exec("file -bi ".escapeshellarg($file)); |
---|
104 | } |
---|
105 | } |
---|
106 | $t = mime_content_type($tmpname); |
---|
107 | $s .= (substr($t,0,5)=="image") ? " <td><img src='$tmpname' alt='$t'></td>\\n" : " <td><a |
---|
108 | href='$tmpname'>$t</a></td>\\n"; |
---|
109 | break; |
---|
110 | */ |
---|
111 | |
---|
112 | default: |
---|
113 | if ($htmlspecialchars) $v = htmlspecialchars(trim($v)); |
---|
114 | $v = trim($v); |
---|
115 | if (strlen($v) == 0) $v = ' '; |
---|
116 | $s .= " <TD>". str_replace("\n",'<br>',stripslashes($v)) ."</TD>\n"; |
---|
117 | |
---|
118 | } |
---|
119 | } // for |
---|
120 | $s .= "</TR>\n\n"; |
---|
121 | |
---|
122 | $rows += 1; |
---|
123 | if ($rows >= $gSQLMaxRows) { |
---|
124 | $rows = "<p>Truncated at $gSQLMaxRows</p>"; |
---|
125 | break; |
---|
126 | } // switch |
---|
127 | |
---|
128 | $rs->MoveNext(); |
---|
129 | |
---|
130 | // additional EOF check to prevent a widow header |
---|
131 | if (!$rs->EOF && $rows % $gSQLBlockRows == 0) { |
---|
132 | |
---|
133 | //if (connection_aborted()) break;// not needed as PHP aborts script, unlike ASP |
---|
134 | if ($echo) print $s . "</TABLE>\n\n"; |
---|
135 | else $html .= $s ."</TABLE>\n\n"; |
---|
136 | $s = $hdr; |
---|
137 | } |
---|
138 | } // while |
---|
139 | |
---|
140 | if ($echo) print $s."</TABLE>\n\n"; |
---|
141 | else $html .= $s."</TABLE>\n\n"; |
---|
142 | |
---|
143 | if ($docnt) if ($echo) print "<H2>".$rows." Rows</H2>"; |
---|
144 | |
---|
145 | return ($echo) ? $rows : $html; |
---|
146 | } |
---|
147 | |
---|
148 | // pass in 2 dimensional array |
---|
149 | function arr2html(&$arr,$ztabhtml='',$zheaderarray='') |
---|
150 | { |
---|
151 | if (!$ztabhtml) $ztabhtml = 'BORDER=1'; |
---|
152 | |
---|
153 | $s = "<TABLE $ztabhtml>";//';print_r($arr); |
---|
154 | |
---|
155 | if ($zheaderarray) { |
---|
156 | $s .= '<TR>'; |
---|
157 | for ($i=0; $i<sizeof($zheaderarray); $i++) { |
---|
158 | $s .= " <TH>{$zheaderarray[$i]}</TH>\n"; |
---|
159 | } |
---|
160 | $s .= "\n</TR>"; |
---|
161 | } |
---|
162 | |
---|
163 | for ($i=0; $i<sizeof($arr); $i++) { |
---|
164 | $s .= '<TR>'; |
---|
165 | $a = &$arr[$i]; |
---|
166 | if (is_array($a)) |
---|
167 | for ($j=0; $j<sizeof($a); $j++) { |
---|
168 | $val = $a[$j]; |
---|
169 | if (empty($val)) $val = ' '; |
---|
170 | $s .= " <TD>$val</TD>\n"; |
---|
171 | } |
---|
172 | else if ($a) { |
---|
173 | $s .= ' <TD>'.$a."</TD>\n"; |
---|
174 | } else $s .= " <TD> </TD>\n"; |
---|
175 | $s .= "\n</TR>\n"; |
---|
176 | } |
---|
177 | $s .= '</TABLE>'; |
---|
178 | print $s; |
---|
179 | } |
---|
180 | |
---|
181 | ?> |
---|