cannot sum specific variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • silversubey
    New Member
    • May 2007
    • 22

    cannot sum specific variables

    Hello all.
    I have a form that users are able to enter dollar amounts for each month and add them to the table. i am having difficulty adding cells that display the totals. For example, rows have 12 columns(one for each month) I would like to have a 13th that totals the entire row. Also at the bottom of the table, i need to have a total for only that column. I hope this makes sense. Below is the code for one of the tables.

    [code=php]
    // income
    $_SESSION["a$CDID"]['pl']['income']['0'] = '';
    if (isset($_POST['iicomp']))
    {
    $mkey = $_POST['iicomp'];
    $istate = $_POST['completed'];

    }
    if (isset($_GET['idel']))
    {
    unset($_SESSION["a$CDID"]['pl']['income'][$_GET['idel']]);
    foreach ($_SESSION["a$CDID"]['pl']['income'] as $line)
    $tmp[] = $line;
    $_SESSION["a$CDID"]['pl']['income'] = $tmp;
    }
    if (isset($_POST['income']))
    {
    if (!empty($_POST['key']))
    $_SESSION["a$CDID"]['pl']['income'][$_POST['key']] = $_POST['income'];
    else
    $_SESSION["a$CDID"]['pl']['income'][] = $_POST['income'];
    }
    if (count($_SESSIO N["a$CDID"]['pl']['income']) > '1')
    {
    foreach ($_SESSION["a$CDID"]['pl']['income'] as $key => $val)
    {
    if ($key != '0')
    $PRINT_INCOME .= '
    <tr class="input" '.$istyle.'><td >'.$key.'. '.$val['descript'].'</td><td>'.$val['jan'].'</td><td>'.$val['feb'].'</td>
    <td>'.$val['mar'].'</td><td>'.$val['apr'].'</td><td>'.$val['may'].'</td><td>'.$val['jun'].'</td><td>'.$val['jul'].'</td><td>'.$val['aug'].'</td><td>'.$val['sep'].'</td><td>'.$val['oct'].'</td><td>'.$val['nov'].'</td><td>'.$val['dec'].'</td>
    <td>"this is where I have been trying to insert a Sum for the row"</td>
    <td align="center"> <a href="pl.php?cd id='.$CDID.'&ie dit='.$key.'">< input type="image" src="icon-blue-check.gif"</a></td><td><a href="pl.php?cd id='.$CDID.'&id el='.$key.'"><i nput type="image" src="icon-red-minus.gif"</a></td></tr>';
    }
    }
    else
    $PRINT_INCOME = '<tr class="row"><td ></td></tr>';
    if (isset($_GET['iedit']))
    [/code]

    The code for displaying the row is
    [code=php]
    <?PHP if (isset($PRINT_I NCOME)) echo $PRINT_INCOME; ?>
    [/code]


    Thankyou
  • aktar
    New Member
    • Jul 2006
    • 105

    #2
    Hi Subey,

    I didn't quite understand the need for certain variables, but I've simplified your script:


    [PHP]
    <form id="form1" name="form1" method="post" action="">
    <table width="100%" border="1" cellspacing="0" cellpadding="0" >
    <tr>
    <?php

    $CDID = 1;
    $account = "a{$CDID}";


    $_SESSION[$account]['pl'] = @$_POST['income'];

    $total = 0;


    for($i=0; $i<12; $i++){

    $value = NULL;

    if (isset($_POST['income'][$i])) $value = $_POST['income'][$i];

    print "<td><input type='text' name='income[]' value='$value' style='width:50 px; border:none; background-color:yellow;' /></td>";

    $total = $total + $value;
    }

    print "<td>$total </td>";

    ?>
    </tr>
    </table>

    <input type="submit" name="button" id="button" value="Total up" />
    [/PHP]

    Ps the yellow boxes are form fields

    Regards

    Comment

    Working...