Think about it as a series of functions. one of the functions is to
add an item to the cart. For simplicity's sake lets call it
"#addtocart()#. This function adds a single item to the cart. It
will validate the part number against the catalogue, work out the
price, any freight cost and update the totals of the cart. If you
use CFCs, you can make addtocart() one of the methods of the
shoppingcart.cfc. You'd call it with something like the following:
<cfset cart = addtoCart( itemToAdd ,qtyToAdd ) />
If you name your variables with something meaningful, you can almost
read that aloud and it tells you what happens - "ädd to the cart,
item ID, quantity"
Once you have that part figured out, all you have to do is figure
out how to loop over the list, calling that function for each item.
In your form, you have a series of input fields, called item_#id#
with a quantity input to each. Now in your form processing script,
you have something like this:
<cfloop list="#form.fieldlist#" index="i">
<!---- check if there is a value in the field, if so, add it ---->
<cfif structKeyExists (form, "#i#") AND val( trim(i) ) >
<!--- now figure out the item ID to add, and the quantity --->
<cfset itemToAdd = replace( i , "item_", "", "ÄLL" ) />
<cfset qtyToAdd = trim( evaluate("form." & i ) ) />
<!-- lastly call the function that adds the item to the cart --->
<cfset cart = addtoCart( itemToAdd ,qtyToAdd ) />
</cfif>
</cfloop>
I find with these things that separating each element out into their
own piece of processing helps get my mind around quite complex things.
CFCs work well with this approach, because if you break up a
process into a number of small elements, usually it neatly fits into
a method for each element. You can often abbreviate my code by
combining some of these elements, doing more than one function in one
line, but I like to make it easily readable. I find that a year
down the track, it's easier to maintain things if you dont have to
wrack your brains trying to remember why you did something that way.
If there are lots of comments in the code, and each element is split
into its own bit of code, you can run through it much faster. And
any developer following you on this job will also have an easier time
of it too.
Anyway, for my simple brain, its a lot easier to figure out how to get
the value of a field, then figure out how to get the name of an item
from the field name, then do a loop over a list than try to grasp
the whole thing all at once.
DISCLAIMER: i havent actual'ly run this so you'll need to debug it.
Hope this helps.
Cheers
Mike Kear
Windsor, NSW, Australia
Adobe Certified Advanced ColdFusion Developer
AFP Webworks
http://afpwebworks.com
ColdFusion, PHP, ASP, ASP.NET hosting from AUD$15/month
On Wed, Dec 24, 2008 at 11:21 AM, Jeff F <[email protected]> wrote:
> I need to build a shopping cart where a list if items is presented including
> a text field for quantity. But instead of each item having it's own "add to
> cart" form where a user adds each item to the cart one item at a time - the
> user needs to be able to go through the list, adding quantities to each item
> they want, then submit one button to add all items selected.
>
> I can't seem to get my head around setting this up, nor can I find an example
> of where it has been done.
>
> Something like:
> <FORM>
> <cfoutput>
> item 1 <input type="hidden" name="#id#" value="#id#"><input type="text"
> name="#id#addQty">
> item 2 <input type="hidden" name="#id#" value="#id#"><input type="text"
> name="#id#addQty">
> </cfoutput>
>
> <input type="submit" name="submit">
> </form>
>
> If I use the above, how best to deal with the submitted form data? The field
> names will vary, based on the list of items.
>
> Or, is there simply a better way to do this.
>
>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Adobe® ColdFusion® 8 software 8 is the most important and dramatic release to
date
Get the Free Trial
http://ad.doubleclick.net/clk;207172674;29440083;f
Archive:
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:317114
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4