Answered
Help with CFIF statement
I don't know how advanced this is but I didn't see another
relevant place for this question.
I have the following price discount table in SQL Server:
Type Discount Range Expires
P 0.50 ALL 10/30/2008
P 0.10 C100 10/30/2008
P 0.15 C200 10/30/2008
When a user selects a product (i.e. product C200) I want to write code that looks in this table (called PDISCOUNT) and first checks to see if there is a Type of P that has a range of ALL. If so, it applies that discount and that's it. If not, it looks through the rest of the table to see if there is a discount that matches the product number.
The code I've got is resulting in the following scenario:
1. If there is a type of P and a range of ALL it applies the proper discount.
2. If not it will apply the discount if the range equals the product number ONLY IF it is the only product number containing a discount.
Here is my code:
<cfquery name="getALL" datasource="#DSource#" dbtype="ODBC" >
SELECT *
FROM priceDiscounts
WHERE expdate > #Now()# AND type = 'P' AND range = 'ALL'
</cfquery>
<cfquery name="getRANGE" datasource="#DSource#" dbtype="ODBC" >
SELECT *
FROM priceDiscounts
WHERE expdate > #Now()# AND type = 'P' AND range <> 'ALL'
</cfquery>
TEST:
<CFIF getALL.RecordCount neq 0>
<cfoutput>
<cfset sale = #field7# * #getALL.discount#>
<cfset weight = #field7# - #sale#>
<cfset dollars = #ListFirst(weight, ".")#>
<cfset cents = #ListLast(NumberFormat(weight,.99),".")#>
$#val(dollars)#.#cents#
</cfoutput>
<CFELSEIF getRange.RecordCount neq 0 AND getALL.RecordCount eq 0 AND getPage.Location CONTAINS getRange.range>
<cfoutput>
<cfset sale = #field7# * #getRange.discount#>
<cfset weight = #field7# - #sale#>
<cfset dollars = #ListFirst(weight, ".")#>
<cfset cents = #ListLast(NumberFormat(weight,.99),".")#>
$#val(dollars)#.#cents#
</cfoutput>
<CFELSE>
<cfoutput>
<cfset weight = #field7#>
<cfset dollars = #ListFirst(weight, ".")#>
<cfset cents = #ListLast(NumberFormat(weight,.99),".")#>
$#val(dollars)#.#cents#
</cfoutput>
</CFIF>
I am by no means an expert and there must be a more efficient way of doing this as well. Any help would be greatly appreciated.
I have the following price discount table in SQL Server:
Type Discount Range Expires
P 0.50 ALL 10/30/2008
P 0.10 C100 10/30/2008
P 0.15 C200 10/30/2008
When a user selects a product (i.e. product C200) I want to write code that looks in this table (called PDISCOUNT) and first checks to see if there is a Type of P that has a range of ALL. If so, it applies that discount and that's it. If not, it looks through the rest of the table to see if there is a discount that matches the product number.
The code I've got is resulting in the following scenario:
1. If there is a type of P and a range of ALL it applies the proper discount.
2. If not it will apply the discount if the range equals the product number ONLY IF it is the only product number containing a discount.
Here is my code:
<cfquery name="getALL" datasource="#DSource#" dbtype="ODBC" >
SELECT *
FROM priceDiscounts
WHERE expdate > #Now()# AND type = 'P' AND range = 'ALL'
</cfquery>
<cfquery name="getRANGE" datasource="#DSource#" dbtype="ODBC" >
SELECT *
FROM priceDiscounts
WHERE expdate > #Now()# AND type = 'P' AND range <> 'ALL'
</cfquery>
TEST:
<CFIF getALL.RecordCount neq 0>
<cfoutput>
<cfset sale = #field7# * #getALL.discount#>
<cfset weight = #field7# - #sale#>
<cfset dollars = #ListFirst(weight, ".")#>
<cfset cents = #ListLast(NumberFormat(weight,.99),".")#>
$#val(dollars)#.#cents#
</cfoutput>
<CFELSEIF getRange.RecordCount neq 0 AND getALL.RecordCount eq 0 AND getPage.Location CONTAINS getRange.range>
<cfoutput>
<cfset sale = #field7# * #getRange.discount#>
<cfset weight = #field7# - #sale#>
<cfset dollars = #ListFirst(weight, ".")#>
<cfset cents = #ListLast(NumberFormat(weight,.99),".")#>
$#val(dollars)#.#cents#
</cfoutput>
<CFELSE>
<cfoutput>
<cfset weight = #field7#>
<cfset dollars = #ListFirst(weight, ".")#>
<cfset cents = #ListLast(NumberFormat(weight,.99),".")#>
$#val(dollars)#.#cents#
</cfoutput>
</CFIF>
I am by no means an expert and there must be a more efficient way of doing this as well. Any help would be greatly appreciated.
