|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 /* 00003 V4.93 10 Oct 2006 (c) 2000-2011 John Lim (jlim#natsoft.com). All rights reserved. 00004 Released under both BSD license and Lesser GPL library license. 00005 Whenever there is any discrepancy between the two licenses, 00006 the BSD license will take precedence. 00007 00008 Some pretty-printing by Chris Oxenreider <oxenreid@state.net> 00009 */ 00010 00011 // specific code for tohtml 00012 GLOBAL $gSQLMaxRows,$gSQLBlockRows,$ADODB_ROUND; 00013 00014 $ADODB_ROUND=4; // rounding 00015 $gSQLMaxRows = 1000; // max no of rows to download 00016 $gSQLBlockRows=20; // max no of rows per table block 00017 00018 // RecordSet to HTML Table 00019 //------------------------------------------------------------ 00020 // Convert a recordset to a html table. Multiple tables are generated 00021 // if the number of rows is > $gSQLBlockRows. This is because 00022 // web browsers normally require the whole table to be downloaded 00023 // before it can be rendered, so we break the output into several 00024 // smaller faster rendering tables. 00025 // 00026 // $rs: the recordset 00027 // $ztabhtml: the table tag attributes (optional) 00028 // $zheaderarray: contains the replacement strings for the headers (optional) 00029 // 00030 // USAGE: 00031 // include('adodb.inc.php'); 00032 // $db = ADONewConnection('mysql'); 00033 // $db->Connect('mysql','userid','password','database'); 00034 // $rs = $db->Execute('select col1,col2,col3 from table'); 00035 // rs2html($rs, 'BORDER=2', array('Title1', 'Title2', 'Title3')); 00036 // $rs->Close(); 00037 // 00038 // RETURNS: number of rows displayed 00039 00040 00041 function rs2html(&$rs,$ztabhtml=false,$zheaderarray=false,$htmlspecialchars=true,$echo = true) 00042 { 00043 $s ='';$rows=0;$docnt = false; 00044 GLOBAL $gSQLMaxRows,$gSQLBlockRows,$ADODB_ROUND; 00045 00046 if (!$rs) { 00047 printf(ADODB_BAD_RS,'rs2html'); 00048 return false; 00049 } 00050 00051 if (! $ztabhtml) $ztabhtml = "BORDER='1' WIDTH='98%'"; 00052 //else $docnt = true; 00053 $typearr = array(); 00054 $ncols = $rs->FieldCount(); 00055 $hdr = "<TABLE COLS=$ncols $ztabhtml><tr>\n\n"; 00056 for ($i=0; $i < $ncols; $i++) { 00057 $field = $rs->FetchField($i); 00058 if ($field) { 00059 if ($zheaderarray) $fname = $zheaderarray[$i]; 00060 else $fname = htmlspecialchars($field->name); 00061 $typearr[$i] = $rs->MetaType($field->type,$field->max_length); 00062 //print " $field->name $field->type $typearr[$i] "; 00063 } else { 00064 $fname = 'Field '.($i+1); 00065 $typearr[$i] = 'C'; 00066 } 00067 if (strlen($fname)==0) $fname = ' '; 00068 $hdr .= "<TH>$fname</TH>"; 00069 } 00070 $hdr .= "\n</tr>"; 00071 if ($echo) print $hdr."\n\n"; 00072 else $html = $hdr; 00073 00074 // smart algorithm - handles ADODB_FETCH_MODE's correctly by probing... 00075 $numoffset = isset($rs->fields[0]) ||isset($rs->fields[1]) || isset($rs->fields[2]); 00076 while (!$rs->EOF) { 00077 00078 $s .= "<TR valign=top>\n"; 00079 00080 for ($i=0; $i < $ncols; $i++) { 00081 if ($i===0) $v=($numoffset) ? $rs->fields[0] : reset($rs->fields); 00082 else $v = ($numoffset) ? $rs->fields[$i] : next($rs->fields); 00083 00084 $type = $typearr[$i]; 00085 switch($type) { 00086 case 'D': 00087 if (strpos($v,':') !== false); 00088 else { 00089 if (empty($v)) { 00090 $s .= "<TD> </TD>\n"; 00091 } else { 00092 $s .= " <TD>".$rs->UserDate($v,"D d, M Y") ."</TD>\n"; 00093 } 00094 break; 00095 } 00096 case 'T': 00097 if (empty($v)) $s .= "<TD> </TD>\n"; 00098 else $s .= " <TD>".$rs->UserTimeStamp($v,"D d, M Y, H:i:s") ."</TD>\n"; 00099 break; 00100 00101 case 'N': 00102 if (abs(abs($v) - round($v,0)) < 0.00000001) 00103 $v = round($v); 00104 else 00105 $v = round($v,$ADODB_ROUND); 00106 case 'I': 00107 $vv = stripslashes((trim($v))); 00108 if (strlen($vv) == 0) $vv .= ' '; 00109 $s .= " <TD align=right>".$vv ."</TD>\n"; 00110 00111 break; 00112 /* 00113 case 'B': 00114 if (substr($v,8,2)=="BM" ) $v = substr($v,8); 00115 $mtime = substr(str_replace(' ','_',microtime()),2); 00116 $tmpname = "tmp/".uniqid($mtime).getmypid(); 00117 $fd = @fopen($tmpname,'a'); 00118 @ftruncate($fd,0); 00119 @fwrite($fd,$v); 00120 @fclose($fd); 00121 if (!function_exists ("mime_content_type")) { 00122 function mime_content_type ($file) { 00123 return exec("file -bi ".escapeshellarg($file)); 00124 } 00125 } 00126 $t = mime_content_type($tmpname); 00127 $s .= (substr($t,0,5)=="image") ? " <td><img src='$tmpname' alt='$t'></td>\\n" : " <td><a 00128 href='$tmpname'>$t</a></td>\\n"; 00129 break; 00130 */ 00131 00132 default: 00133 if ($htmlspecialchars) $v = htmlspecialchars(trim($v)); 00134 $v = trim($v); 00135 if (strlen($v) == 0) $v = ' '; 00136 $s .= " <TD>". str_replace("\n",'<br>',stripslashes($v)) ."</TD>\n"; 00137 00138 } 00139 } // for 00140 $s .= "</TR>\n\n"; 00141 00142 $rows += 1; 00143 if ($rows >= $gSQLMaxRows) { 00144 $rows = "<p>Truncated at $gSQLMaxRows</p>"; 00145 break; 00146 } // switch 00147 00148 $rs->MoveNext(); 00149 00150 // additional EOF check to prevent a widow header 00151 if (!$rs->EOF && $rows % $gSQLBlockRows == 0) { 00152 00153 //if (connection_aborted()) break;// not needed as PHP aborts script, unlike ASP 00154 if ($echo) print $s . "</TABLE>\n\n"; 00155 else $html .= $s ."</TABLE>\n\n"; 00156 $s = $hdr; 00157 } 00158 } // while 00159 00160 if ($echo) print $s."</TABLE>\n\n"; 00161 else $html .= $s."</TABLE>\n\n"; 00162 00163 if ($docnt) if ($echo) print "<H2>".$rows." Rows</H2>"; 00164 00165 return ($echo) ? $rows : $html; 00166 } 00167 00168 // pass in 2 dimensional array 00169 function arr2html(&$arr,$ztabhtml='',$zheaderarray='') 00170 { 00171 if (!$ztabhtml) $ztabhtml = 'BORDER=1'; 00172 00173 $s = "<TABLE $ztabhtml>";//';print_r($arr); 00174 00175 if ($zheaderarray) { 00176 $s .= '<TR>'; 00177 for ($i=0; $i<sizeof($zheaderarray); $i++) { 00178 $s .= " <TH>{$zheaderarray[$i]}</TH>\n"; 00179 } 00180 $s .= "\n</TR>"; 00181 } 00182 00183 for ($i=0; $i<sizeof($arr); $i++) { 00184 $s .= '<TR>'; 00185 $a = $arr[$i]; 00186 if (is_array($a)) 00187 for ($j=0; $j<sizeof($a); $j++) { 00188 $val = $a[$j]; 00189 if (empty($val)) $val = ' '; 00190 $s .= " <TD>$val</TD>\n"; 00191 } 00192 else if ($a) { 00193 $s .= ' <TD>'.$a."</TD>\n"; 00194 } else $s .= " <TD> </TD>\n"; 00195 $s .= "\n</TR>\n"; 00196 } 00197 $s .= '</TABLE>'; 00198 print $s; 00199 } 00200 00201 ?>