forked from jstolpe/blog_code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlike.php
70 lines (63 loc) · 1.41 KB
/
like.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
<?php
// include global things for use
include 'global_data.php';
$sql = array( // array keyed by the various sql statments we can run
'LIKEANY' => array(
'sql' => '
SELECT
*
FROM
drinks
WHERE
name LIKE CONCAT("%", :term, "%")
',
'term' => 'Sugar'
),
'LIKEEND' => array(
'sql' => '
SELECT
*
FROM
drinks
WHERE
name LIKE CONCAT("%", :term)
',
'term' => 'l'
),
'LIKESTART' => array(
'sql' => '
SELECT
*
FROM
drinks
WHERE
name LIKE CONCAT(:term, "%")
',
'term' => 'Red Bull - T'
),
'NOTLIKESTART' => array(
'sql' => '
SELECT
*
FROM
drinks
WHERE
name NOT LIKE CONCAT(:term, "%")
',
'term' => 'Red Bull - T'
)
);
// check url for sql statement to run
$selectedSql = isset( $_GET['filter'] ) && isset( $sql[$_GET['filter']] ) ? $sql[$_GET['filter']] : $sql['LIKEANY'];
$params = array( // keys must match the variables in the sql so they can get replaced with the value
':term' => $selectedSql['term']
);
// prepare the sql
$statement = $database->prepare( $selectedSql['sql'] );
// execute the sql
$statement->execute( $params );
// get the query data
$queryDrinks = $statement->fetchAll();
// display the html
displayViewHtml( $selectedSql['sql'], $allDrinks, $queryDrinks, $statement );
?>