-
Notifications
You must be signed in to change notification settings - Fork 1
/
tableSorting.php
235 lines (154 loc) · 4.86 KB
/
tableSorting.php
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
include("config.php");
include("model.php");
include("stats.php");
// how many rows to show per page
$rowsPerPage = 10;
$pageNum = 1;
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
if (isset($_GET["tbl"])){
$tableName = $_GET["tbl"];
//$pageNum = 1;
}
if (isset($_GET["sort"])){
$sort = $_GET["sort"];
}
if (isset($_GET["dir"])){
$dir = $_GET["dir"];
}
// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
if( array_key_exists("tbl", $_GET) && array_key_exists("sort", $_GET)) {
include('view/header.html');
if ($dir == 1){
$recommendations = sortColumns(TRUE, $sort);
} else if ($dir == 0){
$recommendations = sortColumns(FALSE, $sort);
}
if (isset($recommendations) ){
createStats($tableName, $localDB);
if ($sort == 4){
$column = "1d";
} else if ($sort == 5){
$column = "5d";
} else if ($sort == 6){
$column = "30d";
} else if ($sort == 7){
$column = "365d";
}else{
$column = "30d";
}
/* showStats($tableName, $column, TRUE);
showStats($tableName, $column, FALSE);
showTRStats($tableName, $column, TRUE);
showTRStats($tableName, $column, FALSE);
*/
echo '<script type="text/javascript"> insert("'.$tableName.'", "'.$column.'" ); </script>';
include("view/Performance.html");
}else{
echo "<h3> No Recommendations found in the database for this stock.</h3>";
}
// -------------------------------------------------------------
// the following code for pagination is based on an example by http://www.phpasks.com/articles/phpmysqlpaging-splittingyourqueryresultinmultiplep.html
// count the rows in the database
$queryString = "SELECT COUNT(*) AS count1 FROM $tableName";
$result = mysql_query($queryString, $localDB);
$row = mysql_fetch_array($result);
$num_rows = $row['count1'];
$maxPage = ceil($num_rows/$rowsPerPage);
// echo "num rows:" .$num_rows."</br>";
// echo "max page:" .$maxPage."</br>";
// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav = '';
$data = array('tbl'=>$tableName,
'sort'=> $sort,
'dir' => $dir
);
$self .= "?".http_build_query($data, '', '&');
//echo $self;
$self.= '&';
if ($pageNum > 1) {
$page = $pageNum - 1;
//$prev = " <a href=".$self."page=".$page.">[Prev]</a> ";
$prev = $self."page=".$page;
//$first = " <a href=".$self."page=1\">[First Page]</a> ";
$first = $self."page=1";
} else {
$prev = ' '; // we're on page one, don't print previous link
$first = ' '; // nor the first page link
}
if ($pageNum < $maxPage) {
$page = $pageNum + 1;
//$next = " <a href=".$self."page=".$page.">[Next]</a> ";
$next = $self."page=".$page;
//$last = " <a href=".$self."page=".$maxPage.">[Last Page]</a> ";
$last = $self."page=".$maxPage;
} else {
$next = ' '; // we're on the last page, don't print next link
$last = ' '; // nor the last page link
}
//echo " Showing page $pageNum of $maxPage pages ".
//$first . $prev . $next . $last;
include("view/pages.html");
echo " Showing page $pageNum of $maxPage pages";
// --------------------------------------------------------------------------------
include('view/footer.html');
/* createStats($tableName, $localDB);
showStats($tableName, "30d", TRUE);
showStats($tableName, "30d", FALSE);
showTRStats($tableName, "30d", TRUE);
showTRStats($tableName, "30d", FALSE);
*/
} else{
// If no query was processed show generic homepage
include('view/header.html');
//include('view/index.html');
include('view/footer.html');
}
function sortColumns($asc, $sortType){
global $localDB;
global $tableName;
global $offset;
global $rowsPerPage;
if ($asc == TRUE){
$dir = "ASC";
}else{
$dir = "DESC";
}
if ($sortType == 1){
$column = "displayURL";
} else if ($sortType == 2){
$column = "date";
} else if ($sortType == 3){
$column = "direction";
} else if ($sortType == 4){
$column = "1d";
} else if ($sortType == 5){
$column = "5d";
} else if ($sortType == 6){
$column = "30d";
} else if ($sortType == 7){
$column = "365d";
}
$queryString = "SELECT * FROM $tableName ORDER BY ".$column." ".$dir." LIMIT $offset , $rowsPerPage";
//echo $queryString;
$query = mysql_query($queryString, $localDB);
if(!$query) die(mysql_error()."\n");
$results = array();
$finalResults = array();
$num_rows = mysql_num_rows($query);
if ($num_rows == 0){
$results = null;
return $results;
}
while($result = mysql_fetch_array($query)) {
array_push($results, $result);
}
return $results;
}
?>