Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

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