 | MySQL Database Optimization |  |
|
jamie
HostMySite Sales Rep

| Joined: 19 Mar 2004 |
| Posts: 771 |
| Location: Newark, De |
|
 |
Posted: Tue Oct 12, 2004 12:51 am |
|
 |
 |
 |
 |
Here's a webpage I designed (with Loftboy's help) for an online trading card application. To see this in action goto http://mtgotradingpost.com/login/login.cfm and login with user/pass: test/test.
Here's the question: can this page be done better? I know the checkboxes are ugly - what I'm talking about is the underlying SQL Query. Anything to make is shorter or smoother?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<!-- <br>
<input type="checkbox" name="red" value=1<cfif isDefined("form.red") and form.red eq 1> CHECKED</cfif>>
--->
<form name="form" method="post" action="mycards.cfm">
<fieldset><legend>Search Parameters</legend>
<input type="checkbox" name="white" value="white"<cfif isDefined("form.white") and trim(form.white) eq "white"> CHECKED</cfif>>White
<input type="checkbox" name="blue" value="blue"<cfif isDefined("form.blue") and trim(form.blue) eq "blue"> CHECKED</cfif>>Blue
<input type="checkbox" name="black" value="black"<cfif isDefined("form.black") and trim(form.black) eq "black"> CHECKED</cfif>>Black
<input type="checkbox" name="red" value="red"<cfif isDefined("form.red") and trim(form.red) eq "red"> CHECKED</cfif>>Red
<input type="checkbox" name="green" value="green"<cfif isDefined("form.green") and trim(form.green) eq "green"> CHECKED</cfif>>Green
<input type="checkbox" name="colorless" value="colorless"<cfif isDefined("form.colorless") and trim(form.colorless) eq "colorless"> CHECKED</cfif>>Colorless
<input type="checkbox" name="gold" value="gold"<cfif isDefined("form.gold") and trim(form.gold) eq "gold"> CHECKED</cfif>>Multicolor<br>
<input type="checkbox" name="lands" value="lands"<cfif isDefined("form.lands") and trim(form.lands) eq "lands"> CHECKED</cfif>>Lands
<input type="checkbox" name="artifacts" value="artifacts"<cfif isDefined("form.artifacts") and trim(form.artifacts) eq "artifacts"> CHECKED</cfif>>Artifacts
<input type="checkbox" name="creatures" value="creatures"<cfif isDefined("form.creatures") and trim(form.creatures) eq "creatures"> CHECKED</cfif>>Creatures
<input type="checkbox" name="sorcery" value="sorcery"<cfif isDefined("form.sorcery") and trim(form.sorcery) eq "sorcery"> CHECKED</cfif>>Sorcery
<input type="checkbox" name="instants" value="instants"<cfif isDefined("form.instants") and trim(form.instants) eq "instants"> CHECKED</cfif>>Instants
<input type="checkbox" name="enchantments" value="enchantments"<cfif isDefined("form.enchantments") and trim(form.enchantments) eq "enchantments"> CHECKED</cfif>>Enchantments
</fieldset>
<br>
<br>
<table width="100%" border="1">
<tr>
<th scope="col"><div align="left"><input type="checkbox" name="mirrblock" value="mirrblock"<cfif isDefined("form.mirrblock") and trim(form.mirrblock) eq "mirrblock"> CHECKED</cfif>>Mirrodin Block</div></th>
<th scope="col"><div align="left"><input type="checkbox" name="onslblock" value="onslblock"<cfif isDefined("form.onslblock") and trim(form.onslblock) eq "onslblock"> CHECKED</cfif>>Onslaught Block</div></th>
<th scope="col"><div align="left"><input type="checkbox" name="odysblock" value="odysblock"<cfif isDefined("form.odysblock") and trim(form.odysblock) eq "odysblock"> CHECKED</cfif>>Odyssey</div></th>
<th scope="col"><div align="left"><input type="checkbox" name="invablock" value="invablock"<cfif isDefined("form.invablock") and trim(form.invablock) eq "invablock"> CHECKED</cfif>>Invasion Block </div></th>
</tr>
<tr>
<td><div align="left"><input type="checkbox" name="mirr" value="mirr"<cfif isDefined("form.mirr") and trim(form.mirr) eq "mirr"> CHECKED</cfif>>Mirrodin</div></td>
<td><div align="left"><input type="checkbox" name="onsl" value="onsl"<cfif isDefined("form.onsl") and trim(form.onsl) eq "onsl"> CHECKED</cfif>>Onslaught</div></td>
<td><div align="left"><input type="checkbox" name="odys" value="odys"<cfif isDefined("form.odys") and trim(form.odys) eq "odys"> CHECKED</cfif>>Odyssey</div></td>
<td><div align="left"><input type="checkbox" name="inva" value="inva"<cfif isDefined("form.inva") and trim(form.inva) eq "inva"> CHECKED</cfif>>Invasion</div></td>
</tr>
<tr>
<td><div align="left"><input type="checkbox" name="dark" value="dark"<cfif isDefined("form.dark") and trim(form.dark) eq "dark"> CHECKED</cfif>>Darksteel</div></td>
<td><div align="left"><input type="checkbox" name="legi" value="legi"<cfif isDefined("form.legi") and trim(form.legi) eq "legi"> CHECKED</cfif>>Legions</div></td>
<td><div align="left"><input type="checkbox" name="torm" value="torm"<cfif isDefined("form.torm") and trim(form.torm) eq "torm"> CHECKED</cfif>>Torment</div></td>
<td><div align="left"><input type="checkbox" name="plan" value="plan"<cfif isDefined("form.plan") and trim(form.plan) eq "plan"> CHECKED</cfif>>Planeshift</div></td>
</tr>
<tr>
<td><div align="left"><input type="checkbox" name="fifth" value="fifth"<cfif isDefined("form.fifth") and trim(form.fifth) eq "fifth"> CHECKED</cfif>>5th Dawn</div></td>
<td><div align="left"><input type="checkbox" name="scou" value="scou"<cfif isDefined("form.scou") and trim(form.scou) eq "scou"> CHECKED</cfif>>Scourge</div></td>
<td><div align="left"><input type="checkbox" name="judg" value="judg"<cfif isDefined("form.judg") and trim(form.judg) eq "judg"> CHECKED</cfif>>Judgement</div></td>
<td><div align="left"><input type="checkbox" name="apoc" value="apoc"<cfif isDefined("form.apoc") and trim(form.apoc) eq "apoc"> CHECKED</cfif>>Apocalypse</div></td>
</tr>
</table>
<INPUT type="submit" value="Search"> <INPUT type="Reset" value="Clear">
</form>
<!-- logic goes below here -->
<cfquery name="showmyCards" datasource="#dsn_name#" username="#dsn_login#"
password="#dsn_password#">
select mycards.userID, mycards.CardID, users.username, tblcards.CardTitle, tblcards.ColorCode, mycards.own, mycards.trade, tblcards.RulesText
from mycards, users, tblcards
where mycards.userID = users.userID
and mycards.CardID = tblcards.CardID
and users.username = "#getAuthUser()#"
and tblcards.ColorCode = "X"
<cfif isdefined("form.white") and trim(form.white) EQ "white">
union select mycards.userID, mycards.CardID, users.username, tblcards.CardTitle, tblcards.ColorCode, mycards.own, mycards.trade, tblcards.RulesText
from mycards, users, tblcards
where mycards.userID = users.userID
and users.username = "#getAuthUser()#"
and mycards.CardID = tblcards.CardID
and tblcards.ColorCode = "W"
</cfif>
<cfif isdefined("form.blue") and trim(form.blue) EQ "blue">
union select mycards.userID, mycards.CardID, users.username, tblcards.CardTitle, tblcards.ColorCode, mycards.own, mycards.trade, tblcards.RulesText
from mycards, users, tblcards
where mycards.userID = users.userID
and mycards.CardID = tblcards.CardID
and users.username = "#getAuthUser()#"
and tblcards.ColorCode = "U"
</cfif>
<cfif isdefined("form.black") and trim(form.black) EQ "black">
union select mycards.userID, mycards.CardID, users.username, tblcards.CardTitle, tblcards.ColorCode, mycards.own, mycards.trade, tblcards.RulesText
from mycards, users, tblcards
where mycards.userID = users.userID
and mycards.CardID = tblcards.CardID
and users.username = "#getAuthUser()#"
and tblcards.ColorCode = "B"
</cfif>
<cfif isdefined("form.red") and trim(form.red) EQ "red">
union select mycards.userID, mycards.CardID, users.username, tblcards.CardTitle, tblcards.ColorCode, mycards.own, mycards.trade, tblcards.RulesText
from mycards, users, tblcards
where mycards.userID = users.userID
and mycards.CardID = tblcards.CardID
and users.username = "#getAuthUser()#"
and tblcards.ColorCode = "R"
</cfif>
<cfif isdefined("form.green") and trim(form.green) EQ "green">
union select mycards.userID, mycards.CardID, users.username, tblcards.CardTitle, tblcards.ColorCode, mycards.own, mycards.trade, tblcards.RulesText
from mycards, users, tblcards
where mycards.userID = users.userID
and mycards.CardID = tblcards.CardID
and users.username = "#getAuthUser()#"
and tblcards.ColorCode = "G"
</cfif>
<cfif isdefined("form.colorless") and trim(form.colorless) EQ "colorless">
union select mycards.userID, mycards.CardID, users.username, tblcards.CardTitle, tblcards.ColorCode, mycards.own, mycards.trade, tblcards.RulesText
from mycards, users, tblcards
where mycards.userID = users.userID
and mycards.CardID = tblcards.CardID
and users.username = "#getAuthUser()#"
and tblcards.ColorCode = "A"
</cfif>
<cfif isdefined("form.gold") and trim(form.gold) EQ "gold">
union select mycards.userID, mycards.CardID, users.username, tblcards.CardTitle, tblcards.ColorCode, mycards.own, mycards.trade, tblcards.RulesText
from mycards, users, tblcards
where mycards.userID = users.userID
and mycards.CardID = tblcards.CardID
and users.username = "#getAuthUser()#"
and tblcards.ColorCode = "Z"
</cfif>
<cfif isdefined("form.lands") and trim(form.lands) EQ "lands">
union select mycards.userID, mycards.CardID, users.username, tblcards.CardTitle, tblcards.ColorCode, mycards.own, mycards.trade, tblcards.RulesText
from mycards, users, tblcards
where mycards.userID = users.userID
and mycards.CardID = tblcards.CardID
and users.username = "#getAuthUser()#"
and tblcards.SpellTypeID = "111"
</cfif>
<cfif isdefined("form.artifacts") and trim(form.artifacts) EQ "artifacts">
union select mycards.userID, mycards.CardID, users.username, tblcards.CardTitle, tblcards.ColorCode, mycards.own, mycards.trade, tblcards.RulesText
from mycards, users, tblcards
where mycards.userID = users.userID
and mycards.CardID = tblcards.CardID
and users.username = "#getAuthUser()#"
and tblcards.SpellTypeID = "107"
</cfif>
<cfif isdefined("form.creatures") and trim(form.creatures) EQ "creatures">
union select mycards.userID, mycards.CardID, users.username, tblcards.CardTitle, tblcards.ColorCode, mycards.own, mycards.trade, tblcards.RulesText
from mycards, users, tblcards
where mycards.userID = users.userID
and mycards.CardID = tblcards.CardID
and users.username = "#getAuthUser()#"
and tblcards.SpellTypeID = "104"
</cfif>
<cfif isdefined("form.sorcery") and trim(form.sorcery) EQ "sorcery">
union select mycards.userID, mycards.CardID, users.username, tblcards.CardTitle, tblcards.ColorCode, mycards.own, mycards.trade, tblcards.RulesText
from mycards, users, tblcards
where mycards.userID = users.userID
and mycards.CardID = tblcards.CardID
and users.username = "#getAuthUser()#"
and tblcards.SpellTypeID = "109"
</cfif>
<cfif isdefined("form.instants") and trim(form.instants) EQ "instants">
union select mycards.userID, mycards.CardID, users.username, tblcards.CardTitle, tblcards.ColorCode, mycards.own, mycards.trade, tblcards.RulesText
from mycards, users, tblcards
where mycards.userID = users.userID
and mycards.CardID = tblcards.CardID
and users.username = "#getAuthUser()#"
and tblcards.SpellTypeID = "110"
</cfif>
<cfif isdefined("form.enchantments") and trim(form.enchantments) EQ "enchantments">
union select mycards.userID, mycards.CardID, users.username, tblcards.CardTitle, tblcards.ColorCode, mycards.own, mycards.trade, tblcards.RulesText
from mycards, users, tblcards
where mycards.userID = users.userID
and mycards.CardID = tblcards.CardID
and users.username = "#getAuthUser()#"
and tblcards.SpellTypeID = "105"
and tblcards.SpellTypeID = "112"
</cfif>
ORDER BY users.username
</cfquery>
<cfset current_user = getAuthUser()>
<p>Currently displaying info for user:
<cfoutput>#current_user#</cfoutput><br>
Color Code:<br>
Black = B<br>
White = W<br>
Blue = U<br>
Green = G<br>
Colorless = A<br>
Multi-Color (Gold) = Z
</p>
<table width="100%" border="1">
<tr>
<th scope="col">Username</th>
<th scope="col">Card Name</th>
<th scope="col">Color</th>
<th scope="col">Number Owned</th>
<th scope="col">Number Looking For</th>
<th scope="col">Card Text</th>
</tr>
<cfoutput query="showmyCards">
<tr>
<td><div align="center">#username#</td></div>
<td><div align="center">#CardTitle#</td></div>
<td><div align="center">#ColorCode#</td></div>
<td><div align="center">#own#</td></div>
<td><div align="center">#trade#</td></div>
<td><div align="left">#RulesText#</td></div>
</tr>
</cfoutput>
</table>
</body>
</html> |
BTW, the three tables referenced above are
mycards - contains the cards that individual users own
tblcards - contains details on each card
users - contains user login information
Please let me know your thoughts!
|
|
|
 |
 | |  |
