Skip to main content
New Participant
September 28, 2010
Question

Sort values of structure with arrays

  • September 28, 2010
  • 1 reply
  • 1109 views

Hi

For a shopping cart I'm using a structure with arrays to store the items in the cart. I tried to use StructSort but did not work...

This is how I store the items in the cart:

<cfscript>
            if (not(isdefined("session.cart"))) {                // 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.id#,#attributes.name#,#attributes.price#,#attributes.quantity#');

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

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

Any help will be appreciated.

Thank you.

TJ

    This topic has been closed for replies.

    1 reply

    Inspiring
    September 28, 2010

    Not that this answers your question, but don't you want an array of structs, not a struct of arrays?

    Each element of the array would be an item in the cart, which itself would be a struct keyed on those parts you list:

    Item 1:

    ID: OCE1

    Name: ocelot

    Price: £5

    Quantity: 3

    Item 2:

    ID: PAN6

    Name: pangolin

    Price: £10

    Quantity: 6

    etc

    What is it you're trying to achieve by sorting this?  What's the ordering you are after?

    --

    Adam

    tjoad010Author
    New Participant
    September 28, 2010

    I don't mind to use an array of structs...is there many changes involved?

    To display the cart I simply use a loop:

    <cfloop collection="#session.cart#" item="i">

    #session.cart[2]# #session.cart[3]# #session.cart[4]#<br>

    </cfloop>

    To answer your questions:

    When I display the shopping cart I'd like to show the items ordered by name or by price as at the moment they don't display in any order. It displays the items in a random order. At first I thought it was in the order that you add them in the cart but that's not the case.

    tjoad010Author
    New Participant
    September 28, 2010

    What is the key your using for the outer struct?  I am finding it hard to get my brain around how you are implementing this cart as a struct of arrays.

    What's some sample data?

    --

    Adam


    Hi Adam

    This is inherited old code that I'm trying to update...

    Anyway to answer your question the key for the outer struct is the item's name. Here is a screen-shot of some sample data using cfdump. It's ironic that cfdump successfully sorts the item's name alphabetically...

    I hope this extra information helps.

    TJ