php搜索结果的问题
我有以下的搜索表格:php搜索结果的问题,php,magento,Php,Magento,我有以下的搜索表格: <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="
<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>
在playsearch页面中,我有以下代码:
<?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个过滤器的组合进口,新鲜,标签,包装,有机。我的问题是,例如,当我在搜索表单中单击imported and wrapped(导入并包装)时,我只会得到导入的产品,我知道有导入并包装的产品。对于任何组合,我的查询应该如何获得正确的结果?
非常感谢
如果设置了post属性,则编写SQL代码以检查是否满足条件。这将在以后的联接中使用
不要查询您的所有产品。编写一个查询,将产品与实体属性集连接起来。使用where子句,其中包括步骤1中生成的条件
迭代结果并显示结果
您正在向数据库服务器发送大量查询。通过发送单个查询来减少它们的数量。您正在检索许多不必要的记录。过滤掉它们。在where条件的查询中,对步骤1中生成的条件使用or操作。1。不在数据库中存储是/否,而是0/1;2.为了获取ID而进行的分解是非规范化数据库的味道-我建议您了解联接操作。平台是Magento,所有5个属性$attribute_都已导入,因此都有一个Yes/No值。此外,只有选中复选框,您的PHP变量才会被设置。最好这样做:$qiimported=isset$\u POST['imported']&&$\u POST['imported']==1?”是:'否';好吧,我忘了在答案中加上这个。你完全正确。