1,105,332 Community Members

Sum all the Array textboxes

Member Avatar
borgyborg
Newbie Poster
10 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
veedeoo
Veteran Poster
1,008 posts since Oct 2011
Reputation Points: 314 [?]
Q&As Helped to Solve: 164 [?]
Skill Endorsements: 17 [?]
 
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..

Member Avatar
veedeoo
Veteran Poster
1,008 posts since Oct 2011
Reputation Points: 314 [?]
Q&As Helped to Solve: 164 [?]
Skill Endorsements: 17 [?]
 
0
 

I almost forgot, add before the while{

$sum_ofsalary = 0;
Member Avatar
borgyborg
Newbie Poster
10 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 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>";
Member Avatar
veedeoo
Veteran Poster
1,008 posts since Oct 2011
Reputation Points: 314 [?]
Q&As Helped to Solve: 164 [?]
Skill Endorsements: 17 [?]
 
0
 

edit reason: Wrong codes..

You can try

$r = mysql_fetch_row($result);

$textboxes_sum = $r[0];

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

Member Avatar
diafol
Where are my eyes?
12,943 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
0
 

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

Member Avatar
borgyborg
Newbie Poster
10 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
diafol
Where are my eyes?
12,943 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
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 three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: