2014-05-21 66 views
0

我似乎遇到了另一塊似乎是超級基本的PHP的問題,但它不適用於我。大概簡單的PHP if/else語句不起作用

我的客戶(房地產網站)需要能夠有沒有價格的屬性是「價格應要求」或「拍賣」。目前,將價格字段留空僅允許一個。

我試圖改變如下代碼:

$listing_price_labels = array(
‘sold’ => __(‘Sold’, ‘wpsight’), 
‘rented’ => __(‘Rented’, ‘wpsight’), 
‘request’ => __(‘Price on request’, ‘wpsight’), 
‘auction’ => __(‘Auction’, ‘wpsight’), ***– Added this line*** 
); 

哪裏這段代碼被發現...

if(is_admin()) 
     $listing_price .= ‘<br />’ . wpsight_get_price_value(); 

    } elseif(empty($listing_price)) { 

     // When no price available Price on request 
     $listing_price = ‘<span class=」listing-price-on-request」>’ . $listing_price_labels['request'] . ‘</span><!– .listing-price-on-request –>’; 

    } elseif($listing_price = ‘auction’) { 

     // When price field contains ‘auction’ (case sensitive) 
     $listing_price = ‘<span class=」listing-price-on-request」>’ . $listing_price_labels['auction'] . ‘</span><!– .listing-price-on-request –>’; 

    } 



    function wpsight_get_price($post_id = '') { 

     // Get post ID from $post_id 

     if(empty($post_id)) 
      $post_id = get_the_ID();  

     // If still empty, return false 

     if(empty($post_id)) 
      return false; 

     // Set listing price labels 

     $listing_price_labels = array(
      'sold' => __('Sold', 'wpsight' ), 
      'rented' => __('Rented', 'wpsight' ), 
      'request' => __('Price on request', 'wpsight'), 
      'auction' => __('Auction', 'wpsight'), 
      ); 

     $listing_price_labels = apply_filters('wpsight_get_price_labels', $listing_price_labels); 

     // Get listing price 
     $listing_price = wpsight_get_price_value(); 

     // Get custom fields 

     $custom_fields   = get_post_custom($post_id); 
     $listing_status   = isset($custom_fields['_price_status'][0]) ? $custom_fields['_price_status'][0] : false; 
     $listing_availability = isset($custom_fields['_price_sold_rented'][0]) ? $custom_fields['_price_sold_rented'][0] : false; 

     // Create price output 

     if(! empty($listing_availability)) { 

      // When listing is not available 

      $sold_rented = ($listing_status == 'sale') ? $listing_price_labels['sold'] : $listing_price_labels['rented']; 

      // Display sold/rented bold red in admin 
      $style = is_admin() ? ' style="color:red;font-weight:bold"' : false; 

      $listing_price = '<span class="listing-price-sold-rented"' . $style . '>' . $sold_rented . '</span><!-- .listing-price-sold-rented -->'; 

      if(is_admin()) 
       $listing_price .= '<br />' . wpsight_get_price_value(); 

     } elseif(empty($listing_price)) { 

      // When no price available Price on request 
      $listing_price = '<span class="listing-price-on-request">' . $listing_price_labels['request'] . '</span><!-- .listing-price-on-request -->'; 

     } elseif($listing_price == "auction") { 

      // When price field contains 'auction' (case sensitive) 
      $listing_price = '<span class="listing-price-on-request">' . $listing_price_labels['auction'] . '</span><!-- .listing-price-on-request -->'; 

     } 

     return apply_filters('wpsight_listing_price', $listing_price); 

    } 

我敢肯定,我的語法必須僅僅是錯誤的,因爲在地方,它的代碼使任何寫入價格欄顯示「拍賣」的任何財產。

任何人都可以看到我做錯了什麼嗎?

+2

嘗試'$ listing_price =='auction''而不是'$ listing_price ='auction''。實際上,您將字符串「auction」分配給變量「$ listing_price」,而不是將它們進行比較。在這種情況下,您也可以使用'===',除非值和類型匹配,否則不會返回true。更多信息:http://www.php.net/manual/en/language.operators.comparison.php – Andre

+0

你可以在你的if語句之前回顯'$ listing_price'的值嗎? – Viscocent

+0

$ listing_price。='
'。 wpsight_get_price_value();使它成爲一個串聯的字符串。 } elseif(empty($ listing_price)){即使由於斷點返回false,也不會爲空。你可以使用一個字段來說明它是否被出售等,並使用if條件。您也可以爲每個條目應用條件顯示邏輯。你是否循環着每一件物品? – user3587554

回答

0

嘗試:

if(is_admin()){ 
    $listing_price .= ‘<br />’ . wpsight_get_price_value(); 

    } elseif(empty($listing_price)) { 

    // When no price available Price on request 
    $listing_price = ‘<span class=」listing-price-on-request」>’ . $listing_price_labels['request'] . ‘</span><!– .listing-price-on-request –>’; 
} 

BTW它不推薦使用if(is_admin())因爲它的只適用於1線。