2011-11-25 135 views
-1

編輯:明白了,不能相信我錯過了 -PHP而與循環for循環的麻煩

$ HTTP_POST_VARS不是全球性的,它改變一個參數被傳入

我正常。使用$ _POST,但在oscommerce默認是這樣的,所以我用它來保持相似性。

我無法獲得此代碼的工作,我不知道爲什麼。

function check_product_available() { 

global $cart; 
$products = $cart->get_products(); 

    //product exclusion 
    //check to see if the product is in one of the limited categories 
    $check_product_query = tep_db_query($sql = 'SELECT products_id 
               FROM discount_coupons_to_products 
               WHERE coupons_id="'.tep_db_input($HTTP_POST_VARS['couponcart']).'"'); 
    $exlproducts = array(); 
    if(tep_db_num_rows($check_product_query) > 0) { 
     while($products = tep_db_fetch_array($check_product_query)) { 
     $exlproducts[] = $products['products_id']; 
     } 
    } 
     for ($i=0, $n=sizeof($products); $i<$n; $i++) { 
    if(in_array($products[$i]['id'], $exlproducts)) { 
     //use this to debug exclusions: 
     return false; 
    } 
    } 
    return true; 
    } 
    //end product exclusion 

我試着移動循環,它仍然總是返回true。我已經迴應了產品陣列和id在那裏,並運行SQL查詢hardcoding優惠券ID,它也可以正常工作。

+0

看來你不要在查詢中選擇任何「ID」字段,爲什麼你應該在關聯數組$ products中擁有它?這就是爲什麼你最後的if語句總是轉到else部分(返回true)。恕我直言。 – spider

+0

哪個循環總是返回true?如果它是'if(in_array($ products [$ i] ['id'],$ exlproducts)){ //使用它來調試排除: 返回false; } else { return true; }',我會建議打印出'products []'和'exlproducts'的全部內容,看看它們是否完全匹配。 – CHawk

回答

1

明白了....

$ HTTP_POST_VARS不是全球性的,它改變的參數中進行傳遞。

3

您的for循環沒有意義,因爲它只會循環一次。返回一個值將結束全部其他執行。你可能想要的是當它在數組中時返回false,在所有其他條件中返回true。

for ($i=0, $n=sizeof($products); $i<$n; $i++) { 
    if(in_array($products[$i]['id'], $exlproducts)) { 
     //use this to debug exclusions: 
     return false; 
    } 
} 
return true; 

編輯:您真的不應使用$HTTP_POST_VARS了。

+0

謝謝,我注意到並解決了這個問題,但仍然存在相同的問題。 –