1.11M Members

Sum all the Array textboxes

 
0
 

Hi All,

I want to auto-calculate all the array textboxes I created.
My code is:

<?php
        echo "<table>";

        $sql="SELECT default_salary FROM records WHERE is_active = 'Yes'";  
        while ($row=mysql_fetch_array($result)) {
                $default_salary = $row["default_salary"];
                $num_rows = mysql_num_rows($result);
             

        echo "<tr>";
        echo "<td>";
        echo $count."</td>";
        echo "<td width=\"\" align=\"left\" border=\"0\">";
        echo "<input type=\"text\" size=\"4\" name=\"default_salary[]\" value=\"$default_salary\">";
        echo "</td>";
        echo "</tr>";

        $count++ ;
        } 
        echo "</table>";
?>

<input type="text" name="sum" value="????????">

Now i want to put the calculated value of the array "default_salary[]" to the textbox above.

Please help. Thanks in advanced.

 
0
 

try replacing these lines

echo "<tr>";
        echo "<td>";
        echo $count."</td>";

With these

$sum_ofsalary += int_val($row['default_salary']);
        echo "<tr>";
        echo "<td>";
        echo $sum_ofsalary."</td>";

You can then use the $sum_ofsalary as the total of all salaries "default_salary" within the while loop..

 
0
 

I almost forgot, add before the while{

$sum_ofsalary = 0;
 
0
 

try replacing these lines

echo "<tr>";
        echo "<td>";
        echo $count."</td>";

With these

$sum_ofsalary += int_val($row['default_salary']);
        echo "<tr>";
        echo "<td>";
        echo $sum_ofsalary."</td>";

You can then use the $sum_ofsalary as the total of all salaries "default_salary" within the while loop..

Thanks for the Reply veedeoo, I'm sorry if I misunderstood you. the loop was ok. it's giving me all the salaries in the textboxes. What I want is, say for example i got 10 textboxes in a loop. In textboxes have values from the DB, and outside the loop there is one textbox which is the

<input type="text" name="sum" value="$????????">

. as you can see I'd like to put the sum of all textboxes in this value="$????????".

Hope I explained it well.

Thanks again guys.

anyway, just forget these lines. This is only for serial Number. :D

echo "<tr>";
        echo "<td>";
        echo $count."</td>";
 
0
 

edit reason: Wrong codes..

You can try

$r = mysql_fetch_row($result);

$textboxes_sum = $r[0];

Use $textboxes_sum in the value="$..."

 
0
 

Why are you using textboxes? Are the values going to be sent in a form?

 
0
 

Hi Veedeoo,

I'm submitting this to a form as Ardav asking me.
This should be in the text boxes.

User can edit also those text boxes and it should also calculate automatically without pressing any submit button.

 
0
 

User can edit also those text boxes and it should also calculate automatically without pressing any submit button.

Without submit will be ajax (js) - no need to involve server - use the onchange attribute (or the 'change' event of a jQuery object) to run a js function.

The submit button should then save the new values (but not the total) to the DB.

I wouldn't use a php or mysql function to total up all salaries - just use the same js totalling function on page load (e.g. document ready for jQuery).

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: