SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    still more dhtml form calcs

    hi guys...

    This could go into client-side or server-side because it's both. So I'm posting it here...

    I have a quotesheet that is supposed to loop a series of items and prices from a db. You enter a number in the quantity field and javascript determines the total for that item. This works and echos back item#total in it's own cell.

    Now. How do I add all this up for a subtotal? I could easily write a function that adds up a static number of rows, but how do I do it so it adds up all the rows that are echoed back from the db? There may be items added or deleted later...

    Sketch
    PHP Code:
    <?php
    while($item=mysql_fetch_array($results)) {
        
    $pid=$item['productID'];
        
    $product=$item['product'];
        
    $price=$item['price'];
        
    ?>
    <tr>
        <td id="prod<?php echo $pid?>"><?php echo $product?></td>
        <td align="center"><?php echo $price?>
        <input type="hidden" name="Item_<?php echo $pid?>_price" value="<?php echo $price?>"></td>
        <td align="center"><input type="text" name="qty<?php echo $pid?>" size="4" maxlength="4"value="0" 
        onBlur="javascript: calculate_total(document.frmMain.Item_<?php echo $pid?>_price.value,document.frmMain.qty<?php echo $pid?>.value,Item<?php echo $pid?>total);">
    </td>
        <td align="center"><b> $ <span id="Item<?php echo $pid?>total" style="position:relative;"></span></b></td>
    </tr>
    <?php
    }
    ?>
    <tr>
        <td colspan="2" align="right">Subtotal</td>
        <td>&nbsp;</td>
        <td><!---Input javascript to calculate cell total--></td>
    </tr>
    Aaron Brazell
    Technosailor



  2. #2
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would do something like this:


    function calculateTotal() {
    var i;
    var iRows;
    var fltTotal;

    iRows = tblTableName.rows.length();
    fltTotal = 0;

    for(i=0; i<iRows; i++) {
    fltTotal += parseFloat(tblTableName.rows[i].cells[3].children[1]);
    }

    return fltTotal
    }

    The logic for this should work. The only thing you might need to play with is the DOM. I always have to play with the 'cells[]' and 'children[]' collections to find the value i'm trying to access.
    Last edited by makeda; Feb 13, 2002 at 12:50.


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
  •