我在php,mysql和jquery中制作了一个简单的产品过滤器。我有一个名为" spec_laptop"的数据库。我选择名为"品牌"的专栏之后的产品。 It looks like here当我检查时,会向我显示带有"品牌"的产品。我想在每个复选框选项之前添加,这个"品牌"的产品数量。在数据库like this。
代码: PHP:
<?php
include ('connect.php');
if (isset($_POST["toshiba"])) {
$arguments[] = "brand LIKE '%Toshiba%'";
}
if (isset($_POST["lenovo"])) {
$arguments[] = "brand LIKE '%Lenovo%'";
}
if (isset($_POST["samsung"])) {
$arguments[] = "brand LIKE '%Samsung%'";
}
if(!empty($arguments)) {
$str = implode(' or ',$arguments);
$qry = "SELECT * FROM spec_laptop where " . $str . " ORDER BY id desc";
$row=mysql_query($qry) or die("Unable to select");
while($data = mysql_fetch_array($row))
{
echo $data['procesor']." ";
}
} else {
echo "nothing";
}
?>
HTML&amp; JAVASCRIPT:
<html>
<head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<form id="form" method="post" action="">
<input type="checkbox" name="toshiba" class="checkbox" <?=(isset($_POST['toshiba'])?' checked':'')?>/>Toshiba<?php ?><br>
<input type="checkbox" name="lenovo" class="checkbox" <?=(isset($_POST['lenovo'])?' checked':'')?>/> Lenovo<br>
<input type="checkbox" name="samsung" class="checkbox" <?=(isset($_POST['samsung'])?' checked':'')?>/>Samsung<br>
</form>
<script type="text/javascript">
$(function(){
$('.checkbox').on('change',function(){
$('#form').submit();
});
});
</script>
</body>
</html>
答案 0 :(得分:1)
要查询您的查询,请将其更改为:
$qry = "SELECT * FROM spec_laptop where " . $str . " ORDER BY id desc";
为:
$qry = "SELECT *, count(id) as 'count' FROM spec_laptop where " . $str . " GROUP BY brand ORDER BY id desc";
你的HTML代码好像是在php文件中,因为你也在那里使用php短代码。因此,我建议使用php通过选择查询和循环显示选项。 可以使用与上述类似的查询:
<?php
$qry = "SELECT brand as 'brandname', count(id) as 'count' FROM spec_laptop where " . $str . " GROUP BY brand ORDER BY id desc";
$row=mysql_query($qry) or die("Unable to select");
?>
<form id="form" method="post" action="">
<?php
while($data = mysql_fetch_array($row))
{
?>
<input type="checkbox" name="<?php echo $data['brandname'];?>" class="checkbox" <?php (isset($_POST['toshiba'])?' checked':'') ?>/><?php echo $data['brandname'] . ' [' . $data['count'] . ']';?><br>
<?php
}
?>