Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asp_databases thread: SV: RE: Vote System


Message #1 by "SD-Studios" <info@s...> on Mon, 24 Sep 2001 17:15:35 +0200
Thanks a lot!!! =) Im not so very good at ASP, so i'd like to know what "+1"

in this string is for: averagevote = int(totalvote/votecount)+1

--

Martin Johansson





-----Ursprungligt meddelande-----

Från: Sam Clohesy [mailto:sam@e...]

Skickat: den 24 september 2001 17:06

Till: ASP Databases

Ämne: [asp_databases] RE: Vote System





Something like this:







<%

	dim totalvote

	dim votecount

	dim averagevote





	Set rsd2 = Server.CreateObject("ADODB.Recordset")

	strSQL= "SELECT * FROM tblRating WHERE CodeID = " & choice

	rsd2.Open strSQL, conn, 1'adOpenForwardOnly

	votecount = rsd2.recordcount



	do while NOT rsd2.EOF

		totalvote = totalvote + rsd2("rating")

		rsd2.movenext

	loop

	if votecount > 0 then

	averagevote = int(totalvote/votecount)+1

	end if



	%>

                    <img src="images/<%=averagevote%>stars.gif" width="62"

height="12">



No doubt better way...



Thanks



Sam







-----Original Message-----

From: SD-Studios [mailto:info@s...]

Sent: 24 September 2001 15:51

To: ASP Databases

Subject: [asp_databases] Vote System





Hi!

I've once again got a problem. Im building a vote-system. You are suppose to

vote from 1-10 and the result will be saved in a table in the database. I'm

using cookies so the user can only vote one time. It's no problem adding new

values, but I don't know how to get the average value of these values. I

must add all values and then divide them by the number of votes, right? But

how do I write this?

--

Martin Johansson
Message #2 by Sam Clohesy <sam@e...> on Mon, 24 Sep 2001 16:26:08 +0100
Hi Martin, it is to ensure whole numbers (again this voting system is not

very accurate..)



Thanks



Sam



-----Original Message-----

From: SD-Studios [mailto:info@s...]

Sent: 24 September 2001 16:16

To: ASP Databases

Subject: [asp_databases] SV: RE: Vote System





Thanks a lot!!! =) Im not so very good at ASP, so i'd like to know what "+1"

in this string is for: averagevote = int(totalvote/votecount)+1

--

Martin Johansson

Message #3 by "SD-Studios" <info@s...> on Tue, 25 Sep 2001 18:20:53 +0200
Okay! Thanks! =)

--

Martin Johansson





-----Ursprungligt meddelande-----

Från: David Cameron [mailto:dcameron@i...]

Skickat: den 25 september 2001 01:30

Till: ASP Databases

Ämne: [asp_databases] RE: Vote System







Whoa there. There are faster ways of doing that.



First comment, never use recordcount. There is a page about it on Ken's site

(www.adopenstatic.com), but basically to get a true result you need to move

last and move first, which is a performance loss. COUNT(*) in the SQL

statement is always a better option.



<%

	dim averagevote



	Set rsd2 = Server.CreateObject("ADODB.Recordset")

	strSQL= "SELECT * FROM tblRating WHERE CodeID = " & choice

	strSQL = "SELECT SUM(rating)/COUNT(*) as Average " & _

	  "FROM tblRating WHERE CodeID = " & choice

	rsd2.Open strSQL, conn, adOpenForwardOnly, adLockReadOnly

	rsd2.close

	Set rsd2 = Nothing

	averagevote = int(rsd2("Average"))

	If averagevote < 1 Then averagevote = 1



%>

                    <img src="images/<%=averagevote%>stars.gif" width="62"

height="12">



I checked the SQL string for SQL Server, it should work in Access. If not

you can return the sum and 2 different fields for Count and Sum and do the

calculation in the page.



regards

David Cameron

nOw.b2b

dcameron@i...



-----Original Message-----

From: Sam Clohesy [mailto:sam@e...]

Sent: Tuesday, 25 September 2001 1:06 AM

To: ASP Databases

Subject: [asp_databases] RE: Vote System





Something like this:



<%

	dim totalvote

	dim votecount

	dim averagevote





	Set rsd2 = Server.CreateObject("ADODB.Recordset")

	strSQL= "SELECT * FROM tblRating WHERE CodeID = " & choice

	rsd2.Open strSQL, conn, 1'adOpenForwardOnly

	votecount = rsd2.recordcount



	do while NOT rsd2.EOF

		totalvote = totalvote + rsd2("rating")

		rsd2.movenext

	loop

	if votecount > 0 then

	averagevote = int(totalvote/votecount)+1

	end if



	%>

                    <img src="images/<%=averagevote%>stars.gif" width="62"

height="12">



No doubt better way...



Thanks



Sam


















Message #4 by Kyle Burns <kburns@c...> on Tue, 25 Sep 2001 12:37:29 -0500
Even better than Count(*), which will also give a performance hit, is 

