Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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

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