· 8 years ago · Jan 08, 2018, 09:38 PM
1CREATE TABLE IF NOT EXISTS `test_table` (
2`id` int(11) NOT NULL AUTO_INCREMENT,
3`phone_name` varchar(255) DEFAULT NULL,
4`price` int(11) DEFAULT NULL,
5`samsung` tinyint(1) DEFAULT NULL,
6`apple` tinyint(1) DEFAULT NULL,
7`nokia` tinyint(1) DEFAULT NULL,
8`touchscreen` tinyint(1) DEFAULT NULL,
9`qwerty` tinyint(1) DEFAULT NULL,
10`classic` tinyint(1) DEFAULT NULL,
11`single_core` tinyint(1) DEFAULT NULL,
12`dual_core` tinyint(1) DEFAULT NULL,
13`quad_core` tinyint(1) DEFAULT NULL,
14PRIMARY KEY (`id`)
15) ENGINE=InnoDB DEFAULT CHARSET=utf8;
16
17INSERT INTO `test_table` (`id`, `phone_name`, `price`, `samsung`, `apple`,
18`nokia`, `touchscreen`, `qwerty`, `classic`, `single_core`, `dual_core`, `quad_core`)
19VALUES
20(1, 'Samsung Galaxy S4', 470, 1, 0, 0, 1, 0, 0, 0, 0, 1),
21(2, 'Samsung Chat', 220, 1, 0, 0, 1, 1, 0, 1, 0, 0),
22(3, 'Iphone 4', 380, 0, 1, 0, 1, 0, 0, 0, 1, 0),
23(4, 'Iphone 5', 550, 0, 1, 0, 1, 0, 0, 0, 0, 1),
24(5, 'Nokia Lumia 520', 150, 0, 0, 1, 1, 0, 0, 0, 1, 0),
25(6, 'Nokia E72', 250, 0, 0, 1, 0, 1, 0, 1, 0, 0);
26
27<div id="filter">
28 <h2>Filter options</h2>
29 <div><input type="checkbox" id="samsung" name="samsung"> <label for="samsung">samsung</label></div>
30 <div><input type="checkbox" id="apple" name="apple"> <label for="apple">apple</label></div>
31 <div><input type="checkbox" id="nokia" name="nokia"> <label for="nokia">nokia</label></div>
32 <div><input type="checkbox" id="touchscreen" name="touchscreen"> <label for="touchscreen">touchscreen</label></div>
33 <div><input type="checkbox" id="qwerty" name="qwerty"> <label for="qwerty">qwerty</label></div>
34 <div><input type="checkbox" id="classic" name="classic"> <label for="classic">classic</label></div>
35 <div><input type="checkbox" id="single_core" name="single_core"> <label for="single_core">single_core</label></div>
36 <div><input type="checkbox" id="dual_core" name="dual_core"> <label for="dual_core">dual_core</label></div>
37 <div><input type="checkbox" id="quad_core" name="quad_core"> <label for="quad_core">quad_core</label></div>
38 </div>
39<script src="jquery-3.2.1.min.js"></script>
40 <script src="common.js"></script>
41
42function getEmployeeFilterOptions(){
43var opts = [];
44$checkboxes.each(function(){
45if(this.checked){
46opts.push(this.name);
47}
48});
49return opts;
50}
51
52function updateEmployees(opts){
53$.ajax({
54 type: "POST",
55 url: "submit.php",
56 dataType : 'json',
57 cache: false,
58 data: {filterOpts: opts},
59 success: function(records){
60 $('#mobile-phones tbody').html(makeTable(records));
61 },
62 error: function(data) {
63 console.log("ERROR");
64 }
65 });
66}
67
68var $checkboxes = $("input:checkbox");
69$checkboxes.on("change", function(){
70var opts = getEmployeeFilterOptions();
71updateEmployees(opts);
72});
73
74updateEmployees();
75
76<?php
77$pdo = new PDO('mysql:host=localhost;dbname=test_database', 'root', '');
78$select = 'SELECT id, phone_name, price';
79$from = ' FROM test_table';
80$where = ' WHERE TRUE';
81$opts = isset($_POST['filterOpts'])? $_POST['filterOpts'] : array('');
82
83if (in_array("samsung", $opts)){
84$where .= " OR samsung = 1";
85}
86if (in_array("apple", $opts)){
87$where .= " OR apple = 1";
88}
89if (in_array("nokia", $opts)){
90$where .= " OR nokia = 1";
91}
92if (in_array("touchscreen", $opts)){
93$where .= " OR touchscreen = 1";
94}
95if (in_array("qwerty", $opts)){
96$where .= " OR qwerty = 1";
97}
98if (in_array("classic", $opts)){
99$where .= " OR classic = 1";
100}
101if (in_array("single_core", $opts)){
102$where .= " OR single_core = 1";
103}
104if (in_array("dual_core", $opts)){
105$where .= " OR dual_core = 1";
106}
107if (in_array("quad_core", $opts)){
108$where .= " OR quad_core = 1";
109}
110
111$sql = $select . $from . $where;
112$statement = $pdo->prepare($sql);
113$statement->execute();
114$results = $statement->fetchAll(PDO::FETCH_ASSOC);
115$json = json_encode($results);
116echo($json);
117?>