我有以下搜索表单:
<form action="/playsearch" method="post">
<input type="checkbox" value="1" name="imported" id="imported" class="">
<label class="" for="imported">Imported</label><br>
<input type="checkbox" value="1" name="fresh" id="fresh" class="">
<label class="" for="fresh">Fresh</label><br>
<input type="checkbox" value="1" name="labeled" id="labeled" class="">
<label for="labeled" class="">Labeled</label><br>
<input type="checkbox" value="1" name="wrapped" id="wrapped" class="">
<label for="wrapped" class="">Wrapped</label><br>
<input type="checkbox" value="1" name="organic" id="organic" class="">
<label for="organic" class="">Organic</label><br>
<button type="submit" name="fruitsearch">Submit</button>
</form>
在播放搜索页面我有这样的代码:
<?php
if(isset($_POST['imported']) && $_POST['imported'] == 1){$qImported = 'Yes';}
if(isset($_POST['fresh']) && $_POST['fresh'] == 1){$qFresh = 'Yes';}
if(isset($_POST['labeled']) && $_POST['labeled'] == 1){$qLabeled = 'Yes';}
if(isset($_POST['wrapped']) && $_POST['wrapped'] == 1){$qWrapped = 'Yes';}
if(isset($_POST['organic']) && $_POST['organic'] == 1){$qOrganic = 'Yes';}
if (isset($_POST['fruitsearch'])) { $fruitsearch= $_POST['fruitsearch']; }
if (isset($fruitsearch)) {
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$readresult = $write->query("SELECT DISTINCT product_id FROM catalog_category_product ORDER BY product_id");
while ($row = $readresult->fetch() ) {
$prodid = explode(" ", $row['product_id']);
foreach ($prodid as $id){
$_product = new Mage_Catalog_Model_Product();
$_product->load($id);
$attributeSetName = Mage::getModel('eav/entity_attribute_set')->load($_product->getAttributeSetId())->getAttributeSetName();
if($attributeSetName == 'Fruits'){
$attribute_imported = $_product->getAttributeText('is_imported');
$attribute_fresh = $_product->getAttributeText('is_fresh');
$attribute_labeled = $_product->getAttributeText('is_labeled');
$attribute_wrapped = $_product->getAttributeText('is_wrapped');
$attribute_organic = $_product->getAttributeText('is_organic');
if($qImported == $attribute_imported && $qFresh == $attribute_fresh && $qLabeled == $attribute_labeled && $qWrapped == $attribute_wrapped && $qOrganic == $attribute_organic){
echo $name.'<br/>';
}
}
}
}
}
?>
一种产品可以有这5种过滤器(进口的、新鲜的、有标签的、包装的、有机的)。我的问题是,例如,当我点击导入并包装在搜索表单中时,我只得到"导入"的产品,并且我知道有"导入"answers"包装"的产品。对于任何组合,我的查询应该如何才能得到正确的结果?非常感谢!!!
-
如果设置了post属性,则编写SQL代码以检查是否满足条件。这将在稍后的联接中使用。
-
不要查询您的所有产品。编写一个连接
product
和entity_attribute_set
的单个查询。在包含步骤1中生成的条件的位置使用where子句。 -
遍历结果并显示结果。
您正在向数据库服务器发送大量查询。通过发送一个查询来减少它们的数量。您正在检索许多不必要的记录。把它们过滤掉。在where条件中的查询中,对步骤1中生成的条件使用or
操作。