Hi, I am a newbie in SitePoint. I followed the samples in SitePoint then made my test.
I checked one brand and one price, everything are OK. However, when I checked one more brand (checked two brands), this is no result appear.
Below is my test sample.
CREATE TABLE IF NOT EXISTS shoetest
(
id
int(11) NOT NULL AUTO_INCREMENT,
brand
varchar(20) NOT NULL,
pPrice
int(4) NOT NULL,
PRIMARY KEY (id
)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;
–
– Dumping data for table shoetest
INSERT INTO shoetest
(id
, brand
, pPrice
) VALUES
(1, ‘Adidas’, 105),
(2, ‘Adidas’, 130),
(3, ‘PUMA’, 115),
(4, ‘PUMA’, 108),
(5, ‘New Banlance’, 85),
(6, ‘New Banlance’, 140);
HTML
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>AJAX filter demo</title>
</head>
<body>
<h1>Phones database</h1>
<table id="shoe">
<thead>
<tr>
<th>ID</th>
<th>Brand</th>
<th>Price</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<div id="filter">
<h2>Filter options</h2>
<div>
<input type="checkbox" id="Adidas" value="Adidas">
<label for="Adidas">Adidas</label>
</div>
<div>
<input type="checkbox" id="NB" value="New Banlance">
<label for="NB">New Banlance</label>
</div>
<div>
<input type="checkbox" id="PUMA" value="PUMA">
<label for="PUMA">PUMA</label>
</div>
<div>
<input type="checkbox" id="p1" value="1">
<label for="p1">< 100</label>
</div>
<div>
<input type="checkbox" id="p2" value="2">
<label for="p2">$101 - $150</label>
</div>
</div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
function makeTable(data){
var tbl_body = "";
$.each(data, function() {
var tbl_row = "";
$.each(this, function(k, v) {
tbl_row += "<td>"+v+"</td>";
})
tbl_body += "<tr>"+tbl_row+"</tr>";
})
return tbl_body;
}
function getShoeOptions(){
var opts = [];
$checkboxes.each(function(){
if(this.checked){
opts.push(this.value);
}
});
return opts;
}
function updateShoe(opts){
$.ajax({
type: "POST",
url: "shoe.php",
dataType : 'json',
cache: false,
data: {filterOpts: opts},
success: function(data){
$('#shoe tbody').html(makeTable(data));
}
});
}
var $checkboxes = $("input:checkbox");
$checkboxes.on("change", function(){
var opts = getShoeOptions();
updateShoe(opts);
});
updateShoe();
</script>
</body>
</html>
<?php
$pdo = new PDO('mysql:host=localhost;dbname=filtertest', 'root', '******');
$select = 'SELECT id,brand,pPrice';
$from = ' FROM shoetest';
$where = ' WHERE TRUE';
$opts = isset($_POST['filterOpts'])? $_POST['filterOpts'] : array('');
//brand
if (in_array("Adidas", $opts)){
$where .= " AND brand ='Adidas'";
}
if (in_array("New Banlance", $opts)){
$where .= " AND brand ='New Banlance'";
}
if (in_array("PUMA", $opts)){
$where .= " AND brand ='PUMA'";
}
//price
if (in_array("1", $opts)){
$where .= " AND pPrice > 0 AND pPrice < 100";
}
if (in_array("2", $opts)){
$where .= " AND pPrice > 101 AND pPrice < 150";
}
$sql = $select . $from . $where;
$statement = $pdo->prepare($sql);
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($results);
echo($json);
?>
Thanks