to use

the Count function on a field that you know will contain a value (nulls 

are

skipped in Count) such as CodeID.  I also noticed that you are 

referencing

the value of the field "Average" after the recordset object is closed 

and

set to Nothing.  You'll need to modify the code to save this value to a

variable before closing the recordset.  I kind of shy away from using 

words

that could ever conceivably be a keyword when I write my SQL 

statements.  My

suggestion would be to have your SQL statement end up something like 

this:



"SELECT SUM(rating) / COUNT(codeid) AS avgRating FROM tblRating WHERE 

codeid

=3D " & choice





=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

=3D=3D=3D=3D=3D=3D=3D=3D=3D

Kyle M. Burns, MCSD

ECommerce Technology Manager

Centra Credit Union

kburns@c...









-----Original Message-----

From: SD-Studios [mailto:info@s...]

Sent: Tuesday, September 25, 2001 11:21 AM

To: ASP Databases

Subject: [asp_databases] SV: RE: Vote System





Okay! Thanks! =3D)

--

Martin Johansson





-----Ursprungligt meddelande-----

Fr=E5n: David Cameron [mailto:dcameron@i...]

Skickat: den 25 september 2001 01:30

Till: ASP Databases

=C4mne: [asp_databases] RE: Vote System







Whoa there. There are faster ways of doing that.



First comment, never use recordcount. There is a page about it on Ken's 

site

(www.adopenstatic.com), but basically to get a true result you need to 

move

last and move first, which is a performance loss. COUNT(*) in the SQL

statement is always a better option.



<%

 dim averagevote



 Set rsd2 =3D Server.CreateObject("ADODB.Recordset")

 strSQL=3D "SELECT * FROM tblRating WHERE CodeID =3D " & choice

 strSQL =3D "SELECT SUM(rating)/COUNT(*) as Average " & _

   "FROM tblRating WHERE CodeID =3D " & choice

 rsd2.Open strSQL, conn, adOpenForwardOnly, adLockReadOnly

 rsd2.close

 Set rsd2 =3D Nothing

 averagevote =3D int(rsd2("Average"))

 If averagevote < 1 Then averagevote =3D 1



%>

                    <img src=3D"images/<%=3Daveragevote%>stars.gif" 

width=3D"62"

height=3D"12">



I checked the SQL string for SQL Server, it should work in Access. If 

not

you can return the sum and 2 different fields for Count and Sum and do 

the

calculation in the page.



regards

David Cameron

nOw.b2b

dcameron@i...



-----Original Message-----

From: Sam Clohesy [mailto:sam@e...]

Sent: Tuesday, 25 September 2001 1:06 AM

To: ASP Databases

Subject: [asp_databases] RE: Vote System





Something like this:



<%

 dim totalvote

 dim votecount

 dim averagevote





 Set rsd2 =3D Server.CreateObject("ADODB.Recordset")

 strSQL=3D "SELECT * FROM tblRating WHERE CodeID =3D " & choice

 rsd2.Open strSQL, conn, 1'adOpenForwardOnly

 votecount =3D rsd2.recordcount



 do while NOT rsd2.EOF

  totalvote =3D totalvote + rsd2("rating")

  rsd2.movenext

 loop

 if votecount > 0 then

 averagevote =3D int(totalvote/votecount)+1

 end if



 %>

                    <img src=3D"images/<%=3Daveragevote%>stars.gif" 

width=3D"62"

height=3D"12">



No doubt better way...



Thanks



Sam





Message #5 by David Cameron <dcameron@i...> on Wed, 26 Sep 2001 09:24:53 +1000
Sorry, the code was written in about 20 seconds and was written to

demonstrate a better method, not to be used as is. When it comes to 

column

names I would normally prefix them by the type of data they hold. So in 

this

case I would rename Average as decAvgRating. Thanks for the correction, 

I

will try to make my code suggestions more plug and play. Thanks also 

for the

correction on COUNT()



regards

David Cameron

nOw.b2b

dcameron@i...



-----Original Message-----

From: Kyle Burns [mailto:kburns@c...]

Sent: Wednesday, 26 September 2001 3:37 AM

To: ASP Databases

Subject: [asp_databases] RE: SV: RE: Vote System





Even better than Count(*), which will also give a performance hit, is 

to use

the Count function on a field that you know will contain a value (nulls 

are

skipped in Count) such as CodeID.  I also noticed that you are 

referencing

the value of the field "Average" after the recordset object is closed 

and

set to Nothing.  You'll need to modify the code to save this value to a

variable before closing the recordset.  I kind of shy away from using 

words

that could ever conceivably be a keyword when I write my SQL 

statements.  My

suggestion would be to have your SQL statement end up something like 

this:



"SELECT SUM(rating) / COUNT(codeid) AS avgRating FROM tblRating WHERE 

codeid

=3D " & choice




  Return to Index