|
bobclingan
Forum Regular
| Joined: 16 Sep 2004 |
| Posts: 271 |
| Location: Abingdon, MD |
|
 |
Posted: Tue Oct 12, 2004 2:32 pm |
|
 |
 |
 |
 |
Two things..
if you used <cfparam> you wouldn't have to do all that isDefined crap everytime.
For the query, you could try using a loop to iterate over the query since it seems all that changes is the spellType. Just make sure you have a check to leave the union off if its the last one.
I can elaborate a little more if needed.
|
|
|
|
bobclingan
Forum Regular
| Joined: 16 Sep 2004 |
| Posts: 271 |
| Location: Abingdon, MD |
|
 |
Posted: Tue Oct 12, 2004 4:30 pm |
|
 |
 |
 |
 |
<cfparam> is used to set a default value- a kind of "catch-all" if the variable doesn't exist- like with checkboxes on a form field to use it just go:
<cfparam name="form.checkboxName" default="0">
where name is the name of your variable and default is your default value.
For the query, I'll post an example of what I was thinking when I have more time later today.
|
|
|
 |
 | |  |
 |
 | |  |
|
bobclingan
Forum Regular
| Joined: 16 Sep 2004 |
| Posts: 271 |
| Location: Abingdon, MD |
|
 |
Posted: Wed Oct 13, 2004 12:05 pm |
|
 |
 |
 |
 |
