Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default CFLOOP Problem

    Hi! I'd be grateful if someone could please help me solve this problem I'm
    having. I have a query (e.g. qOne) that generates a quantity (e.g. 29) and I
    have another query (e.g. qTwo) that generates a list of quantities (e.g. 1, 10,
    16, 17, 19, 20) <cfquery name='qTwo' datasource='myDSN'> select
    id, quantity from tableName where conditions </cfquery>
    qTwo Output id quantity 1 1 2 10 3
    16 4 17 5 19 6 20
    How can I loop through qTwo so that I can obtain #qOne.quantity# i.e. 29, from
    a combination of the quantities generated by qTwo, and output the all
    corresponding id's from qTwo? E.g. 29 can be obtained by adding 10 and 19.
    10 corresponds to id 2 and 19 correspnds to id 5. In other words how can I
    obtain id 2 and 5 programmatically? Thanks and regards Yogesh Mahadnac

    YogeshM Guest

  2. Similar Questions and Discussions

    1. cfloop
      The goal is to insert a record into table 'atd', which contains a document id and an associate id, for each document (38) and associate (150). So...
    2. Cfloop..
      I am trying to loop over a list: <cfquery name="updsyllabus" datasource="#arguments.syl.dsn#"> UPDATE #arguments.syl.tname# SET <cfloop...
    3. to cfloop or not to cfloop?
      :confused; I have a list of checkboxes from a form, and a submit button for "Batch Print" The var is "SelectList" and comes out as comma dilimited....
    4. CFLOOP Error?
      I keep getting an error that says that I need to have a CFLOOP end tag? The bolded line is where Coldfusion says the error is occurring: <cfquery...
    5. CFloop question
      Hello: I need to display some course information online, we have multiple sections for some courses, there might be three instrutors teach 3...
  3. #2

    Default Re: CFLOOP Problem

    Can more than two qTwo quantities be summed to produce qOne? In other words, if qTwo was 1, 2, 10, 16, 17, 19, 20, 26 then 10+19=29 and 1+2+26=29.
    jdeline Guest

  4. #3

    Default Re: CFLOOP Problem

    Hi!

    Yes of course! There could have been more than 2 additions, as long as the
    program does the right combinations to match #qOne.quantity#

    Any idea how to do this please?

    Regards,

    Yogesh Mahadnac

    YogeshM Guest

  5. #4

    Default Re: CFLOOP Problem

    I'm not sure how to do this in CF, but here's a quick-and-dirty java example of
    the algorithm. The easiest way to accomplish this, at least the way I think, is
    through recursion. There's definitely a way to do this without recursion, but
    it'll be nasty (there are too many possibilities).



    public class Recurse {
    public void doSum(int desiredTotal, int[] ids, int[] values, int level,
    String idList) {
    if( desiredTotal > 0 ) {
    for(int i = level; i < ids.length; i++) {
    // recurse
    doSum(desiredTotal - values[i], ids, values, i + 1, idList + ids[i] + ",")
    ;
    }
    } else if (desiredTotal == 0) {
    System.out.println("Success! "+idList);
    }
    }

    public static void main(String args[]) {
    int desiredTotal = 29;
    int[] ids = {1,2,3,4,5,6,7,8};
    int[] values = {1,3,5,10,16,17,19,20};

    Recurse recurse = new Recurse();
    recurse.doSum(desiredTotal,ids,values, 0, "");
    }
    }

    The output when this is run is as follows:
    Success! 1,2,3,8,
    Success! 2,4,5,
    Success! 4,7,

    Kronin555 Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139