SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Help with logic

  1. #1
    Take The Blue Pill neilmerton's Avatar
    Join Date
    Jul 2004
    Location
    Leicester, UK
    Posts
    871
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Help with logic

    Hi all,

    I'm currently drawing a blank with the following (probably been thinking about it too much) and would appreciate some outside comments

    Example:

    A customer orders the following -
    7 containers
    3 stars
    2 squares

    I would like to output this order in the following -
    2 containers contains 1 star and 1 square
    1 container contains 1 star
    4 containers are empty


    The data is stored in an 'Orders' table and includes the following fields 'ItemID', 'ItemQty'

    I just can't figure out the coding logic

    Thanks in advance

  2. #2
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can't help you with your problem until you explain how you're sectioning out the stars and squares into containers.

    Why does one container get one star AND one square, while another container only gets one star?
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  3. #3
    Take The Blue Pill neilmerton's Avatar
    Join Date
    Jul 2004
    Location
    Leicester, UK
    Posts
    871
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The thought behind this is to fill the containers with 1 item of all available items (excluding the actual containers) and then when one item is finished have a new line continuing the logic.

    Managed to get it sussed though, and here's a summary of the code if you're interested.

    Code CFM:
    <cfquery dbtype="query" name="optsQtyList">
          SELECT  OptIntCode
           ,  ItemQty
      FROM    orderData
      WHERE   OptIntCode IS NOT NULL
    ORDER BY  ItemQty
    </cfquery>
    <cfset offsetQty = 0 />
    <cfloop from="1" to="#optsQtyList.recordCount#" index="i">
        <cfif (optsQtyList.ItemQty[i] gt offsetQty)>
            <cfset thisQty = optsQtyList.ItemQty[i] - offsetQty />
            <cfset thisCode = "" />
            <order_line>
                <quantity>#thisQty#</quantity>
                <package_id>
                    <cfscript>
                        for (j=i; j LTE optsQtyList.recordCount; j=j+1)
                        {
                            thisCode = thisCode & ":" & optsQtyList.OptIntCode[j];
                        }
                        if (left(thisCode,1) eq ":")
                        {
                            thisCode = right(thisCode,len(thisCode)-1) & ":" & request.packageMaster;
                        }
                    </cfscript>
                    #thisCode#
                </package_id>
            </order_line>
            <cfset offsetQty = optsQtyList.ItemQty[i] />
        </cfif>
    </cfloop>
    <cfset request.countMaster = request.countMaster - offSetQty />
    <order_line>
        <quantity>#request.countMaster#</quantity>
        <package_id>#request.packageMaster#</package_id>
    </order_line>

    that then outputs

    Code XML:
    <order_line>
     <quantity>2</quantity>
     <package_id>HEA840:HEB240:HEX584</package_id>
    </order_line>
    <order_line>
     <quantity>1</quantity>
     <package_id>HEB240:HEX584</package_id>
    </order_line>
    <order_line>
     <quantity>4</quantity>
     <package_id>HEX584</package_id>
    </order_line>

  4. #4
    SitePoint Zealot YAHA's Avatar
    Join Date
    Jan 2008
    Location
    MD, US
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Given your explanation, I just wanted to clarify a couple things. When we assign elements to containers, we first have to assign a maximum possible number of paired elements (stars and squares), one pair per container. We then use whatever single elements are left off, one per container. Is my reasoning correct? Because it seemes to me that you were not very explicit about this and I could only guess based on your example.

  5. #5
    Take The Blue Pill neilmerton's Avatar
    Join Date
    Jul 2004
    Location
    Leicester, UK
    Posts
    871
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess the best way to explain it is you've got multiple containers and a variety of items to put into these containers. The principle is to start off with one of each item in as many containers are there are the least amount of a particular item.

    Eg: 2 containers contains 1 star and 1 square

    You then continue the process until you have no more items left.

    There is no particular order that the items need to appear in the container.

    The code I posted does what I need it to do, so the issue is solved.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •