tohtml.inc.php 5.5 KB

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