| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | V4.93 10 Oct 2006 (c) 2000-2009 John Lim (jlim#natsoft.com). 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,$ADODB_ROUND; |
|---|
| 13 | |
|---|
| 14 | $ADODB_ROUND=4; // rounding |
|---|
| 15 | $gSQLMaxRows = 1000; // max no of rows to download |
|---|
| 16 | $gSQLBlockRows=20; // max no of rows per table block |
|---|
| 17 | |
|---|
| 18 | // RecordSet to HTML Table |
|---|
| 19 | //------------------------------------------------------------ |
|---|
| 20 | // Convert a recordset to a html table. Multiple tables are generated |
|---|
| 21 | // if the number of rows is > $gSQLBlockRows. This is because |
|---|
| 22 | // web browsers normally require the whole table to be downloaded |
|---|
| 23 | // before it can be rendered, so we break the output into several |
|---|
| 24 | // smaller faster rendering tables. |
|---|
| 25 | // |
|---|
| 26 | // $rs: the recordset |
|---|
| 27 | // $ztabhtml: the table tag attributes (optional) |
|---|
| 28 | // $zheaderarray: contains the replacement strings for the headers (optional) |
|---|
| 29 | // |
|---|
| 30 | // USAGE: |
|---|
| 31 | // include('adodb.inc.php'); |
|---|
| 32 | // $db = ADONewConnection('mysql'); |
|---|
| 33 | // $db->Connect('mysql','userid','password','database'); |
|---|
| 34 | // $rs = $db->Execute('select col1,col2,col3 from table'); |
|---|
| 35 | // rs2html($rs, 'BORDER=2', array('Title1', 'Title2', 'Title3')); |
|---|
| 36 | // $rs->Close(); |
|---|
| 37 | // |
|---|
| 38 | // RETURNS: number of rows displayed |
|---|
| 39 | |
|---|
| 40 | function rs2html(&$rs,$ztabhtml=false,$zheaderarray=false,$htmlspecialchars=true,$echo = true) |
|---|
| 41 | { |
|---|
| 42 | $s ='';$rows=0;$docnt = false; |
|---|
| 43 | GLOBAL $gSQLMaxRows,$gSQLBlockRows,$ADODB_ROUND; |
|---|
| 44 | |
|---|
| 45 | if (!$rs) { |
|---|
| 46 | printf(ADODB_BAD_RS,'rs2html'); |
|---|
| 47 | return false; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | if (! $ztabhtml) $ztabhtml = "BORDER='1' WIDTH='98%'"; |
|---|
| 51 | //else $docnt = true; |
|---|
| 52 | $typearr = array(); |
|---|
| 53 | $ncols = $rs->FieldCount(); |
|---|
| 54 | $hdr = "<TABLE COLS=$ncols $ztabhtml><tr>\n\n"; |
|---|
| 55 | for ($i=0; $i < $ncols; $i++) { |
|---|
| 56 | $field = $rs->FetchField($i); |
|---|
| 57 | if ($field) { |
|---|
| 58 | if ($zheaderarray) $fname = $zheaderarray[$i]; |
|---|
| 59 | else $fname = htmlspecialchars($field->name); |
|---|
| 60 | $typearr[$i] = $rs->MetaType($field->type,$field->max_length); |
|---|
| 61 | //print " $field->name $field->type $typearr[$i] "; |
|---|
| 62 | } else { |
|---|
| 63 | $fname = 'Field '.($i+1); |
|---|
| 64 | $typearr[$i] = 'C'; |
|---|
| 65 | } |
|---|
| 66 | if (strlen($fname)==0) $fname = ' '; |
|---|
| 67 | $hdr .= "<TH>$fname</TH>"; |
|---|
| 68 | } |
|---|
| 69 | $hdr .= "\n</tr>"; |
|---|
| 70 | if ($echo) print $hdr."\n\n"; |
|---|
| 71 | else $html = $hdr; |
|---|
| 72 | |
|---|
| 73 | // smart algorithm - handles ADODB_FETCH_MODE's correctly by probing... |
|---|
| 74 | $numoffset = isset($rs->fields[0]) ||isset($rs->fields[1]) || isset($rs->fields[2]); |
|---|
| 75 | while (!$rs->EOF) { |
|---|
| 76 | |
|---|
| 77 | $s .= "<TR valign=top>\n"; |
|---|
| 78 | |
|---|
| 79 | for ($i=0; $i < $ncols; $i++) { |
|---|
| 80 | if ($i===0) $v=($numoffset) ? $rs->fields[0] : reset($rs->fields); |
|---|
| 81 | else $v = ($numoffset) ? $rs->fields[$i] : next($rs->fields); |
|---|
| 82 | |
|---|
| 83 | $type = $typearr[$i]; |
|---|
| 84 | switch($type) { |
|---|
| 85 | case 'D': |
|---|
| 86 | if (strpos($v,':') !== false); |
|---|
| 87 | else { |
|---|
| 88 | if (empty($v)) { |
|---|
| 89 | $s .= "<TD> </TD>\n"; |
|---|
| 90 | } else { |
|---|
| 91 | $s .= " <TD>".$rs->UserDate($v,"D d, M Y") ."</TD>\n"; |
|---|
| 92 | } |
|---|
| 93 | break; |
|---|
| 94 | } |
|---|
| 95 | case 'T': |
|---|
| 96 | if (empty($v)) $s .= "<TD> </TD>\n"; |
|---|
| 97 | else $s .= " <TD>".$rs->UserTimeStamp($v,"D d, M Y, h:i:s") ." </TD>\n"; |
|---|
| 98 | break; |
|---|
| 99 | |
|---|
| 100 | case 'N': |
|---|
| 101 | if (abs(abs($v) - round($v,0)) < 0.00000001) |
|---|
| 102 | $v = round($v); |
|---|
| 103 | else |
|---|
| 104 | $v = round($v,$ADODB_ROUND); |
|---|
| 105 | case 'I': |
|---|
| 106 | $s .= " <TD align=right>".stripslashes((trim($v))) ." </TD>\n"; |
|---|
| 107 | |
|---|
| 108 | break; |
|---|
| 109 | /* |
|---|
| 110 | case 'B': |
|---|
| 111 | if (substr($v,8,2)=="BM" ) $v = substr($v,8); |
|---|
| 112 | $mtime = substr(str_replace(' ','_',microtime()),2); |
|---|
| 113 | $tmpname = "tmp/".uniqid($mtime).getmypid(); |
|---|
| 114 | $fd = @fopen($tmpname,'a'); |
|---|
| 115 | @ftruncate($fd,0); |
|---|
| 116 | @fwrite($fd,$v); |
|---|
| 117 | @fclose($fd); |
|---|
| 118 | if (!function_exists ("mime_content_type")) { |
|---|
| 119 | function mime_content_type ($file) { |
|---|
| 120 | return exec("file -bi ".escapeshellarg($file)); |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | $t = mime_content_type($tmpname); |
|---|
| 124 | $s .= (substr($t,0,5)=="image") ? " <td><img src='$tmpname' alt='$t'></td>\\n" : " <td><a |
|---|
| 125 | href='$tmpname'>$t</a></td>\\n"; |
|---|
| 126 | break; |
|---|
| 127 | */ |
|---|
| 128 | |
|---|
| 129 | default: |
|---|
| 130 | if ($htmlspecialchars) $v = htmlspecialchars(trim($v)); |
|---|
| 131 | $v = trim($v); |
|---|
| 132 | if (strlen($v) == 0) $v = ' '; |
|---|
| 133 | $s .= " <TD>". str_replace("\n",'<br>',stripslashes($v)) ."</TD>\n"; |
|---|
| 134 | |
|---|
| 135 | } |
|---|
| 136 | } // for |
|---|
| 137 | $s .= "</TR>\n\n"; |
|---|
| 138 | |
|---|
| 139 | $rows += 1; |
|---|
| 140 | if ($rows >= $gSQLMaxRows) { |
|---|
| 141 | $rows = "<p>Truncated at $gSQLMaxRows</p>"; |
|---|
| 142 | break; |
|---|
| 143 | } // switch |
|---|
| 144 | |
|---|
| 145 | $rs->MoveNext(); |
|---|
| 146 | |
|---|
| 147 | // additional EOF check to prevent a widow header |
|---|
| 148 | if (!$rs->EOF && $rows % $gSQLBlockRows == 0) { |
|---|
| 149 | |
|---|
| 150 | //if (connection_aborted()) break;// not needed as PHP aborts script, unlike ASP |
|---|
| 151 | if ($echo) print $s . "</TABLE>\n\n"; |
|---|
| 152 | else $html .= $s ."</TABLE>\n\n"; |
|---|
| 153 | $s = $hdr; |
|---|
| 154 | } |
|---|
| 155 | } // while |
|---|
| 156 | |
|---|
| 157 | if ($echo) print $s."</TABLE>\n\n"; |
|---|
| 158 | else $html .= $s."</TABLE>\n\n"; |
|---|
| 159 | |
|---|
| 160 | if ($docnt) if ($echo) print "<H2>".$rows." Rows</H2>"; |
|---|
| 161 | |
|---|
| 162 | return ($echo) ? $rows : $html; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | // pass in 2 dimensional array |
|---|
| 166 | function arr2html(&$arr,$ztabhtml='',$zheaderarray='') |
|---|
| 167 | { |
|---|
| 168 | if (!$ztabhtml) $ztabhtml = 'BORDER=1'; |
|---|
| 169 | |
|---|
| 170 | $s = "<TABLE $ztabhtml>";//';print_r($arr); |
|---|
| 171 | |
|---|
| 172 | if ($zheaderarray) { |
|---|
| 173 | $s .= '<TR>'; |
|---|
| 174 | for ($i=0; $i<sizeof($zheaderarray); $i++) { |
|---|
| 175 | $s .= " <TH>{$zheaderarray[$i]}</TH>\n"; |
|---|
| 176 | } |
|---|
| 177 | $s .= "\n</TR>"; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | for ($i=0; $i<sizeof($arr); $i++) { |
|---|
| 181 | $s .= '<TR>'; |
|---|
| 182 | $a = &$arr[$i]; |
|---|
| 183 | if (is_array($a)) |
|---|
| 184 | for ($j=0; $j<sizeof($a); $j++) { |
|---|
| 185 | $val = $a[$j]; |
|---|
| 186 | if (empty($val)) $val = ' '; |
|---|
| 187 | $s .= " <TD>$val</TD>\n"; |
|---|
| 188 | } |
|---|
| 189 | else if ($a) { |
|---|
| 190 | $s .= ' <TD>'.$a."</TD>\n"; |
|---|
| 191 | } else $s .= " <TD> </TD>\n"; |
|---|
| 192 | $s .= "\n</TR>\n"; |
|---|
| 193 | } |
|---|
| 194 | $s .= '</TABLE>'; |
|---|
| 195 | print $s; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | ?> |
|---|