我有以下搜索表單:問題用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個濾波器(進口,新鮮,標籤,包裝,有機)。我的問題是,如果,例如,當我點擊導入幷包裝在搜索表單中,我只得到「導入」的,我知道有產品「導入」和「包裝」。我的查詢應該如何看待任何組合的正確結果? 非常感謝!
1.不要在db中存儲'Yes/No',而是存儲'0/1'; 2.爆炸()'爲了得到ID是異常數據庫的氣味 - 我建議你瞭解'JOIN'操作。 – moonwave99
該平臺是Magento,並且所有這5個屬性($ attribute_imported等)都具有「是/否」值 – user2727341