Jamie,
This is kinda what I was thinking although my brain is fried this week and I'm not sure I can explain it clearly.
<cfquery name="someList" datasource="#request.datasource#">
<cfloop
index="ii"
list="#trim(attributes.ID)#"
delimiters=",">
<cfset request.loopCount = incrementValue(request.loopCount)>
SELECT (your columns)
FROM (yourTable)
<!--- join the tables unless the last --->
<cfif request.loopCount lt request.listLen>
UNION
</cfif>
</cfloop>
</cfquery> |
Where the loop is a comma delimited list of the items you want to union together. If you are getting this list from a query, you can use ValueList() to convert it.
|
|
|
 |
 | |  |
|
Gerd
| Joined: 12 Oct 2004 |
| Posts: 11 |
| Location: Deltona, FL |
|
 |
Posted: Wed Oct 13, 2004 2:47 pm |
|
 |
 |
 |
 |
Hi Jamie,
The way your using checkboxes is typical of coldfusion newbies. I used to do it the same way when I 1st got started.
Try this train of thought instead; Name all your check boxes the same.
and then loop throught the list. (checkboxes values are returned as a list by default in Coldfusion)
So....
<cfparam name="color" default="">
<input type="checkbox" name="color" value="W"
<cfif trim(form.white) eq "W"> CHECKED</cfif>>White
<input type="checkbox" name="color" value="B"
<cfif trim(form.white) eq "B"> CHECKED</cfif>>White
<!--- Calling page just before your Query --->
<cfif isDefined('form.fieldnames') and Form.fieldnames EQ 'color'>
<cfoutput>
<!--- we create a var for the list (name it) --->
<cfset colorlist="#form.color#">
<!--- now we format the list so it's SQL friendly (in SQL format) --->
<cfset colorlist = #ListchangeDelims(colorlist, "','")#>
</cfoutput>
</cfif>
<cfquery name="showmyCards" datasource="#dsn_name#" username="#dsn_login#"
password="#dsn_password#">
SELECT mycards.userID, mycards.CardID, users.username, tblcards.CardTitle, tblcards.ColorCode, mycards.own, mycards.trade, tblcards.RulesText
FROM mycards, users, tblcards
WHERE mycards.userID = users.userID
AND mycards.CardID = tblcards.CardID
AND users.username = "#getAuthUser()#"
AND tblcards.ColorCode IN ('#colorlist#')
</cfquery>
This is for the color codes only! You could do the same with spelltypeID.
This is pseudo code but I'm pretty sure it should work for you.
|
|
|
 |
 | |  |
 | Multiple Check box Query |  |
