-
Notifications
You must be signed in to change notification settings - Fork 1
/
ideatablegen.php
176 lines (153 loc) · 4.22 KB
/
ideatablegen.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
<?php
include('mysql_connect.php');
include('data_sanitizers.php');
session_start();
$table = 'ideas';
$num_entries = 10;
//This function will create a query that selects $num_entries rows from $table selecting for $tags (an array of tags) in tags field and selecting rows created within $num_days days of the request date and orders by $sort (likes, views, datecreated)
function idea_feed_query($table,$sort,$num_entries,$num_days,$tags)
{
$tag_string = '';
$col_str = '';
if($tags[0] != '')
{
foreach($tags as $tag)
{
$tag_string .= "t.name = '" . $tag . "' AND ";
}
$tag_string = substr($tag_string,0,-5);
}
if($tags != '')
$sql_query = "SELECT DISTINCT(i.id), u.username, u.id as user_id, i.creation_date, i.title, i.short_desc, i.pic, i.likes, i.views, i.id as idea_id, t.name, i.has_location
FROM $table AS i
JOIN users as u ON u.id = i.manager_id
JOIN idea_tags as it ON it.idea_id = i.id
JOIN tags as t ON t.id = it.tag_id
WHERE i.creation_date > DATE_SUB(CURDATE(), INTERVAL $num_days DAY) AND ({$tag_string})
ORDER BY i.$sort
LIMIT $num_entries";
else
$sqlquery = "SELECT u.username, u.id, i.creation_date, i.title, i.short_desc, i.pic, i.likes, i.views, i.id, i.has_location
FROM $table AS i
JOIN users as u ON u.id = i.manager_id
WHERE i.creation_date > DATE_SUB(CURDATE(), INTERVAL $num_days DAY)
ORDER BY i.$sort
LIMIT $num_entries";
return $sqlquery;
}
if(empty($_GET['sort'])) //Test if the get variables are specified in the url, if not defaults to sort by likes within past week.
$sort = 'likes';
else
$sort = clean($_GET['sort']);
if(empty($_GET['time']))
$num_days = 30;
else
$num_days = clean($_GET['time']);
if(!empty($_GET['tag_str'])){
$user_tag_str = $_GET['tag_str'];
$tag_array = explode_filtered("+", $user_tag_str);
$tag_array = preg_replace("/[^A-Za-z0-9?!\s]/","", $tag_array);
$tag_array = array_map('trim',$tag_array);
$_SESSION['idea_tags'][] = $tag_array;
}
if(!empty($_GET['insert_bool']))
$insert_bool = 1;
else
$insert_bool = 0;
if(!isset($_SESSION['idea_tags']))
$tags = '';
else{
$tags = $_SESSION['idea_tags'];
$num_tabs = count($tags)+1;
}
try {
$conn = new PDO($dsn, $user, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
if($insert_bool === 1){
$sql = idea_feed_query($table,$sort,$num_entries,$num_days,$tag_array);
try{
$data = $conn->query($sql);
echo '<ol>';
foreach($data as $row){
include('ideabar.php');
}
echo '</ol>';
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
$conn = null;
}
else{
$j=0;
echo '<div id="tabs">
<ul id = "tab_list">
<li><a href="#tabs-1">All</a><span class="ui-icon ui-icon-close">Remove Tab</span></li>';
if($tags != ''){
while($j<($num_tabs-1)){
$tag_head = '';
if(is_array($tags[$j])){
foreach($tags[$j] as $tag_name){
$tag_head.=$tag_name . ' + ';
}
$tag_head = substr($tag_head,0,-3);
}
else
$tag_head = $tags[$j];
echo '<li><a href="#tabs-'.($j+2).'">'.$tag_head.'</a><span class="ui-icon ui-icon-close">Remove Tab</span></li>';
$j++;
}
echo '</ul>
<div id ="tabs-1">';
$sql = idea_feed_query($table,$sort,$num_entries,$num_days,'');
try{
echo '<ol>';
$data = $conn->query($sql);
foreach($data as $row){
include('ideabar.php');
}
echo '</ol>';
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
echo '</div>';
$j = 0;
while($j<($num_tabs-1)){
$i = 1;
$sql = idea_feed_query($table,$sort,$num_entries,$num_days,$tags[$j]);
try{
$data = $conn->query($sql);
echo '<div id="tabs-'.($j+2).'">
<ol>';
foreach($data as $row){
include('ideabar.php');
}
echo '</ol>';
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
echo '</div>';
$j++;
}
}
else{
echo '</ul><div id="tabs-1"><ol>';
$sql = idea_feed_query($table,$sort,$num_entries,$num_days,'');
$i =1;
try{
$data = $conn->query($sql);
foreach($data as $row){
include('ideabar.php');
}
echo '</ol>';
}catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
echo '</div>';
}
echo '</div>';
$conn = null;
}
?>