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