gpt4 book ai didi

php - 产品过滤器 - 计算产品数量

转载 作者:行者123 更新时间:2023-11-29 12:02:49 26 4
gpt4 key购买 nike

我用 php、mysql 和 jquery 制作了一个简单的产品过滤器。我有一个名为“spec_laptop”的数据库,我在名为“brand”的列后选择产品。 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 和 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>

最佳答案

要获取查询中的计数,请将其更改为:

$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
}
?>

关于php - 产品过滤器 - 计算产品数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32080506/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com