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

  1. #1

    Default Sum Array

    Hi,

    I have a series of text boxes that are generated in a form based on a query.
    They have the same name but have a digit attached to the end (text1, text2,
    text 3). I need to sum these in another textbox (sum2). I am doing this with
    Javascript on blur for each text box. I'm pretty new to javascript (VERY new).
    Can someone look at this code for me? dmworkload is the query which generates
    the number of textboxes.

    <script language="javascript">
    function AddEmUp(hrs)
    {
    <cfoutput>
    var protarray = new Array(#dmworkload.recordcount#);
    for (i = 0; i <= #dmworkload.recordcount#; i++)
    {
    protarray = hrs;
    }
    var sum;
    sum=0;
    for(i = 0; i <= #dmworkload.recordcount#; i++)
    {
    sum = sum + protarray;
    }
    document.workloader.total2.value = sum;
    </cfoutput>
    }

    </script>




    THanks,
    Frank

    fshin Guest

  2. Similar Questions and Discussions

    1. Converting an XML Array to a multi-level array
      I have an array assigned to a data grid such as: private var myIngredients:Array = new Array( <item ln1="Plain" sn="plain" ln2="(3 cups)...
    2. [newbie]saving and reading array of associative array
      i'm looking for examples of saving to file and reading back an array of associative array, in a ruby like way. saying i have something like : ...
    3. array data matches but array created in loop doesn't work
      I have the exact same data in two arrays, but only the array created like so will work: $spaw_imglibs = array( array( 'value' =>...
    4. #24897 [Com]: array_multisort() will reindex the array but not if array length is 1
      ID: 24897 Comment by: franklin_se at hotmail dot com Reported By: chro at sokrates dot uio dot no Status: ...
    5. #24897 [Opn->Asn]: array_multisort() will reindex the array but not if array length is 1
      ID: 24897 Updated by: sniper@php.net Reported By: chro at sokrates dot uio dot no -Status: Open +Status: ...
  3. #2

    Default Re: Sum Array

    OK, I've edited this a little bit.

    ---------

    <script language="javascript">
    function AddEmUp(hrs)
    {
    <cfoutput>
    var protarray = new Array(#dmworkload.recordcount#);
    for (var i = 1; i <= #dmworkload.recordcount#; i++)
    {
    protarray = hrs;
    }
    var sum;
    sum=0;
    for (var i = 1; i <= #dmworkload.recordcount#; i++)
    {
    sum = sum + parseInt(protarray);
    }
    document.workloader.total.value = sum;
    </cfoutput>
    }

    </script>


    -------------

    The thing that doesnt work is whatever textbox is changed, that value is
    assigned to each member of the array. Is there a way for the function to run
    through each textbox instead of just the one actually calling the function?

    Thanks,
    Frank

    fshin Guest

  4. #3

    Default Re: Sum Array

    No need for all the loops ect.
    In the form field, just pass the value, just ensure that the intial value of
    total2 or any other totals are zero.

    <input type="text" name="text1" value="1" onblur="AddEmUp(this.value);">

    <script language="javascript">
    function AddEmUp(hrs)
    {
    var sum = parseInt(document.workloader.total2.value);
    var addvalue = parseInt(hrs);
    document.workloader.total2.value = sum + addvalue;
    }
    </script>

    Ken


    The ScareCrow Guest

  5. #4

    Default Re: Sum Array

    Hi Ken,

    Thanks for your help! There is one remaining issue. If you change one of the
    values in the textboxes and then change it again, the value is simply added to
    the total rather than recalculating all of the boxes. For example, lets say
    there are 3 boxes: 4, 5, and 1. The total2 box reads 10. If you change the 1
    to 4, it reads 14 instead of 13. Any ideas?

    Thanks,
    Frank

    fshin Guest

  6. #5

    Default Re: Sum Array

    Okay, this is a more involved senario. This will require a javascript function.

    Basically when the onblur event occurs the js function is called. The js
    function then loops through all the form fields. If the form feld name
    contains the text "text" then add the value of this field to a varaible. After
    the function has looped through all the form fields, then assign the variable
    to the "total" form field.

    The attached code is a complete working example of this.

    Ken

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html>
    <head>
    <title>Untitled</title>
    <script language="JavaScript">
    <!--
    function calculate(form){
    var totalAmount = 0;
    for(i=0; i < form.elements.length; i++){
    var formField = form.elements[i].name;
    if(formField.indexOf('text') != -1){
    totalAmount = totalAmount + parseInt(form.elements[i].value);
    }
    }
    form.total2.value = totalAmount;
    }
    //-->
    </script>
    </head>

    <body>
    <cfoutput>
    <form>
    <cfloop from="1" to="5" index="idx">
    <input name="text#idx#" value="#idx#" onblur="calculate(this.form);">
    </cfloop>
    <input name="total2" value="0">
    </form>
    </cfoutput>

    </body>
    </html>

    The ScareCrow Guest

  7. #6

    Default Re: Sum Array

    Scare Crow, Thanks so much! It is working except for one thing. For each
    textbox created by the query there are that many textboxes made. That is, lets
    say there are 4 textboxes made by the query results. Instead of only 4 boxes,
    there are 16. Of if there should be 3, there are 9. Any thoughts?

    fshin Guest

  8. #7

    Default Re: Sum Array

    I'm going to need to see some code

    But if you are doing
    <cfoutput query="queryname">
    <input>
    </cfoutput>

    Then it should only create an input for each record of the query result set.

    Ken
    The ScareCrow 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