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!

Extra Credit!: why is this code magically changing the #1 to the #10

Status
Not open for further replies.

leadman

Programmer
Joined
Jun 11, 2001
Messages
177
Location
US
Hi all - here is the puzzle:

My "add to cart button" points to this custom tag code:

<cfparam name=&quot;formVal&quot; default=&quot;Add to Cart&quot;>
<cfif isdefined(&quot;form.submit&quot;)>
<cfif form.submit eq FormVal>
<cf_addItem
strProdID=&quot;#form.addID#&quot;
strProdName=&quot;#form.addName#&quot;
curPrice=&quot;#form.addPrice#&quot;
quantity=&quot;#form.addQty#&quot;>
</cfif>
</cfif>
<cfoutput>
<center>
<FORM action=&quot;#getfilefrompath(cgi.script_name)#?first=#FirstRow#&searchcatagory=#searchcatagory#&textsearch=#textsearch#&querycount=#querycount#&strProdID=#strProdID#&quot; method=&quot;post&quot;>
<input type=&quot;hidden&quot; name=&quot;addName&quot; value=&quot;#strprodName#&quot;>
<input type=&quot;hidden&quot; name=&quot;addID&quot; value=&quot;#strProdID#&quot;>
<input type=&quot;hidden&quot; name=&quot;addPrice&quot; value=&quot;#curPrice#&quot;>
<input type=&quot;hidden&quot; name=&quot;addQty&quot; value=&quot;1&quot;>
<input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;#formVal#&quot;>
</form>
</center>
</cfoutput>

Notice that the value for the &quot;addQty&quot; field is &quot;1&quot; (the rest of the values are being filled in correctly by the variables). Now - if you submit the above form, it submits to itself and the code at the top (in the cfif tag) goes into effect and the values are passed to the cf_addItem custom tag (addQty should still be 1, right?). That tag contains the following (sorry about the long post but i cant zero in any further):

<!--- Make sure that all of the necessary attributes are passed in --->
<cfif isdefined(&quot;attributes.strProdID&quot;) and isdefined(&quot;attributes.strProdName&quot;) and isdefined(&quot;attributes.curPrice&quot;) and isdefined(&quot;attributes.quantity&quot;)>
<cfscript>
if (not(isdefined(&quot;session.cart&quot;))) {
// Check to make sure that the Shopping cart structure exists.
session.cart = structnew();
}

// The item structure we are going to use to store the items in the cart
// is going to have four parts...
// 1. The item id
// 2. The item name
// 3. The price per unit
// 4. The quantity

tempvalue = listtoarray('#attributes.strProdID#,#attributes.strProdName#,#attributes.curPrice#,#attributes.quantity#');

// if the item is not yet in the cart, simply add it to the cart
if (not(structKeyExists(session.cart, attributes.strProdName))) {
StructInsert(session.cart,attributes.strProdname,tempvalue);
}

// if the item is already in the cart, update the item quantity
else {
tempvalue[4]=session.cart[attributes.strProdname][4]+attributes.quantity;
StructUpdate(session.cart,attributes.strProdName,tempvalue);
}
</cfscript>
</cfif>

WHEW! Okay now, the only part of that i dont really get is the &quot;listtoarray&quot; thing so maybe that is wheer the problem is but when you go to view the contents of the shopping cart, the quantity is 10 not 1! Can anyone figure this out?
(just for fun, here is the relevant code for viewing the cart):

<!--- Make sure that the shopping cart structure exists and is the correct format --->
<cfif isdefined(&quot;session.cart&quot;)>
<cfif IsStruct(session.cart)>

<!--- Initialize variables --->
<cfset subtotal = 0>

<cfoutput>
<cfloop collection=&quot;#session.cart#&quot; item=&quot;i&quot;>

<cfset subtotal = subtotal + (session.cart[4] * session.cart[3])>
<tr>
<td valign=top><font face=&quot;arial&quot; size=2><a href=&quot;productpage.cfm?strProdID=#session.cart[1]#&quot;>#session.cart[2]#</a></font></td>
<td valign=top>
<table border=0 width=100%>
<tr>
<td><font face=&quot;arial&quot; size=3>#session.cart[4]#</font></td>
<form method=&quot;post&quot;>
<input type=&quot;hidden&quot; name=&quot;name&quot; value=&quot;#session.cart[2]#&quot;>

<input type=&quot;hidden&quot; name=&quot;id&quot; value=&quot;#session.cart[1]#&quot;>
<input type=&quot;hidden&quot; name=&quot;price&quot; value=&quot;#session.cart[3]#&quot;>
<input type=&quot;hidden&quot; name=&quot;quantity&quot; value=&quot;1&quot;>
<td align=right>
<input type=&quot;submit&quot; name=&quot;action&quot; value=&quot; + &quot;> <input type=&quot;submit&quot; name=&quot;action&quot; value=&quot; - &quot;></td>
</form>
</tr> </table>
</td>
 
