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 SkipVought 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
0
0
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>

It would seem that it would keep the same order, but it does not! Any help is appreciated!
 
When you create a structure or an array or combination of both and session that, you can bring to any form as long as the session is not timed out.

Looking at the codes give above, i suggest you use structure and array to construct your shopping cart easily.

Some simply on these:
<!--- define an array --->
<cfset session.aServCart = ArrayNew(1)>

<!--- append into array --->
<cfset i = ArrayLen(Session.aServCart) + 1>
<cfset session.aServCart = StructNew()>
<cfset session.aServCart.PID = pid>
<cfset session.aServCart.PName = pname>
<cfset session.aServCart.Price = pprice>
<cfset session.aServCart.Qty = pqty>

<!--- display the items in the cart --->
<cfoutuput>
<cfloop from=&quot;1&quot; to=&quot;#ArrayLen(session.aServCart)#&quot; index=&quot;i&quot;>
#session.aServCart.PName#
#session.aServCart.Price#
#session.aServCart.Qty#
</cfloop>
</cfoutput>

Another thing is you need to check whether the session cart is defined using this --> IsDefined(&quot;session.aServCart&quot;) so that you can start creating your cart array and also check whether the session cart is empty to avoid any empty data in the session cart --> ArrayIsEmpty(session.aServCart)

Good Luck

sshhz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top