MySQLTable.php
4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
namespace Tz\WordPress\Tools\MySQLTable;
use PHPExcel;
function array2table($arr, $width = "100%", $with_form = false) {
$count = count($arr);
if (!$count) echo '';
reset($arr);
$num = count(current($arr));
if ($with_form) {
echo <<<HTML
<form id="report-form" action="http://cbv/wp-admin/admin.php?page=report_manager&action=run_action&report_id={$_GET['report_id']}&action_to_run=change_user_status" method="post">
<div style="margin-bottom: 20px">
<label for="action-list"><h4 style="margin-bottom: 5px">With Selected:</h4></label>
<select id="action-list">
<option value="change_user_status">Change User Status</option>
<option value="change_user_role">Change User Role</option>
<option value="add_ce_hours">Add Continuing Education Hours</option>
<option value="send_notification">Send Notification</option>
<option value="create_invoice">Create an Invoice / Credit Note</option>
</select>
<input type="submit" value="Go" />
</div>
HTML;
}
echo "<table border=\"0\" cellpadding=\"5\" cellspacing=\"0\" width=\"$width\" class=\"cbv-report-list\">\n";
echo "<thead>\n";
echo "<tr>\n";
if ($with_form) {
echo '<th style ="min-width: 30px"><input type="checkbox" checked="checked" name="selector" id="selector" /></th>';
}
foreach(current($arr) as $key => $value){
echo "<th valign='bottom'>";
echo $key." ";
echo "</th>\n";
}
echo "</tr>\n";
echo "</thead>\n";
echo "<tbody>\n";
$wi = 0;
while ($curr_row = current($arr)) {
$rclass = ($wi%2) ? "odd" : "";
echo "<tr class='".$rclass."'>\n";
$col = 1;
if ($with_form) {
echo '<td style="min-width: 30px"><input type="checkbox" checked="checked" name="records[' . $curr_row['ID'] . ']" value="1" /></td>';
}
while (false !== ($curr_field = current($curr_row))) {
echo "<td>";
echo $curr_field." ";
echo "</td>\n";
next($curr_row);
$col++;
}
while($col <= $num){
echo "<td> </td>\n";
$col++;
}
echo "</tr>\n";
$wi++;
next($arr);
}
echo "</tbody>\n";
echo "</table>\n";
if ($with_form) {
echo '</form>';
}
}
function CSVExport($arr) {
$out = "";
$count = count($arr);
if( $count > 0 ) {
reset($arr);
$num = count(current($arr));
$k = 0;
foreach(current($arr) as $key => $value){
if ($k > 0) {
$out .= ",";
}
$out .= '"'.str_replace( '"', '\"',$key).'"';
$k++;
}
$out .= "\n";
while ($curr_row = current($arr)) {
$col = 1;
while (false !== ($curr_field = current($curr_row))) {
if ($col > 1) {
$out .= ",";
}
$out .= '"'.str_replace( '"', '\"',$curr_field).'"';
next($curr_row);
$col++;
}
while($col <= $num) {
if ($col > 1) {
$out .= ",";
}
$out .= '""';
$col++;
}
$out .= "\n";
next($arr);
}
}
return $out;
}
function XLSExport ($arr, PHPExcel $writer) {
if (count($arr) < 1) {
return false;
}
$sheet = $writer->getActiveSheet();
$styleArray = Array(
'font' => Array(
'bold' => true
)
);
// Set the headers
$row_count = 2;
$columns = Array();
foreach ($arr as $data) {
$i = $j = ord('A');
$alphabet_count = 0;
$prefix = '';
foreach ($data as $key => $value) {
// Check if we're at the character past 'Z'
if ($i == 91) {
$i = ord('A') + $alphabet_count;
}
if ($j == 91) {
$j = ord('A');
$prefix = chr($i++);
}
// Set headers
$cell = $prefix . chr($j++);
$sheet->setCellValue($cell . '1', $key);
$sheet->getStyle($cell . '1')->applyFromArray($styleArray);
// Put in data
$sheet->setCellValue($cell . $row_count, $value);
$columns[] = $cell;
$alphabet_count++;
}
$row_count++;
}
// Set column dimensions
foreach ($columns as $column) {
$sheet->getColumnDimension($column)->setAutoSize(true);
}
}