PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/concreteOLD/libraries/3rdparty/adodb/tohtml.inc.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 201 lines | 119 code | 27 blank | 55 comment | 47 complexity | d156b5852e9497d028319a96f6181c6a MD5 | raw file
  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. 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 (strpos($v,':') !== false);
  76. else {
  77. if (empty($v)) {
  78. $s .= "<TD> &nbsp; </TD>\n";
  79. } else {
  80. $s .= " <TD>".$rs->UserDate($v,"D d, M Y") ."</TD>\n";
  81. }
  82. break;
  83. }
  84. case 'T':
  85. if (empty($v)) $s .= "<TD> &nbsp; </TD>\n";
  86. else $s .= " <TD>".$rs->UserTimeStamp($v,"D d, M Y, H:i:s") ."</TD>\n";
  87. break;
  88. case 'N':
  89. if (abs(abs($v) - round($v,0)) < 0.00000001)
  90. $v = round($v);
  91. else
  92. $v = round($v,$ADODB_ROUND);
  93. case 'I':
  94. $vv = stripslashes((trim($v)));
  95. if (strlen($vv) == 0) $vv .= '&nbsp;';
  96. $s .= " <TD align=right>".$vv ."</TD>\n";
  97. break;
  98. /*
  99. case 'B':
  100. if (substr($v,8,2)=="BM" ) $v = substr($v,8);
  101. $mtime = substr(str_replace(' ','_',microtime()),2);
  102. $tmpname = "tmp/".uniqid($mtime).getmypid();
  103. $fd = @fopen($tmpname,'a');
  104. @ftruncate($fd,0);
  105. @fwrite($fd,$v);
  106. @fclose($fd);
  107. if (!function_exists ("mime_content_type")) {
  108. function mime_content_type ($file) {
  109. return exec("file -bi ".escapeshellarg($file));
  110. }
  111. }
  112. $t = mime_content_type($tmpname);
  113. $s .= (substr($t,0,5)=="image") ? " <td><img src='$tmpname' alt='$t'></td>\\n" : " <td><a
  114. href='$tmpname'>$t</a></td>\\n";
  115. break;
  116. */
  117. default:
  118. if ($htmlspecialchars) $v = htmlspecialchars(trim($v));
  119. $v = trim($v);
  120. if (strlen($v) == 0) $v = '&nbsp;';
  121. $s .= " <TD>". str_replace("\n",'<br>',stripslashes($v)) ."</TD>\n";
  122. }
  123. } // for
  124. $s .= "</TR>\n\n";
  125. $rows += 1;
  126. if ($rows >= $gSQLMaxRows) {
  127. $rows = "<p>Truncated at $gSQLMaxRows</p>";
  128. break;
  129. } // switch
  130. $rs->MoveNext();
  131. // additional EOF check to prevent a widow header
  132. if (!$rs->EOF && $rows % $gSQLBlockRows == 0) {
  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. if ($echo) print $s."</TABLE>\n\n";
  140. else $html .= $s."</TABLE>\n\n";
  141. if ($docnt) if ($echo) print "<H2>".$rows." Rows</H2>";
  142. return ($echo) ? $rows : $html;
  143. }
  144. // pass in 2 dimensional array
  145. function arr2html(&$arr,$ztabhtml='',$zheaderarray='')
  146. {
  147. if (!$ztabhtml) $ztabhtml = 'BORDER=1';
  148. $s = "<TABLE $ztabhtml>";//';print_r($arr);
  149. if ($zheaderarray) {
  150. $s .= '<TR>';
  151. for ($i=0; $i<sizeof($zheaderarray); $i++) {
  152. $s .= " <TH>{$zheaderarray[$i]}</TH>\n";
  153. }
  154. $s .= "\n</TR>";
  155. }
  156. for ($i=0; $i<sizeof($arr); $i++) {
  157. $s .= '<TR>';
  158. $a = $arr[$i];
  159. if (is_array($a))
  160. for ($j=0; $j<sizeof($a); $j++) {
  161. $val = $a[$j];
  162. if (empty($val)) $val = '&nbsp;';
  163. $s .= " <TD>$val</TD>\n";
  164. }
  165. else if ($a) {
  166. $s .= ' <TD>'.$a."</TD>\n";
  167. } else $s .= " <TD>&nbsp;</TD>\n";
  168. $s .= "\n</TR>\n";
  169. }
  170. $s .= '</TABLE>';
  171. print $s;
  172. }
  173. ?>