2012-08-17 46 views
1

嘿所有我想讓我的數組以正確的格式放置到一個ASP.net網頁的訂單表。數組循環次序問題

的順序應該是:

model_number 
comm_category 
service 
freight 
sales_tax 
sales 
unit_price 
price 
id_price 

目前,它輸出爲這樣的:

model_number 
price 
unit_price 
id_price 
sales_tax 
sales 
service 
freight 
comm_category 

也正因爲如此,我無法在這裏使用我的代碼添加到我在正確的順序串:

Public Sub AssocArray_To_String(ByRef Output As System.Text.StringBuilder, ByVal AssocArrayInput As AssocArray) 
    For iParent As Integer = 0 To AssocArrayInput.Count - 1 
     Dim intX As Integer = 0 
     Dim arrParent As AssocArray = TryCast(AssocArrayInput.Item(iParent), AssocArray) 
     If arrParent Is Nothing Then Continue For 

     For iChild As Integer = 0 To arrParent.Count - 1 
      Call buildItems(PHPConvert.ToString(arrParent.Keys(iChild)), PHPConvert.ToString(arrParent.Values(iChild)), intX) 
      intX += 1 
     Next iChild 
    Next iParent 
End Sub 

Private Sub buildItems(ByVal nameOfItem As String, ByVal itemItself As String, ByVal intX As Integer) 
    Dim tmpStuff As Decimal = 0.0 
    Dim qty As Integer = 1 
    Dim model_number As String = "" 
    Dim comm_category As String = "" 
    Dim service As Double = 0 
    Dim freight As Double = 0 
    Dim sales_tax As Double = 0 
    Dim sales As Double = 0 
    Dim unit_price As Double = 0 
    Dim price As Double = 0 
    Dim id_price As Double = 0 

    If nameOfItem = "model_number" Then 
     model_number = itemItself 
     tableLoop += "<tr><td bgcolor=""#CCCCCC""><asp:Label ID=""item_count_" & intX & """ Text=""Label"">" & qty & "</asp:Label></td>" 
     tableLoop += "<td bgcolor=""#CCCCCC""><asp:Label ID=""model_number_" & intX & """ Text=""Label"">" & model_number & "</asp:Label></td>" 
    ElseIf nameOfItem = "comm_category" Then 
     comm_category = itemItself 
     tableLoop += "<td bgcolor=""#CCCCCC""><asp:Label ID=""comm_category_" & intX & """ Text=""Label"">" & comm_category & "</asp:Label></td>" 
    ElseIf nameOfItem = "service" Then 
     service = Format(Convert.ToDouble(itemItself), "$######.00") 
     tableLoop += "<td bgcolor=""#CCCCCC""><asp:Label ID=""service_" & intX & """ Text=""Label"">" & service & "</asp:Label></td>" 
    ElseIf nameOfItem = "freight" Then 
     freight = Format(Convert.ToDouble(itemItself), "$######.00") 
     tableLoop += "<td><asp:Label ID=""freight_" & intX & """ Text=""Label"">" & freight & "</asp:Label></td>" 
    ElseIf nameOfItem = "sales_tax" Then 
     sales_tax = Format(Convert.ToDouble(itemItself), "$######.00") 
     tableLoop += "<td><asp:Label ID=""sales_tax_" & intX & """ Text=""Label"">" & sales_tax & "</asp:Label></td>" 
    ElseIf nameOfItem = "sales" Then 
     sales = Format(Convert.ToDouble(itemItself), "$######.00") 
     tableLoop += "<td bgcolor=""#CCCCCC""><asp:Label ID=""sales_" & intX & """ Text=""Label"">" & sales & "</asp:Label></td>" 
    ElseIf nameOfItem = "unit_price" Then 
     unit_price = Format(Convert.ToDouble(itemItself), "$######.00") 
     tableLoop += "<td bgcolor=""#CCCCCC""><asp:Label ID=""unit_price_" & intX & """ Text=""Label"">" & unit_price & "</asp:Label></td></tr>" 
    ElseIf nameOfItem = "price" Then 
     price = Format(Convert.ToDouble(itemItself), "$######.00") 
     tableLoop += "<td><asp:Label ID=""amount_" & intX & """ Text=""Label"">" & price & "</asp:Label></td>" 
    ElseIf nameOfItem = "id_price" Then 
     id_price = Format(Convert.ToDouble(itemItself), "$######.00") 
     tableLoop += "<td><asp:Label ID=""id_price_" & intX & """ Text=""Label"">" & id_price & "</asp:Label></td>" 
    End If 
End Sub 

最終結果如下:

