Cleaning code...

This is a discussion on "Cleaning code..." within the Classic ASP section. This forum, and the thread "Cleaning code... are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > Classic ASP

Notices


Closed Thread
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jul 4th, 2004, 22:06
Highly Reputable Member
Join Date: Aug 2003
Location: Australia
Posts: 662
Thanks: 0
Thanked 0 Times in 0 Posts
Cleaning code...

ok, this is what I have .
Code: Select all
<%
Dim moneyLeft
Dim totPrice
Dim WeapID

totPrice = Request.Form("Price")
WeapID = Request.Form("WeapID")

Set connectionToDatabase=Server.CreateObject("ADODB.Connection")
connectionToDatabase.ConnectionTimeout=60
connectionToDatabase.Open  "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=MyDatabase"

Set recordSet=Server.CreateObject("ADODB.Recordset")
recordSet.Open "SELECT * FROM Setup WHERE Username='" & ID &  "'",connectionToDatabase,1,2

moneyLeft = recordSet("Gill") - totPrice

If recordSet("Gill") >= totPrice THEN
 If recordSet("Weapon") <> WeapID THEN
recordSet("Gill") = moneyLeft
recordSet("Weapon") = WeapID
recordSet("Attack") = recordSet("Attack") + 3
recordSet.Update
Response.Write(recordSet("Gill"))

Else 
If recordSet("Gill") < totPrice THEN
Response.Write("Im sorry, you don't have enough money")
Else 
If recordSet("Weapon") <> "" THEN
Response.Write("You currently have ")
Response.Write(recordSet("Weapon"))
Response.Write(" equipped")
Response.Write("
")
Else Response.Write("")
End If

If recordSet("Item1") = "1" THEN
recordSet("Item1") = WeapID
recordSet("Gill") = moneyLeft
recordSet.Update
Response.Write(WeapID)
Response.Write(" was stored in your Items")
Else 
If recordSet("Item2") = "1" THEN
recordSet("Item2") = WeapID
recordSet("Gill") = moneyLeft
recordSet.Update
Response.Write(WeapID)
Response.Write(" was stored in your Items")
Else 
If recordSet("Item3") = "1" THEN
recordSet("Item3") = WeapID
recordSet("Gill") = moneyLeft
recordSet.Update
Response.Write(WeapID)
Response.Write(" was stored in your Items")
Else 
If recordSet("Item4") = "1" THEN
recordSet("Item4") = WeapID
recordSet("Gill") = moneyLeft
recordSet.Update
Response.Write(WeapID)
Response.Write(" was stored in your Items")
Else 
If recordSet("Item5") = "1" THEN
recordSet("Item5") = WeapID
recordSet("Gill") = moneyLeft
recordSet.Update
Response.Write(WeapID)
Response.Write(" was stored in your Items")
Else 
If recordSet("Item6") = "1" THEN
recordSet("Item6") = WeapID
recordSet("Gill") = moneyLeft
recordSet.Update
Response.Write(WeapID)
Response.Write(" was stored in your Items")
Else 
If recordSet("Item7") = "1" THEN
recordSet("Item7") = WeapID
recordSet("Gill") = moneyLeft
recordSet.Update
Response.Write(WeapID)
Response.Write(" was stored in your Items")
Else 
If recordSet("Item8") = "1" THEN
recordSet("Item8") = WeapID
recordSet("Gill") = moneyLeft
recordSet.Update
Response.Write(WeapID)
Response.Write(" was stored in your Items")
Else 
If recordSet("Item9") = "1" THEN
recordSet("Item9") = WeapID
recordSet("Gill") = moneyLeft
recordSet.Update
Response.Write(WeapID)
Response.Write(" was stored in your Items")
Else 
If recordSet("Item10") = "1" THEN
recordSet("Item10") = WeapID
recordSet("Gill") = moneyLeft
recordSet.Update
Response.Write(WeapID)
Response.Write(" was stored in your Items")
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
connectionToDatabase.close
Set connectionToDatabase=Nothing
%>
Just wondering if there is an easier way of doing the same thing. This is the only way I personally know how. Thanks.

  #2 (permalink)  
Old Jul 4th, 2004, 22:42
Most Reputable Member
Join Date: Jul 2003
Posts: 1,856
Thanks: 0
Thanked 0 Times in 0 Posts
I would at least trim it down to this, though I'm not sure if the If statements are supposed to be exclusive - if so you'd want to put a . I never use the .update method of recordsets personally. I use SQL UPDATE queries instead.

Code: Select all
<%
Dim moneyLeft
Dim totPrice
Dim WeapID

totPrice = Request.Form("Price")
WeapID = Request.Form("WeapID")

Set con=Server.CreateObject("ADODB.Connection")
con.Open  "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=MyDatabase"

set rs = con.execute("SELECT * FROM Setup WHERE Username='" & ID &  "'",,1)

moneyLeft = rs("Gill") - totPrice

If rs("Gill") >= totPrice THEN
  If rs("Weapon") <> WeapID THEN
    rs("Gill") = moneyLeft
    rs("Weapon") = WeapID
    rs("Attack") = rs("Attack") + 3
    rs.Update
    Response.Write(rs("Gill"))
  Else 
    If rs("Gill") < totPrice THEN
      Response.Write("Im sorry, you don't have enough money")
    Else 
      If rs("Weapon") <> "" THEN
        Response.Write("You currently have ")
        Response.Write(rs("Weapon"))
        Response.Write(" equipped")
        Response.Write("
")
        Else Response.Write("")
      End If
      
      bstored = false
      for i = 1 to 10
        If rs("Item" & i) = "1" and bstored=false THEN
          rs("Item" & i) = WeapID
          rs("Gill") = moneyLeft
          rs.Update
          Response.Write(WeapID)
          Response.Write(" was stored in your Items")
          bstored = true
        end if
      next
    End If
  End If
End If

con.close
Set con=Nothing
%>
  #3 (permalink)  
Old Jul 6th, 2004, 00:09
Highly Reputable Member
Join Date: Aug 2003
Location: Australia
Posts: 662
Thanks: 0
Thanked 0 Times in 0 Posts
cool, I like the way you did that. I didn't think about doing it that way. It's still got about 10-15 more checks to do but this is the fuondation for my building, the rest I will figure out. I appreciate the help. Also, I noticed that you used rs and con. are these just to save you time or do they actually work? I would love to find out for myself, but my internet connection is down and I am having to use another computer. Thanks again for the help.
  #4 (permalink)  
Old Jul 6th, 2004, 06:14
Most Reputable Member
Join Date: Jul 2003
Posts: 1,856
Thanks: 0
Thanked 0 Times in 0 Posts
No problem. Coding style varies from person to person.

rs and con are to save time and simplify the code. If you're not using Option Explicit and you use long variable names you're going to end up with typos in your variables, which can be wicked-hard to catch.

You might also consider making a function to do a check and calling it when you need to, or using SELECT CASE instead of lots of IF THEN statements.
Closed Thread

Tags
cleaning, code

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
London Dry Cleaning Company spinal007 Free Web Site Critique 19 Sep 17th, 2007 11:39
Eco Friendly and Reliable Domestic and Carpet Cleaning Services in London Axel Free Web Site Critique 10 Aug 17th, 2007 09:11
Cleaning up footer and positioning genista Web Page Design 10 Aug 14th, 2007 14:28
Can somebody give me the code to hide the source code? renren JavaScript Forum 7 Mar 7th, 2006 12:27
History cleaning development joachim shotter Job Opportunities 1 Oct 30th, 2005 11:20


All times are GMT. The time now is 00:02.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43