2017-08-25 160 views
0

在我的代碼中,我正在查詢我的數據庫以獲取城市。ColdFusion如果語句值沒有返回true,而是返回false

我寫了一條if語句,說明這個城市是否等於db中的那個值。返回這個城市的形象。但是,它正在返回一個不同的圖像,它將轉到我的if語句的錯誤塊。

我做了一個城市變量的cfdump,它返回了我想要的城市,但由於某種原因,它返回的是錯誤而不是真實的。我不確定我做錯了什麼。這是我的代碼。

<cfloop query="testData"> 
    <cfif #city# EQ 'Portland'> 
     <!--- I want it to go to this block ---> 
     <img src="images/portlandcity.jpg" alt="Portland City"> 
    <cfelseif #city# EQ 'San Jose'> 
     <img src="images/sanjosecity.jpg alt="San Jose City"> 
    <cfelse> 
     <!-- its going to this block instead of going to my Portland city block. ---> 
     <img src="images/randomcityimage.jpg alt="False block"> 
    </cfif> 

    <!-- Dumped out city variable in the loop and it returns 'Portland'. 
     However, it's going to the false block for some reason and I am not sure why ---> 
    <cfdump var="#city#"> 
</cfloop> 
+1

它的價值可能並不完全等於'波特蘭'。 –

+0

雖然。我做了一個城市變量的cfdump,並且它完全返回'Portland' – Curious13

+4

沒有多餘的空格?標籤?回報?不可見的角色,你不會看到在轉儲? –

回答

2

你可以嘗試在你的代碼下面,以防止空間的項目:

注意我拿出#在你的變量,並添加了TRIM()函數。 在If語句中,您不需要使用#號。

<cfif trim(city) EQ 'Portland'> 
    <img src="images/portlandcity.jpg" alt="Portland City"> <!--- I want it to go to this block ---> 
<cfelseif trim(city) EQ 'San Jose'> 
    <img src="images/sanjosecity.jpg alt="San Jose City"> 
<cfelse> 
    <img src="images/randomcityimage.jpg alt="False block"> <!-- its going to this block instead of going to my Portland city block. ---> 
</cfif> 
相關問題