source: Dev/trunk/rdfapi/util/adodb/tohtml.inc.php @ 12

Last change on this file since 12 was 12, checked in by basvannuland, 14 years ago

Added RAP RDF API
Added RDF reader writer for save and load survey

File size: 5.6 KB
Line 
1<?php
2/*
3  V4.93 10 Oct 2006  (c) 2000-2007 John Lim (jlim#natsoft.com.my). 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 <oxenreid@state.net>
9*/
10 
11// specific code for tohtml
12GLOBAL $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
41function rs2html(&$rs,$ztabhtml=false,$zheaderarray=false,$htmlspecialchars=true,$echo = true)
42{
43$s ='';$rows=0;$docnt = false;
44GLOBAL $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 = '&nbsp;';
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 (empty($v)) $s .= "<TD> &nbsp; </TD>\n";
88                                else if (!strpos($v,':')) {
89                                        $s .= " <TD>".$rs->UserDate($v,"D d, M Y") ."&nbsp;</TD>\n";
90                                }
91                                break;
92                        case 'T':
93                                if (empty($v)) $s .= "<TD> &nbsp; </TD>\n";
94                                else $s .= "    <TD>".$rs->UserTimeStamp($v,"D d, M Y, h:i:s") ."&nbsp;</TD>\n";
95                        break;
96                       
97                        case 'N':
98                                if (abs(abs($v) - round($v,0)) < 0.00000001)
99                                        $v = round($v);
100                                else
101                                        $v = round($v,$ADODB_ROUND);
102                        case 'I':
103                                $s .= " <TD align=right>".stripslashes((trim($v))) ."&nbsp;</TD>\n";
104                               
105                        break;
106                        /*
107                        case 'B':
108                                if (substr($v,8,2)=="BM" ) $v = substr($v,8);
109                                $mtime = substr(str_replace(' ','_',microtime()),2);
110                                $tmpname = "tmp/".uniqid($mtime).getmypid();
111                                $fd = @fopen($tmpname,'a');
112                                @ftruncate($fd,0);
113                                @fwrite($fd,$v);
114                                @fclose($fd);
115                                if (!function_exists ("mime_content_type")) {
116                                  function mime_content_type ($file) {
117                                    return exec("file -bi ".escapeshellarg($file));
118                                  }
119                                }
120                                $t = mime_content_type($tmpname);
121                                $s .= (substr($t,0,5)=="image") ? " <td><img src='$tmpname' alt='$t'></td>\\n" : " <td><a
122                                href='$tmpname'>$t</a></td>\\n";
123                                break;
124                        */
125
126                        default:
127                                if ($htmlspecialchars) $v = htmlspecialchars(trim($v));
128                                $v = trim($v);
129                                if (strlen($v) == 0) $v = '&nbsp;';
130                                $s .= " <TD>". str_replace("\n",'<br>',stripslashes($v)) ."</TD>\n";
131                         
132                        }
133                } // for
134                $s .= "</TR>\n\n";
135                         
136                $rows += 1;
137                if ($rows >= $gSQLMaxRows) {
138                        $rows = "<p>Truncated at $gSQLMaxRows</p>";
139                        break;
140                } // switch
141
142                $rs->MoveNext();
143       
144        // additional EOF check to prevent a widow header
145                if (!$rs->EOF && $rows % $gSQLBlockRows == 0) {
146       
147                //if (connection_aborted()) break;// not needed as PHP aborts script, unlike ASP
148                        if ($echo) print $s . "</TABLE>\n\n";
149                        else $html .= $s ."</TABLE>\n\n";
150                        $s = $hdr;
151                }
152        } // while
153
154        if ($echo) print $s."</TABLE>\n\n";
155        else $html .= $s."</TABLE>\n\n";
156       
157        if ($docnt) if ($echo) print "<H2>".$rows." Rows</H2>";
158       
159        return ($echo) ? $rows : $html;
160 }
161 
162// pass in 2 dimensional array
163function arr2html(&$arr,$ztabhtml='',$zheaderarray='')
164{
165        if (!$ztabhtml) $ztabhtml = 'BORDER=1';
166       
167        $s = "<TABLE $ztabhtml>";//';print_r($arr);
168
169        if ($zheaderarray) {
170                $s .= '<TR>';
171                for ($i=0; $i<sizeof($zheaderarray); $i++) {
172                        $s .= " <TH>{$zheaderarray[$i]}</TH>\n";
173                }
174                $s .= "\n</TR>";
175        }
176       
177        for ($i=0; $i<sizeof($arr); $i++) {
178                $s .= '<TR>';
179                $a = &$arr[$i];
180                if (is_array($a))
181                        for ($j=0; $j<sizeof($a); $j++) {
182                                $val = $a[$j];
183                                if (empty($val)) $val = '&nbsp;';
184                                $s .= " <TD>$val</TD>\n";
185                        }
186                else if ($a) {
187                        $s .=  '        <TD>'.$a."</TD>\n";
188                } else $s .= "  <TD>&nbsp;</TD>\n";
189                $s .= "\n</TR>\n";
190        }
191        $s .= '</TABLE>';
192        print $s;
193}
194
195?>
Note: See TracBrowser for help on using the repository browser.