...

View Full Version : Highlight you submitted score



ellisd5
12-18-2003, 09:58 AM
I have made a game and once you finish play the game you submit your score to the database.

Then after that I display the top 20 table, If you have made the top 20 then I am trying to highlight that score you have just submited, its not workin at the minute and I dont know why?

This is my code


<HTML>
<HEAD>

<TITLE>Submit Score</TITLE>
<LINK REL="stylesheet" TYPE="text/css" HREF="main.css">

</HEAD>
<BODY>
<%
Dim conn

Set conn = Server.CreateObject("ADODB.Connection")

DSNName = "DRIVER=Microsoft Access Driver (*.mdb);DBQ="
DSNName = DSNName & Server.MapPath("/svggames/db/games.mdb")
conn.Open DSNName

sql = "INSERT INTO "& request("whichGame") &" VALUES('"& UCase(request("name")) &"', "& request("points") &")"
conn.Execute(sql)

sql = "SELECT TOP 20 person, score FROM "& request("whichGame") &" ORDER BY score desc"
set rs = conn.Execute(sql)

Response.Write("<TABLE WIDTH='100%'><TR CLASS='myGreen'><TD COLSPAN='3' CLASS='headblack'>"& request("whichGame") &" TOP SCORE LIST</TD></TR>")
postion = 1
While NOT rs.EOF
If rs("person") = UCase(request("name")) and rs("score") = request("points") Then
Response.Write("<TR class='myLightGreen'>")
else
Response.Write("<TR>")
End If
Response.Write("<TD WIDTH='25'>"& postion &"</TD><TD>"& rs("person") &"</TD><TD WIDTH='60' ALIGN='right'>"& rs("score") &"</TD></TR>")
postion = postion+1
rs.MoveNext
WEnd
Response.Write("</TABLE>")

conn.close
set conn = nothing
%>
</BODY>
</HTML>


The problem it seems is that this condition is never coming true
If rs("person") = UCase(request("name")) and rs("score") = request("points") Then
What is wrong is it a datatype thing as score is a number in the database???

Dale

raf
12-18-2003, 10:53 AM
Could very well be.
Try it like this

If rs("person") = UCase(request("name")) and rs("score") = Cstr(request("points")) Then


Note: i could write a few pages of comments on your code because there are quite a few thing you realy should change + somme recommendation.

recommendation:
- put an <? option explicit ?> on top of all your pages
- declare (dim ...) all your variables
- with a small effort, you can make it xhtml. invest in the furture ...
- put a doctype on your page

need to be changed:
- put the connectionstrings in a seperate file (a server side include) with an ASP extension. Place this file above your web-root so it isn't accesible from the web.
When you include the connectionstring in your code, it's possible that they get disclosed, when your server isfalling over or when you simply post them somewhere (like you do now).
I believe it's an intranet application so i'm not gonna bother locating it, but if this would ba on an intranet-server, it would take me just a few minutes to download your db and/ot manipulate it
- always specify your collection. So not request("var") but request.form("var"). It will result in faster processing and it's a lott safer. (well, it makes it at least possible to control the source where the data comes from)
So also not
rs("score") but rs.Fields("score").Value (and even better, replace the rs with rsScores or so? It will make it easier if you start working with more then I recordset inside a page)
- close the recordset and set it to nothing (just like you do with the connection)
- when you insert data, you need to check if the record was indeed inserted.
so instead of just

conn.Execute(sql)

make it

conn.Execute(sql), numinserted

if numinserted = 1 then
.... the rest of your code ....
else
response.write ("Error when inserting record. " & numinserted & " records were inserted.)"
end if

- when you run a select, then always explicitely check if the recordset isn't empty, before you send any output.

I'm not trying to put you down or so, but you might as well get i right from the start on ;)

ellisd5
12-18-2003, 11:09 AM
Thanks raf,

I dont mind you ripping into my code, I dont claim to be no asp expert so its good for some1 to take the time to give me some advise. My attitude when it comes to ASP is the least amount of code I can use the better.

Dale

ellisd5
12-18-2003, 11:32 AM
Still not working, heres my latest code, I have implemented some of your suggestions:-


<HTML>
<HEAD>

<TITLE>Submit Score</TITLE>
<LINK REL="stylesheet" TYPE="text/css" HREF="main.css">

</HEAD>
<BODY>
<!--#include file="db/setConn.inc"-->
<%
Dim rs, postion, gameName, gamePoints

gameName = UCase(request.form("name"))
gamePoints = request.form("points")
sql = "INSERT INTO "& request.form("whichGame") &" VALUES('"& gameName &"', "& gamePoints &")"
conn.Execute(sql)

sql = "SELECT TOP 20 person, score FROM "& request.form("whichGame") &" ORDER BY score desc"
set rs = conn.Execute(sql)

response.Write("<TABLE WIDTH='100%'><TR CLASS='myGreen'><TD COLSPAN='3' CLASS='headblack'>"& request("whichGame") &" TOP SCORE LIST</TD></TR>")
postion = 1
While NOT rs.EOF
if rs("person") = gameName and rs("score") = Cstr(gamePoints) then
Response.Write("<TR class='myLightGreen'>")
else
Response.Write("<TR>")
end if
response.Write("<TD WIDTH='25'>"& postion &"</TD><TD>"& rs("person") &"</TD><TD WIDTH='60' ALIGN='right'>"& rs("score") &"</TD></TR>")
postion = postion+1
rs.MoveNext
WEnd
response.Write("</TABLE>")

rs.close
set rs = nothing
conn.close
set conn = nothing
%>
</BODY>
</HTML>

raf
12-18-2003, 01:36 PM
try changing

if rs("person") = gameName and rs("score") = Cstr(gamePoints) then
Response.Write("<TR class='myLightGreen'>")

into

if rs("person") = gameName and rs("score") = Cstr(gamePoints) then
Response.Write("Condition = OK")
else
response.write ("person from db = " & rs("person") & ", person from form=" & gameName)
response.write ("score from db = " & rs("score") & ", score from form=" & Cstr(gamePoints) )
end if

And then look at the output on your screen when you browse thepage. You might want to add some layout orso to have it nicerdisplayed but you'll get the id, right?

ellisd5
12-18-2003, 01:53 PM
Its ok, I already sorted it

if rs("person") = gameName and Cstr(rs("score")) = Cstr(gamePoints) then

Thats the line that works, seemed stupid converting a string into a String which is what was happening, So im converting the number from the data base into a string and it works :D

raf
12-18-2003, 02:48 PM
Indeed. Thats what i meant to do. My bad.

All values from the formscollection are initially strings so you can do with
if rs("person") = gameName and Cstr(rs("score")) = gamePoints then


But normally, in your formvalue-checking section, you will check if its a numerical value and do the conversion there.
Like
If IsNumeric(request.form("points")) then
gamePoints = CInt(request.form("points"))
else
error = error + "You need to fill in a valid numerical value for the points."
end if

I think it's better to convert the string into an integer (or double), which is what you actually want, then converting the number from the db into a string. Specially if you want to use them in mathematical operations.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum