2013-08-29 39 views
1

我有以下搜索表单:问题用PHP搜索结果

<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个滤波器(进口,新鲜,标签,包装,有机)。我的问题是,如果,例如,当我点击导入并包装在搜索表单中,我只得到“导入”的,我知道有产品“导入”和“包装”。我的查询应该如何看待任何组合的正确结果? 非常感谢!

+0

1.不要在db中存储'Yes/No',而是存储'0/1'; 2.爆炸()'为了得到ID是异常数据库的气味 - 我建议你了解'JOIN'操作。 – moonwave99

+0

该平台是Magento,并且所有这5个属性($ attribute_imported等)都具有“是/否”值 – user2727341

回答

0
  1. 如果设置了帖子属性,请编写SQL代码以检查是否满足条件。这将在稍后的连接中使用。

  2. 请勿查询您的所有产品。写一个查询,其中加入productentity_attribute_set。在包含步骤1中生成的条件的地方使用where子句。

  3. 遍历结果并显示结果。

您正向数据库服务器发送大量查询。通过发送单个查询来减少它们的数量。您正在检索许多不必要的记录。过滤出来。在你的查询中,where条件使用or操作对第1步中产生的条件进行操作。

+1

此外,只有在复选框被选中时,您的PHP变量才会被设置。最好这样做:'$ qImported =(isset($ _ POST ['imported'])&& $ _POST ['imported'] == 1)? '是':'不';'这样它们将以任何方式设置。 – Revent

+0

不错的一个,@Revent,我忘了添加到答案。你是绝对正确的。 –