<tr><td bgcolor="#CCCCCC"><asp:Label ID="item_count_0" Text="Label">1</asp:Label></td> 
<td bgcolor="#CCCCCC"><asp:Label ID="model_number_0" Text="Label">TTN491-7BW</asp:Label></td> 
<td bgcolor="#CCCCCC"><asp:Label ID="sales_1" Text="Label">3084.63</asp:Label></td> 
<td><asp:Label ID="amount_2" Text="Label">3810</asp:Label></td> 
<td><asp:Label ID="sales_tax_3" Text="Label">290.37</asp:Label></td> 
<td><asp:Label ID="id_price_4" Text="Label">3810</asp:Label></td></tr> 
<td bgcolor="#CCCCCC"><asp:Label ID="comm_category_5" Text="Label">X24</asp:Label></td> 
<td bgcolor="#CCCCCC"><asp:Label ID="service_6" Text="Label">51</asp:Label></td> 
<td><asp:Label ID="freight_7" Text="Label">384</asp:Label></td> 
<td bgcolor="#CCCCCC"><asp:Label ID="unit_price_8" Text="Label">3135.63</asp:Label></td></tr> 

這應該是這樣的輸出:

<tr><td bgcolor="#CCCCCC"><asp:Label ID="item_count_0" Text="Label">1</asp:Label></td> 
<td bgcolor="#CCCCCC"><asp:Label ID="model_number_0" Text="Label">TTN491-7BW</asp:Label></td> 
<td bgcolor="#CCCCCC"><asp:Label ID="comm_category_5" Text="Label">X24</asp:Label></td> 
<td bgcolor="#CCCCCC"><asp:Label ID="service_6" Text="Label">51</asp:Label></td> 
<td><asp:Label ID="freight_7" Text="Label">384</asp:Label></td> 
<td><asp:Label ID="sales_tax_3" Text="Label">290.37</asp:Label></td> 
<td bgcolor="#CCCCCC"><asp:Label ID="sales_1" Text="Label">3084.63</asp:Label></td> 
<td bgcolor="#CCCCCC"><asp:Label ID="unit_price_8" Text="Label">3135.63</asp:Label></td></tr> 
<td><asp:Label ID="amount_2" Text="Label">3810</asp:Label></td> 
<td><asp:Label ID="id_price_4" Text="Label">3810</asp:Label></td></tr> 

我怎樣才能把數組中的正確的順序?

回答

1

你爲什麼不只是簡單引用需要直接(其中回來以任意順序)的項目,而不是通過關聯鍵循環:

Dim arrParent As AssocArray = TryCast(AssocArrayInput.Item(iParent), AssocArray) 
If arrParent Is Nothing Then Continue For 

Call buildItems("model_number", PHPConvert.ToString(arrParent.Values("model_number")), intX) 
Call buildItems("comm_category", PHPConvert.ToString(arrParent.Values("model_number")), intX) 
Call buildItems("service", PHPConvert.ToString(arrParent.Values("model_number")), intX) 
Call buildItems("freight", PHPConvert.ToString(arrParent.Values("model_number")), intX) 
'ETC 

或者更好的是,剛剛擺脫BuildItems完全並且在上面適當的地方嵌入邏輯。

+0

這個循環幾次(列表中可能有4個不同的項),所以我認爲它仍然可以工作? – StealthRT 2012-08-17 14:49:12

+1

是否所有的鍵都存在於所有的數組行中,或者某些項沒有所有鍵,例如'freight'不適用於某些條目?如果是這樣,這裏可能需要更多的工作。 – mellamokb 2012-08-17 14:50:28

+0

它們都一樣 - 每個陣列只能有一個以上的項目。如在model_number等等,然後它啓動數組1 model_number等等等,然後數組2 model_number等等等。 – StealthRT 2012-08-17 14:53:47

1

要調用

For iChild As Integer = 0 To arrParent.Count - 1 
     Call buildItems(PHPConvert.ToString(arrParent.Keys(iChild)), 
         PHPConvert.ToString(arrParent.Values(iChild)), 
         intX) 
     intX += 1 
Next iChild 

爲此你總是會得到在arrParent該項目的順序。因此,所有你需要做的ist調用buildItems(...)以正確的順序

Call buildItems(PHPConvert.ToString("model_number"), 
       PHPConvert.ToString(arrParent.Values("model_number")), 
         intX) 
Call buildItems(PHPConvert.ToString("comm_category"), 
       PHPConvert.ToString(arrParent.Values("comm_category")), 
         intX) 

' and so on 

此外,我不能在buildItems找到一個定義tableLoop手動。看起來這是一個簡單字符串變量。你應該使用StringBuilder,因爲這個類在連接字符串方面更快!見MSDN

+0

我得到錯誤**從字符串「model_number」轉換爲類型「integer」的錯誤無效** ? – StealthRT 2012-08-17 15:05:08

+1

什麼類型是assocArray?找不到任何定義。對不起,我認爲可以按名稱訪問此數組中的項目!如果你知道任何財產的索引號碼,你可以取代它。例如,如果「comm_category」的索引號爲1,只是將代碼更新爲arrParent.Values(1) – 2012-08-17 15:10:16

+0

我無法使用** arrParent.Values(0)**,** arrParent.Values(1)**,等等,如果它有多個項目在數組中...它只是不斷重複相同的值達到值(10) – StealthRT 2012-08-17 15:30:50