14_east
asked on
Check for a certain value in a list
So I have a list that I make based on items in a shopping cart. It is named [myNewProdList]. It is functioning fine... it builds a list of 1's or 0's. Lets say the list looks like this: 0,0,0,0,0,1,0,0,0
I want to write a cfif script to say something like... if there's a 1 in the list, do this, else, do that. But I'm stuck...
I want to write a cfif script to say something like... if there's a 1 in the list, do this, else, do that. But I'm stuck...
ASKER
NOR does this:
<cfif find("1",myNewProdList)>Pe rishable ITEMS are in the cart.</cfif>
<cfif find("1",myNewProdList)>Pe
ASKER
OK, this is weird. The listFind works great, I found my problem. The following code returns the following on my screen:
740,255
1 0
PROBLEM: why is there no comma in my second list?
740,255
1 0
PROBLEM: why is there no comma in my second list?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<cfquery name="rsGetCartPer" datasource="#request.dsn#" username="#request.dsnUsername#" password="#request.dsnPassword#">
SELECT cart_custcart_ID,cart_sku_ID
FROM tbl_cart
WHERE cart_custcart_ID = '#Client.CartID#'
</cfquery>
<cfset myPerList = Valuelist(rsGetCartPer.cart_sku_ID)>
<cfoutput>#myPerList#</cfoutput><br />
<br />
<cfif rsGetCartPer.recordcount GTE 1>
<cfloop From = "1" To = "#ListLen(myPerList)#" index = "Counter">
<cfquery name="rsGetPerSKUS" datasource="#request.dsn#" username="#request.dsnUsername#" password="#request.dsnPassword#">
SELECT tbl_skus.SKU_ID,tbl_skus.SKU_ProductID,tbl_products.product_ID,tbl_products.product_per
FROM tbl_skus
INNER JOIN tbl_products ON tbl_skus.SKU_ProductID = tbl_products.product_ID
WHERE SKU_ID = #ListGetAt(myPerList, Counter)#
</cfquery>
<cfset myNewProdList = ValueList(rsGetPerSKUS.product_per)>
<cfoutput>#myNewProdList#</cfoutput>
</cfloop>
</cfif>
<br />
<br />
<br />
<br />
<cfif listFind(myNewProdList, "1")>
Perishable ITEMS are in the cart.
</cfif>
</body>
</html>
use LISTCONTAINS
Description
Determines the index of the first list element that contains a specified substring.
Returns
Index of the first list element that contains substring. If not found, returns zero.
Category
List functions
Function syntax
ListContains(list, substring [, delimiters ])
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions_l_06.html
Description
Determines the index of the first list element that contains a specified substring.
Returns
Index of the first list element that contains substring. If not found, returns zero.
Category
List functions
Function syntax
ListContains(list, substring [, delimiters ])
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions_l_06.html
That is because you are LOOPING and displaying the list, the code in BOLD everytime creates the new list .. in each LOOP....
<cfloop From = "1" To = "#ListLen(myPerList)#" index = "Counter">
<cfquery name="rsGetPerSKUS" datasource="#request.dsn#" username="#request.dsnUser name#" password="#request.dsnPass word#">
SELECT tbl_skus.SKU_ID,tbl_skus.S KU_Product ID,tbl_pro ducts.prod uct_ID,tbl _products. product_pe r
FROM tbl_skus
INNER JOIN tbl_products ON tbl_skus.SKU_ProductID = tbl_products.product_ID
WHERE SKU_ID = #ListGetAt(myPerList, Counter)#
</cfquery>
<cfset myNewProdList = ValueList(rsGetPerSKUS.pro duct_per)>
<cfoutput>#myNewProdList#< /cfoutput>
</cfloop>
<cfloop From = "1" To = "#ListLen(myPerList)#" index = "Counter">
<cfquery name="rsGetPerSKUS" datasource="#request.dsn#"
SELECT tbl_skus.SKU_ID,tbl_skus.S
FROM tbl_skus
INNER JOIN tbl_products ON tbl_skus.SKU_ProductID = tbl_products.product_ID
WHERE SKU_ID = #ListGetAt(myPerList, Counter)#
</cfquery>
<cfset myNewProdList = ValueList(rsGetPerSKUS.pro
<cfoutput>#myNewProdList#<
</cfloop>
ASKER
I dont understand how it is returning the correct string, but doesnt have commas included. thus, the code: <cfif listFind(myNewProdList, "1")>
Perishable ITEMS are in the cart.
</cfif>
returns nothing, even though there is a 1 in the list
Perishable ITEMS are in the cart.
</cfif>
returns nothing, even though there is a 1 in the list
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I just tried your suggestion: <cfif LISTCONTAINS(myNewProdList , '1')>
didnt make any difference... if i change the above 1 to a 0, it sees the 0 in the list. The list reads [ 1 0] again, with no commas. It doesnt recognize the 1, only the zero... got any idea why?
didnt make any difference... if i change the above 1 to a 0, it sees the 0 in the list. The list reads [ 1 0] again, with no commas. It doesnt recognize the 1, only the zero... got any idea why?
>>I dont understand how it is returning the correct string, but doesnt have commas included
MOVE your CFOUTPUT outside the LOOP, you will NOT get the entire string, only the last value.. your CFOUTPUT is in the loop therefore it is returing you the individual values EVERY TIME while looping...
Try below, you will ONLY get one value, that would be the last one...
<cfloop From = "1" To = "#ListLen(myPerList)#" index = "Counter">
<cfquery name="rsGetPerSKUS" datasource="#request.dsn#" username="#request.dsnUser name#" password="#request.dsnPass word#">
SELECT tbl_skus.SKU_ID,tbl_skus.S KU_Product ID,tbl_pro ducts.prod uct_ID,tbl _products. product_pe r
FROM tbl_skus
INNER JOIN tbl_products ON tbl_skus.SKU_ProductID = tbl_products.product_ID
WHERE SKU_ID = #ListGetAt(myPerList, Counter)#
</cfquery>
<cfset myNewProdList = ValueList(rsGetPerSKUS.pro duct_per)>
</cfloop>
<cfoutput>#myNewProdList#< /cfoutput>
MOVE your CFOUTPUT outside the LOOP, you will NOT get the entire string, only the last value.. your CFOUTPUT is in the loop therefore it is returing you the individual values EVERY TIME while looping...
Try below, you will ONLY get one value, that would be the last one...
<cfloop From = "1" To = "#ListLen(myPerList)#" index = "Counter">
<cfquery name="rsGetPerSKUS" datasource="#request.dsn#"
SELECT tbl_skus.SKU_ID,tbl_skus.S
FROM tbl_skus
INNER JOIN tbl_products ON tbl_skus.SKU_ProductID = tbl_products.product_ID
WHERE SKU_ID = #ListGetAt(myPerList, Counter)#
</cfquery>
<cfset myNewProdList = ValueList(rsGetPerSKUS.pro
</cfloop>
<cfoutput>#myNewProdList#<
Try without '' in your list
<cfif LISTCONTAINS(myNewProdList , 1)> ---
I just tested this ...
<cfset myNewProdList = "1,0">
<cfoutput> #ListContains(myNewProdLis t,1)#</cfo utput>
<cfoutput> #ListContains(myNewProdLis t,0)#</cfo utput>
<cfoutput> #ListContains(myNewProdLis t,3)#</cfo utput>
it returns, 1 2 and 0, which is correct, as 1 is the position of 1 in the list, 2 is the position of 0 in the list and 3 does not exists so it returns 0
<cfif LISTCONTAINS(myNewProdList
I just tested this ...
<cfset myNewProdList = "1,0">
<cfoutput> #ListContains(myNewProdLis
<cfoutput> #ListContains(myNewProdLis
<cfoutput> #ListContains(myNewProdLis
it returns, 1 2 and 0, which is correct, as 1 is the position of 1 in the list, 2 is the position of 0 in the list and 3 does not exists so it returns 0
Does the above resolve your issue ? also you should change your condition to
<cfif LISTCONTAINS(myNewProdList , 1) NEQ 0>
<!--- IT IS IN THE LIST --->
<cfelse>
<!--- NOT IN THE LIST --->
</cfif>
<cfif LISTCONTAINS(myNewProdList
<!--- IT IS IN THE LIST --->
<cfelse>
<!--- NOT IN THE LIST --->
</cfif>
ASKER
<cfif listFind(myNewProdList, "1")>
Perishable ITEMS are in the cart.
</cfif>