|
Gerd
| Joined: 12 Oct 2004 |
| Posts: 11 |
| Location: Deltona, FL |
|
 |
Posted: Wed Oct 13, 2004 5:35 pm |
|
 |
 |
 |
 |
Of course... Copy and paste the following into a new coldfusion template and name it test.cfm. RUN IT and view the ouput. It will help you make senese of what's happening.
<cfoutput>
<form action="#path_info#" method="post">
<input type="checkbox" name="color" value="W"> White<br />
<input type="checkbox" name="color" value="B"> Black<br />
<input type="checkbox" name="color" value="R"> Red<br />
<input type="submit" value="submit">
</form>
<cfif isDefined('form.fieldnames') and Form.fieldnames EQ 'color'>
<!--- we create a var for the list (name it) --->
<cfset colorlist="#form.color#">
<!--- now we format the list so it's SQL friendly (in SQL format) --->
<cfset colorlist = #ListchangeDelims(colorlist, "','")#>
#colorlist#
then when we surround the outside of the variable with the single quotes it's formated to use a list in your sql statement with
the 'IN' predicate ie...
WHERE blah blah = "blah blah
AND tblcards.ColorCode IN ('#colorlist#')
</cfif>
</cfoutput>
|
[/code]
|
|
|
 |
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
All times are GMT
Page 1 of 1
|
|
|
|
|