-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauthors.php
More file actions
211 lines (210 loc) · 9.28 KB
/
authors.php
File metadata and controls
211 lines (210 loc) · 9.28 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
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
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<title>Opera Omnia - Authors</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="utf-8"/>
<style>
.topnav ul li [href='authors'] {
background-color: #d7d7d7;
}
</style>
<script>
jQuery(document).ready(function($) {
$(".clickable-row").click(function() {
window.location = $(this).data("href");
});
});
</script>
</head>
<body>
<?php include("header.php");
$nResults=0;
$selected =""?>
<section>
<h1>Authors</h1>
<form method="post" action="#">
<div id="formAuthors">
<fieldset>
<legend><b> Search an author :</b></legend>
<label for="name">Name of the author:</label>
<input type="text" name="name" id="name"<?php if(isset($_POST['name'])){echo "value=\"".$_POST['name']."\"";}?>/>
<div id="selectField">
<p>Only show:</p>
<input type="checkbox" name="field[]" value="Physics" <?php if(isset($_POST['field']) && in_array("Physics", $_POST['field'])){echo 'checked';}?>>Physicists<br>
<input type="checkbox" name="field[]" value="Mathematics" <?php if(isset($_POST['field']) && in_array("Mathematics", $_POST['field'])){echo 'checked';}?>>Mathematicians<br>
<input type="checkbox" name="field[]" value="Computer Science" <?php if(isset($_POST['field']) && in_array("Computer Science", $_POST['field'])){echo 'checked';}?>>Computer Scientists<br>
</div>
<fieldset>
<legend>Sort by:</legend>
<select name="sorting" size="1">
<option value="LastName" <?php if (isset($_POST['sorting']) && $_POST['sorting'] == 'LastName') {echo 'selected';} ?>>Last Name</option>
<option value="FirstName" <?php if (isset($_POST['sorting']) && $_POST['sorting'] == 'FirstName') {echo 'selected';} ?>>First Name </option>
<option value="Birthday" <?php if (isset($_POST['sorting']) && $_POST['sorting'] == 'Birthday') {echo 'selected';} ?>>Birth Date</option>
<option value="DateOfDeath" <?php if (isset($_POST['sorting']) && $_POST['sorting'] == 'DateOfDeath') {echo 'selected';} ?>>Death Date</option>
<option value="Field" <?php if (isset($_POST['sorting']) && $_POST['sorting'] == 'Field') {echo 'selected';} ?>>Field</option>
</select>
<div id="order">
<input type="radio" name="order" value=" " <?php if(isset($_POST['order']) && $_POST['order'] == " "){echo 'checked';}?> checked>Ascending<br>
<input type="radio" name="order" value="DESC" <?php if(isset($_POST['order']) && $_POST['order'] == "DESC"){echo 'checked';}?>>Descending<br>
</div>
</fieldset>
</fieldset>
</div>
<input id="submitButton" type="submit" value="Submit">
</form>
<div style="overflow-x:auto;">
<table id="authList" width="90%">
<tr id="tableAuthorList">
<th>Name</th>
<th>Years</th>
<th class="biography">Bio</th>
<th>Fields</th>
</tr>
<?php
$chosenMethod='LastName';
$order = " ";
if (isset($_POST['sorting']))
{
$chosenMethod = $_POST['sorting'];
$selected = $_POST['sorting'];
}
try
{
$bdd = new PDO('mysql:host=localhost;dbname=operaomnia v2;charset=utf8', 'root', '');
$bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}
$query="SELECT * FROM authors";
if(isset($_POST['name']) && !empty($_POST['name']))
{
$query.=" WHERE LastName LIKE '%".strtoupper($_POST['name'])."%' ";
if(isset($_POST['field']))
{
$fieldWanted=$_POST['field'];
if(!empty($fieldWanted))
{
$iterations=0;
foreach($fieldWanted as $singleField)
{
if($iterations==0)
{
$query.=" AND Field='".$singleField."'";
}
else
{
$query.=" OR Field='".$singleField."'";
}
$iterations=$iterations+1;
}
}
}
}
else
{
if(isset($_POST['field']))
{
$fieldWanted=$_POST['field'];
if(!empty($fieldWanted))
{
$iterations=0;
foreach($fieldWanted as $singleField)
{
if($iterations==0)
{
$query.=" WHERE Field='".$singleField."'";
}
else
{
$query.=" OR Field='".$singleField."'";
}
$iterations=$iterations+1;
}
}
}
}
if (isset($_POST['order']))
{
$order = $_POST['order'];
}
$query.=" ORDER BY ". $chosenMethod ." ". $order;
$reponse=$bdd->query($query);
$nResults=0;
while ($data = $reponse->fetch())
{
$fieldList="";
$temp="";
for($i=0; $i<strlen($data['Fields']); $i++)
{
// var_dump($data['Fields']);
if($data['Fields'][$i]!='_')
{
$temp.=$data['Fields'][$i];
}
if($data['Fields'][$i]=='_' or $i==strlen($data['Fields'])-1)
{
if($temp==0)
{
$fieldList="Other";
}
else
{
if($temp==1)
{
$fieldList.="Mathematics, ";
}
if($temp==2)
{
$fieldList.="Physics, ";
}
if($temp==3)
{
$fieldList.="Computer Science, ";
}
}
$temp="";
}
}
$fieldList=substr($fieldList, 0, -2);
$nResults=$nResults+1;
?>
<tr class='clickable-row' data-href='author.php?authid=<?php echo $data['ID']?>'>
<td id="authName"><?php echo $data['FirstName']; ?> <strong><?php echo $data['LastName']; ?></strong></td>
<td>(<?php echo date_format(date_create($data['DateBirth']),"Y");?>-<?php if($data['DateDeath'] != NULL){echo date_format(date_create($data['DateDeath']),"Y");}else{echo " ";} ?>)</td>
<td id="bio"><?php echo substr($data['Bio'], 0, 100)?>...</td>
<td><?php echo $fieldList?></td>
</tr>
<?php
}
if ($nResults==0)
{
if(isset($_POST['title']))
{
?>
<tr>
<td id="noResult" colspan="5">There were no results for the name "<?php echo $_POST['name']?>". You could check the spelling, or try this name in other fields.</td>
</tr>
<?php
}
else
{
?>
<tr>
<td id="noResult" colspan="5">No author was found.</td>
</tr>
<?php
}
}
$reponse->closeCursor();
?>
</table>
</div>
</section>
<?php include("footer.php"); ?>
</body>
</html>