2016-10-18 49 views
0

嗨,大家好我想在我的PHP中執行多個條件搜索,所以我想使用foreach根據用戶選擇的數量自動添加AND語句。下面是我的代碼:[PHP]如何使用foreach將AND或OR語句添加到sql查詢中?

$sub = trim(ucfirst($_POST['sub'])); 
$any = trim(ucfirst($_POST['key'])); 
$count = 0; 
$query = "SELECT P.PTY_ID, P.PTY_UNITNUM, P.PTY_STREET, P.PTY_POSTCODE, P.PTY_SUBURB,P.PTY_CITY, T.P_TYPE_NAME, 
        L.SALE_PRICE, L.AVAILABILITY, R.PTY_ID,F.FEAT_ID, R.FEAT_ID,F.FEAT_NAME 
      FROM PROPERTY_TYPE T, PROPERTY P, LISTINGS L, PROPERTY_FEATURE R, FEATURE F 
      WHERE P.P_TYPE_ID = T.P_TYPE_ID(+) 
      AND P.PTY_ID = L.PTY_ID(+) 
      AND R.FEAT_ID= F.FEAT_ID 
      AND R.PTY_ID= P.PTY_ID 
      AND REGEXP_LIKE (P.PTY_SUBURB,'$sub') 
      AND REGEXP_LIKE (P.PTY_STREET||P.PTY_POSTCODE,'$any') 
      OR(
       ".foreach ($_POST["check"] as $feat_id) { 
        if ($count != 0) 
         echo "AND"; 
        echo "FEAT_ID = ' . $feat_id . '"; 
        $count++; 
       }.")"; 

$stmt = oci_parse($conn, $query); 
oci_execute($stmt); 

所以在我的情況,我想郊區,一些關鍵字,例如郵政編碼和街道名稱,以及強大的搜尋性能。爲了響應用戶對多個功能的選擇,我嘗試使用foreach循環來打印「AND FEAT_ID =(發佈的ID)」,但在「OR」部分後面出現語法錯誤直到查詢結束,說預計會有一些表達。 我現在在PHP學習方面很新穎。有人能爲我指出正確的格式嗎?乾杯(:

+0

您應該學會使用正確的顯式'JOIN'語法,並且*從不*使用不贊成使用外部連接的'(+)'語法。 –

+0

好的,歡呼聲@ GordonLinoff – dhyy

回答

0

我不是100%肯定你的問題是什麼,但我想它清理乾淨一點再次,這是我的編碼風格,以便其他人可能仍會認爲這是可以改善

$sub = trim(ucfirst($_POST['sub'])); 
$any = trim(ucfirst($_POST['key'])); 
$count = 0; 
$or = ""; 

if (isset($_POST["check"])) 
{ 
    $featArray = []; 
    foreach ($_POST["check"] as $feat_id) 
    { 
     array_push($featArray, "FEAT_ID = " .$feat_id); 
    } 

    $or = implode(' AND ', $featArray); 
} 

$query = "SELECT P.PTY_ID, P.PTY_UNITNUM, P.PTY_STREET, P.PTY_POSTCODE, P.PTY_SUBURB,P.PTY_CITY, T.P_TYPE_NAME, 
        L.SALE_PRICE, L.AVAILABILITY, R.PTY_ID,F.FEAT_ID, R.FEAT_ID,F.FEAT_NAME 
      FROM PROPERTY_TYPE T, PROPERTY P, LISTINGS L, PROPERTY_FEATURE R, FEATURE F 
      WHERE P.P_TYPE_ID = T.P_TYPE_ID(+) 
      AND P.PTY_ID = L.PTY_ID(+) 
      AND R.FEAT_ID= F.FEAT_ID 
      AND R.PTY_ID= P.PTY_ID 
      AND REGEXP_LIKE (P.PTY_SUBURB, '$sub') 
      AND REGEXP_LIKE (P.PTY_STREET||P.PTY_POSTCODE, '$any')"; 

if ($or != null) 
    $query .= " OR (" . $or . ")"; 

$stmt = oci_parse($conn, $query); 
oci_execute($stmt); 

我也會檢查你的$ _POST ['sub']和$ _POST ['key']或者設置爲。

+0

這正是我想要的!十分感謝(: – dhyy

0

@Lee Stevens 謝謝你的想法! 爲了適合我的使用,我已將代碼修改爲:

<?php 
    $sub = trim(ucfirst($_POST['sub'])); 
    $any = trim(ucfirst($_POST['key'])); 
    $count = 0; 
    $and = ""; 
/* All the same above */ 

    if (isset($_POST["check"])) 
    { 
     $featArray = []; 
     foreach ($_POST["check"] as $feat_id) 
     { 
      array_push($featArray, "AND F.FEAT_ID = " .$feat_id); 
      /* Add an "AND" here coz the original codes can't print */ 
      /* the "AND" for some reasons */ 
     } 

     $and = implode(" ", $featArray); 
     /* Remove "AND" here */ 
    } 
    $query = "SELECT P.PTY_ID, P.PTY_UNITNUM, P.PTY_STREET, P.PTY_POSTCODE, P.PTY_SUBURB,P.PTY_CITY, T.P_TYPE_NAME, 
         L.SALE_PRICE, L.AVAILABILITY, R.PTY_ID,F.FEAT_ID, R.FEAT_ID,F.FEAT_NAME 
       FROM PROPERTY_TYPE T, PROPERTY P, LISTINGS L, PROPERTY_FEATURE R, FEATURE F 
       WHERE P.P_TYPE_ID = T.P_TYPE_ID(+) 
       AND P.PTY_ID = L.PTY_ID(+) 
       AND R.FEAT_ID= F.FEAT_ID 
       AND R.PTY_ID= P.PTY_ID 
       AND REGEXP_LIKE (P.PTY_SUBURB,'$sub') 
       AND REGEXP_LIKE (P.PTY_STREET||P.PTY_POSTCODE,'$any')"; 


    if (!empty($and)) 
     $query.="$and"; 

    $stmt = oci_parse($conn, $query); 
    oci_execute($stmt); 
?>