Be sure this code is not in a loop:

<cfparam name=&quot;formVal&quot; default=&quot;Add to Cart&quot;>
<cfif isdefined(&quot;form.submit&quot;)>
<cfif form.submit eq FormVal>
<cf_addItem
strProdID=&quot;#form.addID#&quot;
strProdName=&quot;#form.addName#&quot;
curPrice=&quot;#form.addPrice#&quot;
quantity=&quot;#form.addQty#&quot;>
</cfif>
</cfif>


You have 10 items/page so it is adding 10 items of each to cart? Hope that helps

 
Try this:

[COLOR=008080]<table border=1>[/color]

<cfif isdefined(&quot;form.action&quot;)>
<cf_addItem
strProdID=&quot;#form.ID#&quot;
strProdName=&quot;#form.Name#&quot;
curPrice=&quot;#form.Price#&quot;
quantity=&quot;#Trim(FORM.action)##form.quantity#&quot;>

</cfif>

[COLOR=666666]<!--- Make sure that the shopping cart structure exists and is the correct format --->[/color]
<cfif isdefined(&quot;session.cart&quot;) AND IsStruct(session.cart)>

[COLOR=666666]<!--- Transfer SESSION.cart to REQUEST.cart --->[/color]
<CFLOCK SCOPE=&quot;SESSION&quot; TYPE=&quot;READONLY&quot; TIMEOUT=&quot;10&quot;>
<CFSET REQUEST.cart = SESSION.cart>
</CFLOCK>

[COLOR=666666]<!--- Initialize variables --->[/color]
<cfset subtotal = 0>

<cfoutput>
<cfloop collection=&quot;#request.cart#&quot; item=&quot;i&quot;>
<cfset subtotal = subtotal + (request.cart
Code:
[
i
Code:
]
Code:
[
4
Code:
]
* request.cart
Code:
[
i
Code:
]
Code:
[
3
Code:
]
)>

[COLOR=008080]<tr>[/color]
[COLOR=008080]<td valign=top>[/color]
[COLOR=000080]<font face=&quot;arial&quot; size=2>[/color]
<a href=&quot;productpage.cfm?strProdID=#request.cart
Code:
[
i
Code:
]
Code:
[
1
Code:
]
#&quot;
>

#request.cart
Code:
[
i
Code:
]
Code:
[
2
Code:
]
#
</a>
[COLOR=000080]</font>[/color]
[COLOR=008080]</td>[/color]
[COLOR=008080]<td valign=top>[/color]
[COLOR=008080]<table border=0 width=100%>[/color]
[COLOR=008080]<tr>[/color]
[COLOR=008080]<td>[/color]
[COLOR=000080]<font face=&quot;arial&quot; size=3>[/color]
#request.cart
Code:
[
i
Code:
]
Code:
[
4
Code:
]
#
[COLOR=000080]</font>[/color]
[COLOR=008080]</td>[/color]
[COLOR=000080][COLOR=FF8000]<form method=&quot;post&quot;>[/color][/color]
[COLOR=000080][COLOR=FF8000]<input type=&quot;hidden&quot; name=&quot;name&quot;
value=&quot;#request.cart
Code:
[
i
Code:
]
Code:
[
2
Code:
]
#&quot;
>[/color][/color]
[COLOR=000080][COLOR=FF8000]<input type=&quot;hidden&quot; name=&quot;id&quot;
value=&quot;#request.cart
Code:
[
i
Code:
]
Code:
[
1
Code:
]
#&quot;
>[/color][/color]
[COLOR=000080][COLOR=FF8000]<input type=&quot;hidden&quot; name=&quot;price&quot;
value=&quot;#request.cart
Code:
[
i
Code:
]
Code:
[
3
Code:
]
#&quot;
>[/color][/color]
[COLOR=000080][COLOR=FF8000]<input type=&quot;hidden&quot; name=&quot;quantity&quot; value=&quot;1&quot;>[/color][/color]
[COLOR=008080]<td align=right>[/color]
[COLOR=000080][COLOR=FF8000]<input type=&quot;submit&quot; name=&quot;action&quot; value=&quot; + &quot;>[/color][/color]
[COLOR=000080][COLOR=FF8000]<input type=&quot;submit&quot; name=&quot;action&quot; value=&quot; - &quot;>[/color][/color]
[COLOR=008080]</td>[/color]
[COLOR=000080][COLOR=FF8000]</form>[/color][/color]
[COLOR=008080]</tr>[/color]
[COLOR=008080]</table>[/color]
[COLOR=008080]</td>[/color]
[COLOR=008080]</tr>[/color]
</cfloop>
</cfoutput>
</cfif>

[COLOR=008080]</table>[/color] - tleish
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top