1 |
rodolico |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
This class simply extends DBQuery to allow output to be converted to HTML
|
|
|
5 |
*/
|
|
|
6 |
|
|
|
7 |
require_once( 'DBQueryHTML.class.php' );
|
|
|
8 |
|
|
|
9 |
class DBQueryHTML extends DBQuery {
|
|
|
10 |
|
|
|
11 |
/*
|
|
|
12 |
Function takes an SQL statement and converts it to an HTML table.
|
|
|
13 |
Return Value: HTML table representation of the query
|
|
|
14 |
Parameters:
|
|
|
15 |
$sql A valid SQL query to run
|
|
|
16 |
$format An optional array of format strings (suitable for printf) for each column (empty strings ignored)
|
|
|
17 |
$makeTableDef If True, the resulting HTML has the <table></table> tags; otherwise starts with header
|
|
|
18 |
$append Arbitrary string that is appended as a single column to each row
|
|
|
19 |
|
|
|
20 |
NOTE ON $append
|
|
|
21 |
$append may optionally contain variables of the form %colname%, which will be replaced
|
|
|
22 |
with values from the current row. $colname is taken from the META data from the query, thus
|
|
|
23 |
the query select foo from bar would have a meta of header of foo for column 0.
|
|
|
24 |
$append is searched for strings of form %foo% in that case, and the current value of column
|
|
|
25 |
foo replaces the tag %foo%.
|
|
|
26 |
%foo% is ignored if foo is not a valid column name in a query.
|
|
|
27 |
thus, a query such as select zip,city,state from zip_codes and a string of Hello %city% I'm glad
|
|
|
28 |
you are in %State% will not replace the second as State is not a column of this query (it is case
|
|
|
29 |
sensitive). Also, beware of items like select zip,concat(state,', ', city) from zip_codes. It is
|
|
|
30 |
much better to rewrite that as select zip,concat(state,', ', city) mytown from zip_codes.
|
|
|
31 |
|
|
|
32 |
If you don't know what all that means, get a book on SQL
|
|
|
33 |
*/
|
|
|
34 |
function queryToTable ( $format = '', $makeTableDef = true, $append='', $headers = '' ) {
|
|
|
35 |
print "\n\n<pre>$this->query</pre>\n\n";
|
|
|
36 |
$tdTextDefinition = '<td valign=' . HTML_QUOTE_CHAR . 'top' . HTML_QUOTE_CHAR . '>';
|
|
|
37 |
$tdNumberDefinition = '<td valign=' . HTML_QUOTE_CHAR .'top' . HTML_QUOTE_CHAR . 'align=' . HTML_QUOTE_CHAR . 'right' . HTML_QUOTE_CHAR . '>';
|
|
|
38 |
$html = '';
|
|
|
39 |
$rows = array();
|
|
|
40 |
$save = $this->useAssociativeArray;
|
|
|
41 |
$this->useAssociativeArray = false; // temporarily turn off associative arrays
|
|
|
42 |
$this->run();
|
|
|
43 |
if ( $this->rowsAffected ) { // we returned at least one row
|
|
|
44 |
// Build the searchFor array for $append
|
|
|
45 |
$searchFor = array();
|
|
|
46 |
foreach ( $this->columnMetaData as $field ) {
|
|
|
47 |
$searchFor[] = '%' . $field['name'] . '%';
|
|
|
48 |
}
|
|
|
49 |
/* special condition where only one row is returned. In that case, $this->returnData is not an array
|
|
|
50 |
of associations, but a simple association. In this case, we need to convert it
|
|
|
51 |
*/
|
|
|
52 |
if (count($this->returnData[0]) == 1) { // convert from association to single row array of associations
|
|
|
53 |
$temp = array();
|
|
|
54 |
foreach ($this->returnData as $column => $value) {
|
|
|
55 |
$temp[0][$column] = $value;
|
|
|
56 |
} // foreach
|
|
|
57 |
$this->returnData = $temp;
|
|
|
58 |
}
|
|
|
59 |
if (count($format) > 0 ) { // we have some formats, so let's do it the hard, slow way
|
|
|
60 |
for ( $row = 0; $row < count($this->returnData); $row++) {
|
|
|
61 |
$rows[$row] = '';
|
|
|
62 |
for ( $column = 0; $column < count($this->returnData[$row]); $column++ ) {
|
|
|
63 |
$rows[$row] .= strlen($format[$column]) && isset($this->returnData[$row][$column])> 0 ?
|
|
|
64 |
( $tdNumberDefinition . sprintf($format[$column],$this->returnData[$row][$column]) . '</td>')
|
|
|
65 |
: ($tdTextDefinition . $this->returnData[$row][$column] . '</td>');
|
|
|
66 |
} // for $column
|
|
|
67 |
/*
|
|
|
68 |
let's append some stuff to the row if it exists.
|
|
|
69 |
We will take the array of SearchFor containing the column names
|
|
|
70 |
and the array of values from this row, then look for matches and replace them
|
|
|
71 |
with the correct values. Note, the column names in $append should have percent
|
|
|
72 |
signs appended and prepended, thus a column name of joe would be %joe% in %append
|
|
|
73 |
*/
|
|
|
74 |
if (strlen($append) > 0) { // let's append some stuff to the row
|
|
|
75 |
$rows[$row] .= $tdTextDefinition . str_replace ( $searchFor, $this->returnData[$row], $append ) . '</td>';
|
|
|
76 |
}
|
|
|
77 |
} // for $row
|
|
|
78 |
} else { // no formatting, so we just slam the stuff together
|
|
|
79 |
for ( $row = 0; $row < count($this->returnData); $row++) {
|
|
|
80 |
$currentValue = $tdTextDefinition . implode( '</td>' . $tdTextDefinition, $this->returnData[$row]) . '</td>';
|
|
|
81 |
if (strlen($append) > 0) { // see explaination in if part of this structure
|
|
|
82 |
$currentValue .= $tdTextDefinition . str_replace ( $searchFor, $this->returnData[$row], $append ) . '</td>';
|
|
|
83 |
}
|
|
|
84 |
$rows[] = $currentValue;
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
// ok, let's get the field headers from the table
|
|
|
88 |
$html .= '<tr>';
|
|
|
89 |
if ( $headers ) { // user has passed in some header fields for us to use
|
|
|
90 |
$html .= '<th>' . implode( '</th><th>', $headers ) . '</th>';
|
|
|
91 |
} else { // no headers, so we just get them from the returned column names
|
|
|
92 |
foreach ( $this->columnMetaData as $field ) {
|
|
|
93 |
$html .= '<th>' . $field['name'] . '</th>';
|
|
|
94 |
}
|
|
|
95 |
} // if headers .. else
|
|
|
96 |
$html .= '</tr>';
|
|
|
97 |
// we now have all the info, so let's make it into a table
|
|
|
98 |
$html .= '<tr>' . implode('</tr><tr>', $rows ) . '</tr>';
|
|
|
99 |
} else {
|
|
|
100 |
$html = '<tr><td>No rows returned by the query</td></tr>';
|
|
|
101 |
}
|
|
|
102 |
if ( $makeTableDef ) { // they want the <table> def, so let's give it to them
|
|
|
103 |
$html = '<table border=' . HTML_QUOTE_CHAR . '1' . HTML_QUOTE_CHAR . '>' . $html . '</table>';
|
|
|
104 |
} // if ( $makeTableDef
|
|
|
105 |
|
|
|
106 |
$this->useAssociativeArray = $save;
|
|
|
107 |
//return '<pre>' . print_r ($this,true) . '</pre>';
|
|
|
108 |
return $html;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
/*
|
|
|
112 |
function executes a query, then returns an array containing only the values of the first
|
|
|
113 |
field in the return as the keys to an associative array. NOTE: any duplicates will be
|
|
|
114 |
discarded. This is useful when you simply want to know if a value exists, ie if you
|
|
|
115 |
are building an html select from a query, and a separate query needs to hold the
|
|
|
116 |
values that are currently selected
|
|
|
117 |
*/
|
|
|
118 |
|
|
|
119 |
function valuesToKeys () {
|
|
|
120 |
$returnValue = array();
|
|
|
121 |
$save = $this->useAssociativeArray;
|
|
|
122 |
$useAssociativeArray = false; // temporarily turn off associative arrays
|
|
|
123 |
$this->run();
|
|
|
124 |
if ( $this->rowsAffected ) { // we returned at least one row
|
|
|
125 |
foreach ( $info as $key => $value ) { // only thing I know to do is iterate
|
|
|
126 |
$returnValue[$this->returnData[0]] = true;
|
|
|
127 |
}
|
|
|
128 |
} // if
|
|
|
129 |
$useAssociativeArray = $save;
|
|
|
130 |
return $returnValue;
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
/* this function will take the result of an SQL query that returns at least two columns.
|
|
|
134 |
The first column becomes the keys in an associative array, and the second column
|
|
|
135 |
becomes the value.
|
|
|
136 |
|
|
|
137 |
Note, because it is an associative array, duplicates of the first column will only contain
|
|
|
138 |
the last value.
|
|
|
139 |
*/
|
|
|
140 |
|
|
|
141 |
function ColumnColumnArray () {
|
|
|
142 |
$returnValue = array();
|
|
|
143 |
$save = $this->useAssociativeArray;
|
|
|
144 |
$useAssociativeArray = false; // temporarily turn off associative arrays
|
|
|
145 |
$this->run();
|
|
|
146 |
if ( $this->rowsAffected ) { // we returned at least one row
|
|
|
147 |
foreach ( $info as $key => $value ) { // only thing I know to do is iterate
|
|
|
148 |
$returnValue[$this->returnData[0]] = $this->returnData[1];
|
|
|
149 |
}
|
|
|
150 |
} // if
|
|
|
151 |
$useAssociativeArray = $save;
|
|
|
152 |
return $returnValue;
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
|
|
|
156 |
/* the first returned column becomes the value, the second becomes the display element
|
|
|
157 |
if $selectedFieldsQuery is begins with 'select', items matching that query will be selected
|
|
|
158 |
if $selectedFieldsQuery is any other value, it is assumed to be the value of the first column
|
|
|
159 |
|
|
|
160 |
*/
|
|
|
161 |
function htmlSelect ( $selectedFieldsQuery = '' ) {
|
|
|
162 |
$html = '';
|
|
|
163 |
$selectedFields = array();
|
|
|
164 |
if (strlen( $selectedFieldsQuery )) {
|
|
|
165 |
if ( preg_match ( '/^\s*select/i', $selectedFieldsQuery ) ) { // They passed in a query
|
|
|
166 |
if (strlen($selectedFieldsQuery) ) {
|
|
|
167 |
$subSet = new DBQuery($selectedFieldsQuery);
|
|
|
168 |
$subSet->run();
|
|
|
169 |
$selectedFields = $subSet->sqlValuesToKeys();
|
|
|
170 |
}
|
|
|
171 |
} else { // assume the passed in a value
|
|
|
172 |
$selectedFields[$selectedFieldsQuery] = 1;
|
|
|
173 |
}
|
|
|
174 |
}
|
|
|
175 |
$save = $this->useAssociativeArray;
|
|
|
176 |
$useAssociativeArray = false; // temporarily turn off associative arrays
|
|
|
177 |
$this->run();
|
|
|
178 |
if ( $this->rowsAffected ) { // we returned at least one row
|
|
|
179 |
foreach ($this->returnData as $rowNumber => $value ) {
|
|
|
180 |
$html .= '<option value="' . $value[0] . '"';
|
|
|
181 |
if ( $selectedFields[$value[0]] ) {
|
|
|
182 |
$html .= ' selected';
|
|
|
183 |
}
|
|
|
184 |
$html .= '>' . $value[1] . '</option>';
|
|
|
185 |
}
|
|
|
186 |
}
|
|
|
187 |
$useAssociativeArray = $save;
|
|
|
188 |
return $html;
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
/* function will take a query and turn it into a series of check boxes. It must contain
|
|
|
192 |
two columns, with the first becoming the the name of the checkbox
|
|
|
193 |
and the second becoming the displayed value. an optional third column will be used
|
|
|
194 |
to match if the $checkedValuesQuery is used.
|
|
|
195 |
if $checkedValuesQuery is not empty, it will be run to see what boxes need to be
|
|
|
196 |
checked by being compared to the third column of the $sql query.
|
|
|
197 |
$htmlBefore will be placed before each check box, and $htmlAfter will be placed after
|
|
|
198 |
each checkbox.
|
|
|
199 |
|
|
|
200 |
if $tableColumns is set to a number, the checkboxes will be embedded in a group of
|
|
|
201 |
<tr></tr>, each containing table columns of $tableColumns width. In this case, $htmlBefore
|
|
|
202 |
will have <td> prepended and $htmlAfter will have </td> appended, meaning any passed
|
|
|
203 |
values will be INSIDE of the td. NOTE: the <table></table> tags are NOT put in.
|
|
|
204 |
|
|
|
205 |
NOTE: currently, using the table stuff will leave a dangling row with 0 elements if
|
|
|
206 |
the number of elements equal the number of columns.
|
|
|
207 |
*/
|
|
|
208 |
|
|
|
209 |
function htmlCheckBoxes ( $checkedValuesQuery = '', $htmlBefore = '', $htmlAfter = '', $table_columns='' ) {
|
|
|
210 |
$html = '';
|
|
|
211 |
if ($table_columns) {
|
|
|
212 |
$htmlBefore = '<td>' . $htmlBefore;
|
|
|
213 |
$htmlAfter .= '</td>';
|
|
|
214 |
$html .= '<tr>';
|
|
|
215 |
}
|
|
|
216 |
$numColumns = 0;
|
|
|
217 |
$checkBoxes = queryDatabaseExtended( $sql,0 );
|
|
|
218 |
$selectedFields = array();
|
|
|
219 |
if (strlen($checkedValuesQuery) ) {
|
|
|
220 |
$subSet = new DBQuery($selectedFieldsQuery);
|
|
|
221 |
$subSet->run();
|
|
|
222 |
$selectedFields = $subSet->sqlValuesToKeys();
|
|
|
223 |
}
|
|
|
224 |
$save = $this->useAssociativeArray;
|
|
|
225 |
$useAssociativeArray = false; // temporarily turn off associative arrays
|
|
|
226 |
foreach ($this->returnData as $row => $values) {
|
|
|
227 |
if ($table_columns && ++$numColumns == $table_columns) {
|
|
|
228 |
$html .= '</tr><tr>';
|
|
|
229 |
$numColumns = 0;
|
|
|
230 |
}
|
|
|
231 |
//objectDebugScreen($row);
|
|
|
232 |
$html .= $htmlBefore . '<input type="checkbox" name="' . $values[0] . '"';
|
|
|
233 |
if ( $selectedFields[$values[2]] ) {
|
|
|
234 |
$html .= ' checked';
|
|
|
235 |
}
|
|
|
236 |
$html .= '>' . $values[1] . $htmlAfter;
|
|
|
237 |
//<INPUT type="checkbox" checked name="temp">
|
|
|
238 |
} // foreach
|
|
|
239 |
$html .= '</tr>';
|
|
|
240 |
$useAssociativeArray = $save;
|
|
|
241 |
return $html;
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
/*
|
|
|
245 |
function CSV ( $sql ) {
|
|
|
246 |
$rows = array();
|
|
|
247 |
$CSV = '';
|
|
|
248 |
$info = array();
|
|
|
249 |
if ( $result = queryDatabaseExtended($sql,0) ) {
|
|
|
250 |
$meta = $result['meta'];
|
|
|
251 |
//objectDebugScreen($result);
|
|
|
252 |
$info = $result['data'];
|
|
|
253 |
unset ($result);
|
|
|
254 |
$headers = array();
|
|
|
255 |
foreach ( $meta as $field ) {
|
|
|
256 |
$headers[] = $field['name'];
|
|
|
257 |
} // foreach
|
|
|
258 |
$rows[] = implode("\t", $headers);
|
|
|
259 |
// special condition where only one row is returned. In that case, $info is not an array
|
|
|
260 |
// of associations, but a simple association. In this case, we need to convert it
|
|
|
261 |
|
|
|
262 |
if (count($info[0]) == 1) { // convert from association to single row array of associations
|
|
|
263 |
$temp = array();
|
|
|
264 |
foreach ($info as $column => $value) {
|
|
|
265 |
$temp[0][$column] = $value;
|
|
|
266 |
} // foreach
|
|
|
267 |
$info = $temp;
|
|
|
268 |
}
|
|
|
269 |
for ( $row = 0; $row < count($info); $row++) {
|
|
|
270 |
$rows[] = implode("\t", $info[$row]);
|
|
|
271 |
}
|
|
|
272 |
// we now have all the info, so let's make it into a table
|
|
|
273 |
$CSV .= implode("\n", $rows );
|
|
|
274 |
} // if ( $info = queryDatabase($sql,0) )
|
|
|
275 |
// print '<pre>'; print_r ($info); print '</pre>';
|
|
|
276 |
return $CSV;
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
*/
|
|
|
280 |
|
|
|
281 |
} // class DBQueryHTML
|
|
|
282 |
|
|
|
283 |
?>
|