PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/mcmis/reportico/adodb/tohtml.inc.php

http://mcmis.googlecode.com/
PHP | 195 lines | 113 code | 27 blank | 55 comment | 44 complexity | 61cf1142e73f7ae3fd7b8d50649948eb MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. V4.93 10 Oct 2006 (c) 2000-2007 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. Some pretty-printing by Chris Oxenreider <oxenreid@state.net>
  8. */
  9. // specific code for tohtml
  10. GLOBAL $gSQLMaxRows,$gSQLBlockRows,$ADODB_ROUND;
  11. $ADODB_ROUND=4; // rounding
  12. $gSQLMaxRows = 1000; // max no of rows to download
  13. $gSQLBlockRows=20; // max no of rows per table block
  14. // RecordSet to HTML Table
  15. //------------------------------------------------------------
  16. // Convert a recordset to a html table. Multiple tables are generated
  17. // if the number of rows is > $gSQLBlockRows. This is because
  18. // web browsers normally require the whole table to be downloaded
  19. // before it can be rendered, so we break the output into several
  20. // smaller faster rendering tables.
  21. //
  22. // $rs: the recordset
  23. // $ztabhtml: the table tag attributes (optional)
  24. // $zheaderarray: contains the replacement strings for the headers (optional)
  25. //
  26. // USAGE:
  27. // include('adodb.inc.php');
  28. // $db = ADONewConnection('mysql');
  29. // $db->Connect('mysql','userid','password','database');
  30. // $rs = $db->Execute('select col1,col2,col3 from table');
  31. // rs2html($rs, 'BORDER=2', array('Title1', 'Title2', 'Title3'));
  32. // $rs->Close();
  33. //
  34. // RETURNS: number of rows displayed
  35. function rs2html(&$rs,$ztabhtml=false,$zheaderarray=false,$htmlspecialchars=true,$echo = true)
  36. {
  37. $s ='';$rows=0;$docnt = false;
  38. GLOBAL $gSQLMaxRows,$gSQLBlockRows,$ADODB_ROUND;
  39. if (!$rs) {
  40. printf(ADODB_BAD_RS,'rs2html');
  41. return false;
  42. }
  43. if (! $ztabhtml) $ztabhtml = "BORDER='1' WIDTH='98%'";
  44. //else $docnt = true;
  45. $typearr = array();
  46. $ncols = $rs->FieldCount();
  47. $hdr = "<TABLE COLS=$ncols $ztabhtml><tr>\n\n";
  48. for ($i=0; $i < $ncols; $i++) {
  49. $field = $rs->FetchField($i);
  50. if ($field) {
  51. if ($zheaderarray) $fname = $zheaderarray[$i];
  52. else $fname = htmlspecialchars($field->name);
  53. $typearr[$i] = $rs->MetaType($field->type,$field->max_length);
  54. //print " $field->name $field->type $typearr[$i] ";
  55. } else {
  56. $fname = 'Field '.($i+1);
  57. $typearr[$i] = 'C';
  58. }
  59. if (strlen($fname)==0) $fname = '&nbsp;';
  60. $hdr .= "<TH>$fname</TH>";
  61. }
  62. $hdr .= "\n</tr>";
  63. if ($echo) print $hdr."\n\n";
  64. else $html = $hdr;
  65. // smart algorithm - handles ADODB_FETCH_MODE's correctly by probing...
  66. $numoffset = isset($rs->fields[0]) ||isset($rs->fields[1]) || isset($rs->fields[2]);
  67. while (!$rs->EOF) {
  68. $s .= "<TR valign=top>\n";
  69. for ($i=0; $i < $ncols; $i++) {
  70. if ($i===0) $v=($numoffset) ? $rs->fields[0] : reset($rs->fields);
  71. else $v = ($numoffset) ? $rs->fields[$i] : next($rs->fields);
  72. $type = $typearr[$i];
  73. switch($type) {
  74. case 'D':
  75. if (empty($v)) $s .= "<TD> &nbsp; </TD>\n";
  76. else if (!strpos($v,':')) {
  77. $s .= " <TD>".$rs->UserDate($v,"D d, M Y") ."&nbsp;</TD>\n";
  78. }
  79. break;
  80. case 'T':
  81. if (empty($v)) $s .= "<TD> &nbsp; </TD>\n";
  82. else $s .= " <TD>".$rs->UserTimeStamp($v,"D d, M Y, h:i:s") ."&nbsp;</TD>\n";
  83. break;
  84. case 'N':
  85. if (abs(abs($v) - round($v,0)) < 0.00000001)
  86. $v = round($v);
  87. else
  88. $v = round($v,$ADODB_ROUND);
  89. case 'I':
  90. $s .= " <TD align=right>".stripslashes((trim($v))) ."&nbsp;</TD>\n";
  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. default:
  112. if ($htmlspecialchars) $v = htmlspecialchars(trim($v));
  113. $v = trim($v);
  114. if (strlen($v) == 0) $v = '&nbsp;';
  115. $s .= " <TD>". str_replace("\n",'<br>',stripslashes($v)) ."</TD>\n";
  116. }
  117. } // for
  118. $s .= "</TR>\n\n";
  119. $rows += 1;
  120. if ($rows >= $gSQLMaxRows) {
  121. $rows = "<p>Truncated at $gSQLMaxRows</p>";
  122. break;
  123. } // switch
  124. $rs->MoveNext();
  125. // additional EOF check to prevent a widow header
  126. if (!$rs->EOF && $rows % $gSQLBlockRows == 0) {
  127. //if (connection_aborted()) break;// not needed as PHP aborts script, unlike ASP
  128. if ($echo) print $s . "</TABLE>\n\n";
  129. else $html .= $s ."</TABLE>\n\n";
  130. $s = $hdr;
  131. }
  132. } // while
  133. if ($echo) print $s."</TABLE>\n\n";
  134. else $html .= $s."</TABLE>\n\n";
  135. if ($docnt) if ($echo) print "<H2>".$rows." Rows</H2>";
  136. return ($echo) ? $rows : $html;
  137. }
  138. // pass in 2 dimensional array
  139. function arr2html(&$arr,$ztabhtml='',$zheaderarray='')
  140. {
  141. if (!$ztabhtml) $ztabhtml = 'BORDER=1';
  142. $s = "<TABLE $ztabhtml>";//';print_r($arr);
  143. if ($zheaderarray) {
  144. $s .= '<TR>';
  145. for ($i=0; $i<sizeof($zheaderarray); $i++) {
  146. $s .= " <TH>{$zheaderarray[$i]}</TH>\n";
  147. }
  148. $s .= "\n</TR>";
  149. }
  150. for ($i=0; $i<sizeof($arr); $i++) {
  151. $s .= '<TR>';
  152. $a = $arr[$i];
  153. if (is_array($a))
  154. for ($j=0; $j<sizeof($a); $j++) {
  155. $val = $a[$j];
  156. if (empty($val)) $val = '&nbsp;';
  157. $s .= " <TD>$val</TD>\n";
  158. }
  159. else if ($a) {
  160. $s .= ' <TD>'.$a."</TD>\n";
  161. } else $s .= " <TD>&nbsp;</TD>\n";
  162. $s .= "\n</TR>\n";
  163. }
  164. $s .= '</TABLE>';
  165. print $s;
  166. }
  167. ?>