2015-09-10 63 views
3

我很抱歉壞頭銜,但我不知道更好的選擇。PHP從類型和名稱搜索

當前我正在創建搜索引擎以獲取產品詳細信息。我有2種搜索類型,第一種是使用產品類型進行搜索,另一種是使用產品名稱。

下面是搜索代碼:

<?php 
$search_exploded = explode (" ", $search); 

$x = ""; 
$construct = ""; 

foreach ($search_exploded as $search_each) { 
    $x++; 
    if ($x==1) 
     $construct .="product_type LIKE '%$search_each%'"; 
    else 
     $construct .="AND product_type LIKE '%$search_each%'"; 
} 
?> 

,這是數據獲取的代碼。

$results = $mysqli->query("SELECT * FROM produk2 WHERE $construct"); 
if ($results) { 

    //fetch results set as object and output HTML 
    while ($obj = $results->fetch_object()) { 
     echo '<div class="product">'; 
     echo '<form method="post" action="cart_update.php">'; 
     echo '<div class="product-thumb"><img src="images/'.$obj- >product_img_name.'"></div>'; 
     echo '<div class="product-content"><h3>'.$obj->product_name.'</h3>'; 
     echo '<div class="product-desc">'.$obj->product_desc.'</div>'; 
     echo '<div class="product-info">'; 
     echo 'Price '.$currency.$obj->price.' | '; 
     echo 'Qty <input type="text" name="product_qty" value="1" size="3" />'; 
     echo '<button class="add_to_cart">Add To Cart</button>'; 
     echo '</div></div>'; 
     echo '<input type="hidden" name="product_code" value="'.$obj->product_code.'" />'; 
     echo '<input type="hidden" name="type" value="add" />'; 
     echo '<input type="hidden" name="return_url" value="'.$current_url.'" />'; 
     echo '</form>'; 
     echo '</div>'; 
    } 

如何從product_type數據搜索改爲product_name,反之亦然,根據什麼我是從用戶輸入的故事嗎?

因爲我英語不好,我無法更好地解釋它。

+2

您的結構'連接缺少空間的一件事。你應該得到語法錯誤,但你可能沒有檢查它們。例如'。=「AND'應該讀作''=」AND' –

+0

在頭部的頂部,我使用$ search = $ _GET ['search_value'];從另一個頁面獲取數據。 –

+0

fred,對於product_type,構造工作得很好,但我不知道如何在產品類型和名稱之間進行更改 –

回答

4

您可以使其成爲OR條件,而不是像下面那樣。這樣,無論uset類型將應用於這兩列,並且因爲它是一個OR條件,它將返回true,如果兩者匹配。

where product_type LIKE '%$search_each%' 
or product_name LIKE '%$search_each%'; 

修改你的下面的代碼段

if ($x==1) 
    $construct .="product_type LIKE '%$search_each%'"; 
else 
    $construct .="AND product_type LIKE '%$search_each%'"; 

$construct .="product_type LIKE '%$search_each%' OR product_name LIKE '%$search_each%'"; 
+0

如何在我的源代碼中實現? –

+0

@MohdFadli,如果有幫助,請參閱編輯答案。 – Rahul

0

你說的英語很差,所以請糾正我,如果我誤解了問題:您要使用單個方法根據搜索類型搜索兩個字段之一?

如果是這樣的話:這是我經常使用的設計模式。它基本上是一個類似於工廠的映射器類。我在這裏使用了一個類,因爲我通常做的遠不止這些(有它選擇不同的表和各種各樣的東西)。如果您只需映射兩個字段,則可以將其重寫爲程序。

<?php 
$search_exploded = explode(" ", $search); 

$x = ""; 
$construct = ""; 

$mapper = new Mapper(); 

foreach ($search_exploded as $search_each) { 
    $x++; 

    // pass in what you want to map. in this example, it will take either prodtype or prodname, and return the field that you want 
    $tablename = $mapper->getMap(); 
    if ($x == 1) 
    $construct .= "$tablename LIKE '%$search_each%'"; 
    else 
    $construct .= "AND $tablename LIKE '%$search_each%'"; 

} 

class Mapper 
{ 
    public $map = array(
     'prodtype' => 'product_type', 
     'prodname' => 'product_name' 
); 

    public function getMap($forsearch) 
    { 
    return $this->map[$forsearch]; 
    } 
} 
0

嘗試將您的代碼更改爲。

foreach ($search_exploded as $search_each) { 
    $x++; 
    if ($x==1) 
     $construct .="product_type LIKE '%$search_each%'"; 
    else 
     $construct .="OR product_type LIKE '%$search_each%'"; 
} 
?> 

在這種情況下,無論用戶給出什麼輸入,它都會得到數據。