I have a form that I submit with these variables: category_time
, date_month
, date_year
, and status_time
.
Here is an example of different form possibilities:
CATEGORY_TIME 1 DATE_MONTH Month: DATE_YEAR Year: FIELDNAMES DATE_MONTH,DATE_YEAR,SUBMIT STATUS_TIME Select: SUBMIT submit ---------------- CATEGORY_TIME Choose: DATE_MONTH Month: DATE_YEAR Year: FIELDNAMES DATE_MONTH,DATE_YEAR,SUBMIT STATUS_TIME Pending SUBMIT submit ---------------- CATEGORY_TIME Choose: DATE_MONTH Month: DATE_YEAR 2016 FIELDNAMES DATE_MONTH,DATE_YEAR,SUBMIT STATUS_TIME Select: SUBMIT submit
After I submit the form depending on the variables it outputs a query. For example, with the first possibility only category_time
chose, it will output the second if
statement.
From what you can see, there's a lot of if
s statements that will be needed. Is there an easier way to make this work without so many if
s statement and just depending on the variables that are chosen, even if I have to use JavaScript to make it work better?
<cfif structKeyExists(form, "category_time") || structKeyExists(form, "date_month") || structKeyExists(form, "date_year") || structKeyExists(form, "status_time") >
<cfquery ....>
SELECT *
FROM work_timeline
<cfif (category_time eq 'ALL' || category_time eq 'Choose:') && date_month eq 'Month:' && date_year eq 'Year:' && status_time eq 'Select:' >
order by year(date_time) desc, month(date_time) desc
</cfif>
<cfif category_time neq 'ALL' && category_time neq 'Choose:' && date_month eq 'Month:' && date_year eq 'Year:' && status_time eq 'Select:' >
where category_time = #category_time#
</cfif>
<cfif structKeyExists(form, "category_time") && date_month neq 'Month:' && date_year eq 'Year:' && category_time neq 'Choose:'
and category_time neq 'ALL' && status eq 'Select:'>
where category_time = #category_time# and month(date_time)=#date_month#
</cfif>
more if's statments .....
</cfquery>
</cfif>
where
clause are relatively self explanatory, but I am not sure I follow how/why the filters impact theorder by
. Also, does the database table actually contain a record with the value "ALL", as the snippet above implies? – Leigh Dec 4 '15 at 19:35where category_time = 'All'
? – Leigh Dec 4 '15 at 23:29