-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.php
147 lines (107 loc) · 3 KB
/
search.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
<?php
$max = 500;
#$phototext = 'Has photo';
$phototext = '<img src="photo.png" height="14" align="bottom"/>';
# Using a simple templating library, mustache, which is implemented in about a dozen different languages.
# php mustache is from bobthecow....
include('Mustache.php');
# rendering function.
function mrender($t,$v) {
$m = new Mustache;
echo $m->render($t, $v);
exit;
}
# Found a nice date checker snippet at php.net!!
function datecheck($input) {
$date_format = 'Y-m-d';
$input = trim($input);
$time = strtotime($input);
$is_valid = date($date_format, $time) == $input;
if (!($is_valid)) {
$template = file_get_contents('exception.mustache');
$view['exception'] = "$input is not valid. Dates must be in the form: YYYY-MM-DD. Example: 1973-04-08. Dates must also be real.";
mrender($template, $view);
}
}
# This is the results template.
# For exceptions use a different one.
# And make it something simple, with a return link to the search form
# and a one scalar view.
$template = file_get_contents('list.mustache');
$con = mysql_connect ("<server>", "<user>","<password>") or die (mysql_error());
$datesort = $_POST['dsort'];
$term = $_POST['term'];
$startdate = $_POST['startdate'];
$enddate = $_POST['enddate'];
$paper = $_POST['paper'];
$papers = join(", ", $paper);
datecheck($startdate);
datecheck($enddate);
if (strlen($term) > 100) {
$template = file_get_contents('exception.mustache');
$view['exception'] = "You must shorten your search term. It is over 100 characters.";
mrender($template, $view);
}
$view = array();
$view['datesort'] = $datesort;
$view['term'] = $term;
$view['startdate'] = $startdate;
$view['enddate'] = $enddate;
$view['papers'] = $papers;
$results = array();
mysql_select_db("ncs",$con);
$sql = mysql_query("
select * from ncsi
where
sdate between '$startdate' and '$enddate'
and
(headline like '%$term%'
or
subject like '%$term%'
or
columntitle like '%$term%'
or
author like '%$term%')
order by sdate $datesort, page asc, author asc, headline asc;
");
$count = 0;
while ($row = mysql_fetch_array($sql)){
if(isset($_POST['photo']) && $_POST['photo'] == 'Yes') {
if ($row['photo'] == '') {
continue;
}
}
if ($row['photo'] == 'yes' || $row['photo'] == 'Yes') {
$row['photo'] = $phototext;
}
foreach($paper as $k=>$v) {
if(isset($paper[$k])) {
$count++;
if ($row['newspaper'] != $v) {
$count--;
continue;
} else {
array_push($results, array(
'headline' => $row['headline'],
'newspaper' => $row['newspaper'],
'date' => $row['date'],
'page' => $row['page'],
'photo' => $row['photo'],
'subject' => $row['subject'],
'columntitle' => $row['columntitle'],
'count' => $count
));
}
}
}
if ($count > $max) {
unset($results);
$template = file_get_contents('exception.mustache');
$view['exception'] = "Your search returned more than $max results. Please refine it.";
mrender($template, $view);
}
}
$view['count'] = $count;
$view['results'] = $results;
mrender($template, $view);
?>