This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Calculating Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Calculating" Watch "Calculating" New topic
Author

Calculating

Ikasari Widiyanto
Greenhorn

Joined: May 31, 2004
Posts: 29
Hi,

For example, I have 3 columns, description of items, quantity and unit cost, in the database, For example:
Description Quantity unitcost
item1 10 2
item2 10 3
these are retrieved from the database. How can I automatically calculate the total cost of these 2 things? Anybody can help me?
Balan Raj
Ranch Hand

Joined: Aug 26, 2003
Posts: 74
If u have HTML + JavaScript , make javascript to do it

better use something like JSP to do it
Ikasari Widiyanto
Greenhorn

Joined: May 31, 2004
Posts: 29
Yah I am using javascript to count it since it is faster but how? any sample code?

this is the javascript for calculate:
function calculate(){
var unitPrice = document.test.unitcost.value
var quantity = document.test.quantity.value

if (isNaN(unitPrice)||unitPrice.length==0){
alert ("Enter the unit Price in numeric")
}else if (isNaN(quantity)||quantity.length==0){
alert ("Enter the quantity in numeric")
}else{
var amount = unitPrice * quantity
amount = round(amount,3)
document.test.totalcost.value=amount
}

}

this is the part where i retrieve from database
<table width="878" border="1">
<tr>
<td width="355">Description:</td>
<td width="58">Quantity:</td>
<td width="70">Unit Cost: </td>
<td width="367">Recommended Supplier:</td>
</tr>
<%
System.out.println("requestview1.jsp:"+requestNo);
conn = db.getConnection();
stmt = conn.createStatement();
sql = "select * from requestdetail where requestNo="+requestNo+" order by id asc";
rs = stmt.executeQuery(sql);
while (rs.next())
{
description = rs.getString("description");
unitcost = rs.getString("unitcost");
quantity = rs.getString("quantity");
supplier = rs.getString("supplier");
%>
<tr>
<td><%= description %></td>
<td><%= unitcost %> </td>
<td><%= quantity %></td>
<td><%= supplier %></td>
</tr>
<%
}
rs.close();
stmt.close();
conn.close();
%>
</table>
Yuriy Fuksenko
Ranch Hand

Joined: Feb 02, 2001
Posts: 413
I am using javascript to count it since it is faster

I don't think it is faster Javascript is generally much slowlier than anything else you use (Unless you handling user input - than it wins if you don't have any fancy i18n validation)

By the way, did you think about calculating it right in the SQL?
Something like


I am not applying any functions to convert Strings to numbers, but I don't think it is hard to do.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calculating
 
Similar Threads
Limit Table data within the width assigned
xml parsing
Group by in mysql
onchange for dynamically added rows
xml parsing with dom