Skip to main content
July 28, 2007
Question

SELECT AND/OR

  • July 28, 2007
  • 4 replies
  • 515 views
Hi, here is what I am trying to do: I have a large form where users fill out to search for profiles based on their age, country, state, personality type, eye color etc etc.

After each form element (country, age, state etc) I have a checkbox with a label "Must Have Criteria". If the user checks this MUST HAVE CRITERIA box for "country=canada" and also for "age=30-45" but selects a personality type of "Out Going" (but did NOT check the MUST HAVE CRITERIA box), then even if there are profiles from Canada who are a personality type "Out Going", if that same profile is not inbetween the age of 30-45, then I want NO RESULTS.

Now, if the user does NOT select the MUST HAVE CRITERIA box for anything, then the results will be based on ANY of the criteria. Does this make sense?

An example of exactly what I want to do can be found at http://personals.yahoo.com/.
After you hit the Search button, you'll notice a left hand multi drop-down menu. Drop down the "Personality Type" and you'll notice at the bottom: "Must Have Criteria". This is what I want to do!

I am assuming that if the MUST HAVE CRITERIA box is checked, then the SQL operater must be an "AND" as opposed to an "OR". Is this how it is done? (Sure aint werkin the way I got it below!)

Here is my SQL statement attempt for a few Form elements:
---------------------------------------------------------------------------------------------------
<!--- STATEPROVINCE --->
<cfelseif isDefined("Form.stateprovince")>
<cfif isDefined("Form.state_musthave")> <<<<<<<<This is the MUST HAVE checkbox
AND stateprovince LIKE '#Form.stateprovince#'
<cfelse>
OR stateprovince LIKE '#Form.stateprovince#'
</cfif>

<!--- CITY --->
<cfelseif isDefined("Form.city")>
<cfif isDefined("Form.city_musthave")> <<<<<<<<This is the MUST HAVE checkbox
AND city LIKE '#Form.city#'
<cfelse>
OR city LIKE '#Form.city#'
</cfif>

<!--- LOOKINGFOR --->
<cfelseif isDefined("Form.lookingfor_checkboxes")>
<cfif isDefined("Form.lookingfor_musthave")> <<<<<<<<This is the MUST HAVE checkbox
AND lookingfor IN
<cfelse>
OR lookingfor IN
</cfif>
(
<cfqueryparam cfsqltype="cf_sql_VARCHAR" list="true"
value="#form.lookingfor_checkboxes#">
)

<!--- PERSONALITY TYPE --->
<cfelseif isDefined("Form.personality_checkboxes")>
<cfif isDefined("Form.personality_musthave")> <<<<<<<<This is the MUST HAVE checkbox
AND personalitytype IN
<cfelse>
OR personalitytype IN
</cfif>
(
<cfqueryparam cfsqltype="cf_sql_VARCHAR" list="true"
value="#form.personality_checkboxes#">
)
---------------------------------------------------------------------------------------------------

Any help in getting this to function would be awsome!
This topic has been closed for replies.

4 replies

July 29, 2007
Thanks, Dan!
July 28, 2007
Hi Azadi,

I guess what you are saying is that I should only use a "must have criteria" for multiple possible selections (ie - multiple checkboxes) correct? I think that is making more sense.

Now, in a multiple selection, if the user wants to view profiles with 3 different Personality Types (eg - outgoing, shy, reserved) and no other personality types, I assume I would have to see if the MUST HAVE box is selected, and if so, use the AND operator, if not, then use the OR operater to select ANY of them, right?
-----------------------------------------------------------------------------
<!--- PERSONALITY TYPE --->
<cfelseif isDefined("Form.personality_checkboxes")>
<cfif isDefined("Form.personality_musthave")> <This is the MUST HAVE checkbox
AND personalitytype IN
<cfelse>
OR personalitytype IN
</cfif>
-----------------------------------------------------------------------------

Also, I think your suggestion to use the "ANY" option is also nice (Yahoo personals uses that). So, if the user selects "ANY" (I could also set that as a default choice) what would THAT SQL statement look like? I assume I would have to initially set a List Array when the form page loads and load the list with all personality selections and pass that list to the search page if the user does not deselect "ANY"?
Inspiring
July 28, 2007
My guess is that you are getting too many results. As soon as a must have box is unchecked, the keyword "or" is going to give you a lot more records than you want.

Try doing a query of query to narrow down your results to the "must haves".
July 28, 2007
Hi Dan, I'm not too sure what a "query of query" is. Do you have a link for me that discusses this in relation to what I am trying to do? :)
Inspiring
July 29, 2007
quote:

Originally posted by: jerreye04
Hi Dan, I'm not too sure what a "query of query" is. Do you have a link for me that discusses this in relation to what I am trying to do? :)

http://livedocs.adobe.com/coldfusion/7/htmldocs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=ColdFusion_Documentation&file=00001265.htm

In terms of what you are trying to do, run your query as per your original post. Then use Q of Q to do that Must Have stuff.
Inspiring
July 28, 2007
exact execution aside, let's ponder your logic for a moment:

what if NO must-have checkbox has been checked? will you return ALL
results contained in your db then? based on the description you provided
it looks like that is what is going to happen...

in general, all your separate criteria should be combined using AND
operator. OR operator should be used between multiple-choice criteria
(i.e. if several personalty types have been selected by user), if you
have any.

basically, if a user has selected anything, that is a must-have
criteria. don't forget that select boxes (<select> or <cfselect>
elements) will ALWAYS have a value, even if no item has been explicitly
selected by user in them. so in your select boxes (if you have them)
provide an option like "Any", which you can also make a default choice
if no exact choice is required (ie. with a select box like "country" you
my want to enforce a selection, so a user from canada is not presented
with results from uruguay...)

please do let me know if i am totally wrong in regard to your logic,
which i may be...

---
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com
July 28, 2007
Edit - delete