-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilterFunction-SQL.php
More file actions
142 lines (123 loc) · 5.44 KB
/
Copy pathFilterFunction-SQL.php
File metadata and controls
142 lines (123 loc) · 5.44 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
<?php
require_once(".\search\dbcontroller.php");
$db_handle = new DBController();
$onderwerp = "";
$wat = "";
$why = "";
$how = "";
$niveau = "";
$queryCondition = "";
/*
Gebruik ~* ipv LIKE, omdat deze functie:
a. Veel sneller is,
b. Caseinsensitive zoekt, dus hoofdletters zijn niet belangrijk
Je ziet dat je het hele stuk php-case kan weglaten hiermee, wat de code lekker kort en overzichtelijk houdt.
Net als LIKE kan ~* alleen bij zoek op argumenten van het type text en bijvoorbeeld niet op integer, daarom convert ik de laatste (niveau) voor de zekerheid naar text. Dit heeft wel als bijwerking dat als je zoekt op niveau 1 je ook niveau 10,11,12,etc tergu krijgt, omdat daar ook een 1 in zit. Soms is dat juist handig, soms juist niet. Dan moet je hem anders schrijven "AND niveau = COALESCE(".$niveau.",niveau)"
de coalesce() zorgt voor hetzelfde als jouw case. Namelijk als er geen zoek-criteria is meegegeven, dan neemt het vervangings-argument mee. Door het vervangings argument het zelfde te nemen als het veld waar je op zoekt zorgt er voor dat er geen records uit de tabel worden uitgesloten en dus verder kijkt naar de andere where-criteria. Als er dus helemaal geen zoek-criteria zijn meegeven dan krijg je dus alle records terug...
*/
if(!empty($_POST["search"])) {
$onderwerp = $_POST("search[onderwerp]");
$wat = "";
$why = "";
$how = "";
$query = "SELECT * FROM sch_map.kenniskaart
WHERE onderwerp ~* COALESCE(".$onderwerp.",onderwerp)
AND wat ~* COALESCE('".$wat."',wat)
AND why ~* COALESCE('".$why."',why)
AND how ~* COALESCE('".$how."',how)
AND niveau::text ~* COALESCE('".$niveau."',niveau)::text
ORDER BY kenniskaart_id desc";
$result = $db_handle->runQuery($query);
}
?>
<html>
<head>
<title>Zoeken</title>
<link href="style.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<h2>PHP CRUD with Search and Pagination</h2>
<div id="toys-grid">
<form name="frmSearch" method="post" action="filterfunction-SQL.php">
<div class="search-box">
<p>
<input type="text" id="mysearch" placeholder="onderwerp" name="search[onderwerp]" class="demoInputBox" value="<?php echo $onderwerp; ?>"/>
<input type="text" id="mysearch" placeholder="wat" name="search[wat]" class="demoInputBox" value="<?php echo $wat; ?>"/>
<input type="text" id="mysearch" placeholder="why" name="search[why]" class="demoInputBox" value="<?php echo $why; ?>"/>
<input type="text" id="mysearch" placeholder="how" name="search[how]" class="demoInputBox" value="<?php echo $how; ?>"/>
<select id="Place" name="search[niveau]" multiple="multiple">
<?php
if (! empty($result)) {
foreach ($result as $k => $v) {
echo '<option value="' . $result[$k]['niveau'] . '">' . $result[$k]['niveau'] . '</option>';
}
}
?>
</select><br> <br>
<input type="submit" name="go" class="btnSearch" value="Search">
<input type="reset" class="btnSearch" value="Reset" onclick="window.location='filterfunction.php'">
<?php
if (! empty($_POST['niveau'])) {
?>
<table cellpadding="10" cellspacing="1">
<thead>
<tr>
<th><strong>Onderwerp</strong></th>
<th><strong>Rol</strong></th>
<th><strong>Competentie</strong></th>
<th><strong>Wat</strong></th>
<th><strong>Why</strong></th>
<th><strong>How</strong></th>
<th><strong>Plaatje</strong></th>
<th><strong>Bronnen</strong></th>
<th><strong>Niveau</strong></th>
<th><strong>Studieduur</strong></th>
<th><strong>Rating</strong></th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * from sch_map.kenniskaart";
$i = 0;
$selectedOptionCount = count($_POST['niveau']);
$selectedOption = "";
while ($i < $selectedOptionCount) {
$selectedOption = $selectedOption . "'" . $_POST['niveau'][$i] . "'";
if ($i < $selectedOptionCount - 1) {
$selectedOption = $selectedOption . ", ";
}
$i ++;
}
$query = $query . " WHERE niveau in (" . $selectedOption . ")";
$result = $db_handle->runQuery($query);
}
if (! empty($result)) {
foreach ($result as $k => $v) {
if(is_numeric($k)) {
?>
<tr>
<td><?php echo $result[$k]["onderwerp"]; ?></td>
<td><?php echo $result[$k]["rol"]; ?></td>
<td><?php echo $result[$k]["competentie"]; ?></td>
<td><?php echo $result[$k]["wat"]; ?></td>
<td><?php echo $result[$k]["why"]; ?></td>
<td><?php echo $result[$k]["how"]; ?></td>
<td><img src='<?php echo $result[$k]["plaatje"]; ?>' ></td>
<td><?php echo $result[$k]["bronnen"]; ?></td>
<td><?php echo $result[$k]["niveau"]; ?></td>
<td><?php echo $result[$k]["studieduur"]; ?></td>
<td><?php echo $result[$k]["rating"]; ?></td>
</tr>
<?php
}
}
?>
<tbody>
</table>
<?php
}
?>
</form>
</div>
</body>
</html>