We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,590 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Sum all the Array textboxes

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.

3
Contributors
7
Replies
9 Hours
Discussion Span
1 Year Ago
Last Updated
8
Views
borgyborg
Newbie Poster
12 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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..

veedeoo
Master Poster
735 posts since Oct 2011
Reputation Points: 298
Solved Threads: 129
Skill Endorsements: 13

I almost forgot, add before the while{

$sum_ofsalary = 0;
veedeoo
Master Poster
735 posts since Oct 2011
Reputation Points: 298
Solved Threads: 129
Skill Endorsements: 13

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>";
borgyborg
Newbie Poster
12 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

edit reason: Wrong codes..

You can try

$r = mysql_fetch_row($result);

$textboxes_sum = $r[0];

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

veedeoo
Master Poster
735 posts since Oct 2011
Reputation Points: 298
Solved Threads: 129
Skill Endorsements: 13

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

diafol
Keep Smiling
Moderator
10,613 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57

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.

borgyborg
Newbie Poster
12 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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).

diafol
Keep Smiling
Moderator
10,613 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,506
Skill Endorsements: 57

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.3465 seconds using 2.67MB