2011-03-02 67 views
2

我正在輸出帶有下面列出的子類別的類別。如果它不是循環中的第一個項目,則每個子類別都會獲得一個逗號。輸出創建不需要的空格

此外,我只顯示四個結果,所以如果記錄數超過四個,我需要在第四個循環結果的末尾附加...

問題是,在應用了...的情況下,每個子類別之後都有一個額外的空間。見下: enter image description here

看看逗號之前有空格嗎?

代碼:

<ul class="defaultUL" style="float:right;"> 
      <cfloop query="getParent" startrow="7" endrow="12"> 
       <cfquery name="getSubCategory" datasource="dss"> 
       SELECT Name, ID FROM Category WHERE ParentID = #getParent.ID# 
       </cfquery> 
      <cfset SubNumb = getSubCategory.recordcount> 

       <li><h3><a href="?Page=#Application.Utility.qsEncode(getParent.Name)#">#getParent.Name#</a></h3> 
        <cfloop query="getSubCategory" startrow="1" endrow="#SubNumb#"> 
        <cfif SubNumb gt 4> 
         <cfif getSubCategory.currentRow lt 4 AND getSubCategory.currentRow gt 1> 
          , #getSubCategory.Name# 
       <cfelseif getSubCategory.currentRow eq 1> 
          #getSubCategory.Name# 
          <cfelseif getSubCategory.currentRow eq 4> 
          #getSubCategory.Name#... 
         </cfif> 
         <cfelse> 
         #getSubCategory.Name#, 
        </cfif> 

        </cfloop> 
        </li> 
      </cfloop> 
      </ul> 

我確信,在數據庫中的數據在最後沒有空格。

回答

7

使用listAppend功能來構建你的字符串:

<cfset subCatList = "" /> <!--- define a variable to hold the list of subcats; variable gets reset for each iteration of outer loop ---> 
<cfloop query="getSubCategory" startrow="1" endrow="4"> 
    <!--- listAppend uses , as a the default delimiter. ---> 
    <cfset subCatList = listAppend(subCatList, getSubCategory.Name) /> 
</cfloop> 
<cfif getSubCategory.RecordCount gt 4> 
    <cfset subCatList = listAppend(subCatList,"...") /> 
</cfif> 
<!---- value of subCatList at this point: subcat1,subcat2,subcat3,subcat4... ---> 
<!--- output subcatlist and fix spacing ---> 
#replace(subCatList, ",", ", ","all"# 
<!--- output is subcat1, subcat2, subcat3, subcat4... ---> 
3

在頂部使用<cfsetting enablecfoutputonly="true" />,在底部使用<cfsetting enablecfoutputonly="false" />。然後使用<cfoutput></cfoutput>明確定義應輸出到瀏覽器的內容。