[英]PHP-SQL - populate a dynamic filter list
I am trying to : 我在尝试着 :
At the moment, the filter dropdown contains by default all columns, in some scenarios ( based on the selected location ) this is okay on the other hand, if a location has only two products out of the 20 it is annoying to display in the filter drop-down features that are irrelevant and that will return no-result... 目前,默认情况下,过滤条件下拉列表包含所有列,在某些情况下(基于所选位置),这是可以的,如果某个位置只有20种产品中的两种产品,那么在过滤器中显示就很烦人了无关的下拉功能,这些功能将返回无结果...
This is a brief example of what I am trying to say ; 这是我想说的一个简短例子;
Database Table structure 数据库表结构
+------+------------+-------------+----------------+-------------+-------------+ | SKU | HDMI | 3D | Android | Ethernet | location | +------+------------+-------------+----------------+-------------+-------------+ |TV1X | 0 | 0 | 0 | 0 |all | |TV5X | 0 | 0 | 0 | 0 |fr uk usa | |TV3Z | 1 | 0 | 0 | 1 |usa | |TVH3 | 1 | 1 | 1 | 1 |mex | |TVH1 | 1 | 1 | 1 | 1 |fr es uk | +------+------------+-------------+----------------+-------------+-------------+
Features columns are of type bit
功能列的类型为
bit
Product display 产品展示
Let's say $location = "%".'usa'."%"
假设
$location = "%".'usa'."%"
$queryString = "SELECT * FROM `products` WHERE `location` LIKE :location" ;
$statement = $dbh->prepare($queryString)
$statement->bindParam(':location', $location, PDO::PARAM_STR);
$statement->execute();
$statement->setFetchMode(PDO::FETCH_ASSOC);
$result = $statement->fetchAll();
The query above will return TV5X
& TV3Z
which are related to USA, these products features that are 'TRUE' are HDMI
+ Ethernet
only where the remaining two columns I do not wish to display in the filter dropdown. 上面的查询将返回与美国有关的
TV5X
和TV3Z
,“ TRUE”这些产品功能只有HDMI
+ Ethernet
,而其余两列是我不希望在过滤器下拉列表中显示的。 This example might be more relevant when we many products/features and locations... 当我们有许多产品/功能和位置时,此示例可能更相关...
Default Filter List 默认过滤器列表
<li >
<button data-filter=".HDMI" >HDMI</button>
</li>
<li >
<button data-filter=".3D" >3D</button>
</li>
<li >
<button data-filter=".Android" >Android OS</button>
</li>
<li >
<button data-filter=".Ethernet" >Ethernet</button>
</li>
The default filter list is relevant to some countries but not USA 默认过滤器列表与某些国家/地区相关,但与美国无关
Dynamic Filter List 动态过滤器列表
Based on the retrieved products (two in this case), query the db and find which columns for these products have TRUE as value. 根据检索到的产品(在这种情况下为两个),查询数据库并查找这些产品的哪些列的值为TRUE。
<?php foreach( $feature as $feat) : ?>
<li >
<button data-filter=".<?php print //Col_Name ;?>" ><?php print ect.. ;?></button>
</li>
<?php endforeach; ?>
I apologize for the long post, I am trying to explain the best way I can. 我为冗长的帖子致歉,我想尽我所能解释最佳方法。
My question is how do build a query for this purpose ? 我的问题是如何为此构建查询?
You could do this in one additional query by selecting the sum of each column and then checking that the returned value is > 0: 您可以通过选择每一列的总和,然后检查返回的值是否大于0,来在另一查询中执行此操作:
SELECT SUM(`HDMI`) AS `HDMI`,
SUM(`3D`) AS `3D`,
SUM(`Android`) AS `Android`,
SUM(`Ethernet`) AS `Ethernet`
FROM `products`
WHERE `location` LIKE :location
Run this query after your first query, and the code to output the filter list simply becomes this: 在第一个查询之后运行此查询,输出过滤器列表的代码将变成以下代码:
foreach ($result as $row) {
foreach ($row as $column => $numProducts) {
if ($numProducts > 0) { // i.e., SUM($column) is > 0
$html .= "<li>
<button data-filter=\".{$column}\">{$column} ({$numProducts})</button>
</li>";
}
}
}
This also has the added bonus of giving you the number of products which have each feature. 这也给您增加了具有每种功能的产品数量的好处。 :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.