Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

StructInsert order problem

Status
Not open for further replies.

pixiesfb

Programmer
Apr 27, 2001
62
US
I have a shopping cart. When the customer proceeds to checkout, they go to a new domain. Session variables cannot be passed, so I must re-build my cart on the new domain. Trouble is, the structure is re-built in a different order and I can't figure out why. There is no pattern, it is not always reversed, for example.

Before checkout, I have a form with hidden fields which are built dynamically. Might look like:

<form action=&quot;newpath/newpage.cfm&quot; method=&quot;POST&quot;>

<input type=&quot;hidden&quot; name=&quot;CartList1&quot; value=&quot;54,Book titleA,25.0,20.0,1&quot;>

<input type=&quot;hidden&quot; name=&quot;CartList2&quot; value=&quot;234,Book titleB,30.0,10.0,1&quot;>

<input type=&quot;hidden&quot; name=&quot;CartList3&quot; value=&quot;21,Book titleC,10.0,5.0,1&quot;>

<input type=&quot;hidden&quot; name=&quot;lastrownum&quot; value=&quot;3&quot;>
</form>

On next page, (in new domain), I re-build the cart:

<cfset session.newcart = structnew()>

<cfloop index=&quot;x&quot; from=&quot;1&quot; to=&quot;#form.lastrownum#&quot;>
<cfset z=Evaluate(&quot;CartList&quot;&&quot;#x#&quot;)>
<cfset tempvalue = listtoarray(z ,&quot;,&quot;)>
<cfset StructInsert(session.newcart,z,tempvalue)>
</cfloop>

When I want to look at Book Titles in my structure, I do a collection loop:

<cfloop collection=&quot;#session.newcart#&quot; item=&quot;i&quot;>
#session.cartsecure[2]#<br>
</cfloop>

The titles come out in different order!

Book TitleB
Book TitleA
Book TitleC

It would seem that they should keep the same order when new cart is build, but it does not! Any help is appreciated!
 
In the StructInsert() function, the code is inserting the key z, which is really a list of items (54,Book titleA,25.0,20.0,1). Try changing it to something like CartList#x#.

=== START CODE EXAMPLE ===
<cfset session.newcart = structnew()>

<cfloop index=&quot;x&quot; from=&quot;1&quot; to=&quot;#form.lastrownum#&quot;>
[COLOR=666666]<!--- Read from FORM fields as array instead of Evaluate to speed up code --->[/color]
<cfset z = FORM
Code:
[
&quot;CartList#x#&quot;
Code:
]
>

<cfset tempvalue = listtoarray(z ,&quot;,&quot;)>
<cfset StructInsert(session.newcart, &quot;CartList&quot; & x, tempvalue)>
</cfloop>
=== END CODE EXAMPLE ===

I didn't understand why the code is looping session.newcart but outputing session.cartsecure, so I modified it this way. If it needs to be different, perhaps you could explain why.

=== START CODE EXAMPLE ===
<cfloop collection=#session.newcart# item=&quot;i&quot;>
#session.newcart
Code:
[
i
Code:
]
Code:
[
2
Code:
]
#[COLOR=000080]<br>[/color]
</cfloop>
=== END CODE EXAMPLE === - tleish
 
Thanks a lot,

<cfset z = FORM[&quot;CartList#x#&quot;]> did not seem to work, but


<cfset StructInsert(session.newcart, &quot;CartList&quot; & x, tempvalue)>

did, and it solved the ordering problem, although I'm still not sure why.

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top