Jump to content

Getting sum of a column from a table into a textbox


lindisfarne

Recommended Posts

In the code below I can get the total of the column into the field in a table,but how would I go about this if I wanted to get the total into a text box(html).
I tried placing the $total variable into the value attribute of the text box but this didn't work.Any suggestions much appreciated:)

 

$total = 0; // initialise total

while($record = mysql_fetch_array($myData)){
echo "<tr>";
echo "<td>" . $record['id'] . "</td>";
echo "<td>" . $record['author'] . "</td>";
echo "<td>" . $record['title'] . "</td>";
echo "<td>" . $record['publisher'] . "</td>";
echo "<td>" . $record['year'] . "</td>";
echo "<td>" . $record['genre'] . "</td>";
echo "<td>" . $record['sold'] . "</td>";
echo "<tr />";

$total += $record['sold']; // accumulate total
}

echo "<tr><td colspan='6'>Total:</td><td>$total</td></tr>"; // output total

echo "</table>";

 

Kind regards

Lind

Link to comment
Share on other sites

Are you referring to an input box of type "text" or a textarea element?

 

If the former then using the value attribute should work.

echo "<input type='text' name='total' value='$total' />";

If you mean the latter then there is no value attribute. The text content goes between the open and close tags

echo "<textarea rows='2' cols='20' name='total'>$total</textarea>";

Basic HTML.

 

And please use code tags, or the "<>" button in the toolbar.

Edited by Barand
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.