Hi All. Im new to the forum and hope to find some help here.
I’m making a very small shopping cart (with Arrays and Structures) which I found on easycfm. Most of the time it is working but fine but then suddenly I get a very upredictable sequence I get error messages like:
Element PRODUCTID is undefined in a CFML structure referenced as part of an expression.
Another small negativity is that I’m only able to enter the quantity for a product on the shopping/product page (product.cfm). I’m looking for a way to update the quantity on the cart page(cart.cfm) before checking out. I tried everything. I included a textfield in my cart page and then created a update page but with what ever I tried I got all kind of error messages.
I have to admit Arrays and Structures are not my strongest point and I hope that someone here can push me in the right direction
Hi cfStarlight, It was completely my wrong I worked to long on this without any sleep i quess. The problem was with my hidden fields! The where not inside the CFoutput/CFloop so it was a undefined element. So now everything works fine.
But I have to thank you for your input though. It brought me to another way of looking to the structure.
No the productID can only exist once that’s why I was doubting about the cfloop?
No, assuming you’re on CF8 - you’re totally right about needing a cfloop. There’s no ArrayFind(), like in CF9. So you just have to loop through the array until you find the productID you want.
Since the id’s are unique, the one small improvement I’d suggest is cfbreak’ing out of the loop once you find a match. Because there’s no need to keep searching once you’ve found the ID you want.
Hi cfStarlight, thank you for that last tip about the break. Good to know.
I’m now struggling with one last thing. It is similar to my former question though. This has to do with a double entry! Someone add a product to the cart that is already there. In that case I would just like the quantity to be updated. In the addtocart page (addtocart.cfm), I have except for the array and structure creation a similar code as on the update page:
Element 1 is undefined in a CFML structure referenced as part of an expression.
I don’t see what I’m doing wrong here It is nearly the same as the update page from before except that here the quantity is made up from the old quantity + the new quantity and the fact that on the product page (where you add the item) the Form.pid the value from the query (#getProducts.productID#) has and on the cart page (from my former question) part of the structure is (session.cart[i].productID#). What do you think i’m doing wrong?
When I press the back button and add another product every thing is okay.
Hey thank you so much for your reply. No the productID can only exist once that’s why I was doubting about the cfloop?
Anyway I tried your suggestion and I didn’t get a match at first but because of that I could figure what I was doing wrong. The pid in the form wasn’t declared the right way.
The code looks fine to me and works for me - assuming the element already exists in the cart.
Which line is CF pointing to when the error “Element 1 is undefined in a CFML structure referenced as part of an expression” occurs? Also, if you dump session.cart# before the code that errors, what are the contents? Maybe that will give some clues about the source of the problem.
Sorry I can’t help with your problem. It’s been many many years since I even looked at ColdFusion (4.0). But as far as editing posts, the current window is 30 minutes.
The latest code looks right ie Search for the ID in the array, then update it. Your best bet is to to add some debugging code, so you can see the values and why it’s not updating. Somethin like
<cfoutput>
<cfloop index="thisCartItem" from="1" to="#arrayLen( session.cart )#">
debugging: checking index #thisCartItem# against #form.pid#
<cfif session.cart[thisCartItem].productID EQ form.pid>
match found. OLD qty = #session.cart[thisCartItem].quantity#
<cfset session.cart[thisCartItem].quantity = form.qnt>
NEW qty = #session.cart[thisCartItem].quantity#
<cfelse>
no match
</cfif><br>
</cfloop>
</cfoutput>
Btw, can the productID exist in the array more than once?