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.

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..

I almost forgot, add before the while{

$sum_ofsalary = 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>";

edit reason: Wrong codes..

You can try

$r = mysql_fetch_row($result);

$textboxes_sum = $r[0];

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

Member Avatar for diafol

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

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.

Member Avatar for diafol

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).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.