Hey all i am trying to get my array in the correct format to be placed into an ASP.net webpage for a table for an order.
The order should be:
model_number
comm_category
service
freight
sales_tax
sales
unit_price
price
id_price
Currently it is outputting as this:
model_number
price
unit_price
id_price
sales_tax
sales
service
freight
comm_category
And because of this i am unable to append to my string in the correct order using my code here:
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
The end results are this:
<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>
Which should output like this:
<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>
How can i put the array in the correct order?
Why aren’t you just simply referencing the items you need directly, instead of looping through the associative keys (which come back in arbitrary order):
Or better yet, just get rid of
BuildItemsaltogether and inline the logic in the appropriate places above.