When I click on "Asus" checkbox the result is all models of "Asus" with all 'sizes', good. But when I click on "Asus" AND "15''" checkboxes both, the result is all models of this size including "Acer" and so on. How it must work( at least in my imagination:)) - click both on "Asus" and "15''" and get data relating only to clicked (checked) checkboxes.
Thank you.
Checkboxes:
<input type="checkbox" name="Acer"> Acer
<input type="checkbox" name="Asus"> Asus
...
<input type="checkbox" name="15.6"> 15''
<input type="checkbox" name="17"> 17''
JQuery/Ajax:
$("input[type=checkbox]").click(function() {
var ids = [];
$("input[type=checkbox]:checked").each(function() {
ids.push($(this).attr("name"));
});
ids = ids.join(",");
$.get("mysql.php", {q: ids},
function(result) {
$("div#output").html(result);
});
});
MySQL query:
$g = $_GET['q'];
if(isset($g)) {
$param = "" . str_replace(",", "','", $_GET['q']) . "";
$sql = "SELECT * FROM `notebook` WHERE `trademark` IN ('$param') OR `size` IN ('$param') ORDER BY `trademark`";
$query = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($query)) {
echo '<p>'.$row['trademark'].' = '.$row['size'].'</p>';
}
}
you have 2 params to filter results, you must work with them separately.
my suggest:
checkboxes:
<input type="checkbox" name="trademark" value="Acer"> Acer
<input type="checkbox" name="trademark" value="Asus"> Asus
....
<input type="checkbox" name="size" value="15.6"> 15''
<input type="checkbox" name="size" value="17"> 17''
jquery:
$("input[type=checkbox]").click(function() {
var trade = [];
var sizes= [];
$("input[type=checkbox]:checked").each(function() {
if($(this).attr("name")=="trademark"){
trade.push($(this).val());
}
else{
sizes.push($(this).val());
}
});
trade = trade.join(",");
sizes= sizes.join(",");
$.get("mysql.php", {q: trade,p: sizes},
function(result) {
$("div#output").html(result);
});
});
the mysql:
$g = $_GET['q'];
$h = $_GET['p'];
$wheresql="";
$where=0;
if(isset($g)) {
$where=1;
$param = "" . str_replace(",", "','", $_GET['q']) . "";
$wheresql.="`trademark` IN ('$param')";
}
if(isset($h)) {
if($where==1){$wheresql.=" AND ";}
$where=1;
$param = "" . str_replace(",", "','", $_GET['p']) . "";
$wheresql.="`size` IN ('$param')";
}
if($where==1){
$sql = "SELECT * FROM `notebook` WHERE ".$wheresql." ORDER BY `trademark`";
$query = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($query)) {
echo '<p>'.$row['trademark'].' = '.$row['size'].'</p>';
}
}
little explanation: you have 2 groups of checkboxes distinguished by its name, and working with them separately in the jquery code, making possible send them to the mysql search in 2 variables, then in the mysql search script, for each variable do its "where" to do the search, the $where
variable have 2 purposes: first add the " AND "
to the sql if there are any trademark and size checked, second: check if there are any criteria to search (if there are called with no params, it wont do the search).
I have not tested the code :P
Sorry for my English