Home-->
Articles -->
Site Tools
To do some calculations in one ASP page

Post By rodzok on 2008-9-26 15:52:21 [Reads:373]
That you wished to convert a price automatically in Euro without having to do trying seizures in your data base, or that you wished to display statistics, the calculations are possible in one ASP page. And of a simplicity... arithmetic.
Our basis of work is always structured like follows: Name of the field Parameters Id Type NuméroAuto Name Marks Text, Length 50 bus.
First name Marks Text, Length 30 bus.
Age Numeric Type (whole long)
Let's create a page virgin ASP, that we are going to name calcul.asp. We are going to display the set of the registrations of our table table1 there.
<% Const adOpenKeyset = 1
Set objConnection = Server.CreateObject ("ADODB.Connection") OBJConnection.Open Dim "mabase" strSQL strSQL = strSQL & "SELECT * FROM table1 order "Id by Set objRecordset = Server.CreateObject ("ADODB.Recordset") objRecordset.Open strSQL, objConnection, adOpenKeyset% >
We create here a game of registrations (recordset), that will be able to be thereafter very useful in the case of complex calculations. We will come back there during a future article.
Now, we are going "to sail" in the data base to collect the information there one by one.
<% Do While Not objRecordset.EOF% >
<% = objRecordset ("Name")% > & nbsp; <% = objRecordset ("First name")% >, & nbsp; <% = objRecordset ("Age")% > & nbsp;ans < br >
We create then, always inside the buckle, two variables. One named total serves to calculate the heap of ages. The second, titled nb, counts the number of registrations.
<% total = total + objRecordset2 ("age")% > <% nb = nb + 1% >
And one executes the buckle.
<% objRecordset.MoveNext Loop% >
With the progression of the execution of the buckle, the variables total and nb incremented themselves. The buckle being now finished, we can display our results:
< p>Nombre of people: <% = nb% > < / p > < accumulated p>Age: <% = total% > & nbsp;ans < / p >
(note that in itself this #118alue doesn't have any real significance, and serves to conduct the calculation of the average only)
And has the average:
< p>Moyenne of age: <% = total / nb% > & nbsp;ans < / p >
And to finish, as usual, one closes the connection AND the game of registrations.
<% objRecordset.Close objConnection.Close Set objRecordset = Nothing Set objConnection = Nothing% >