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...
14_eastAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
14_eastAuthor Commented:
THIS did nto work...

<cfif listFind(myNewProdList, "1")>
    Perishable ITEMS are in the cart.
 </cfif>
0
 
14_eastAuthor Commented:
NOR does this:

<cfif find("1",myNewProdList)>Perishable ITEMS are in the cart.</cfif>
0
 
14_eastAuthor Commented:
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?


<!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>

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Brijesh ChauhanStaff IT EngineerCommented:
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
0
 
Brijesh ChauhanStaff IT EngineerCommented:
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.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>
0
 
14_eastAuthor Commented:
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
0
 
Brijesh ChauhanStaff IT EngineerCommented:
Change you above portion of code to below.. this should give you the correct list...

<cfset myNewProdList = ''>

<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 = listAppend(myNewProdList,rsGetPerSKUS.product_per)>
                   </cfloop>
                   <cfdump var="#myNewProdList#">
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
14_eastAuthor Commented:
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?
0
 
Brijesh ChauhanStaff IT EngineerCommented:
>>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.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)>
               
            </cfloop>

<cfoutput>#myNewProdList#</cfoutput>
0
 
Brijesh ChauhanStaff IT EngineerCommented:
Try without '' in your list

<cfif LISTCONTAINS(myNewProdList, 1)> ---

I just tested this ...

<cfset myNewProdList = "1,0">
<cfoutput> #ListContains(myNewProdList,1)#</cfoutput>
<cfoutput> #ListContains(myNewProdList,0)#</cfoutput>
<cfoutput> #ListContains(myNewProdList,3)#</cfoutput>

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
0
 
Brijesh ChauhanStaff IT EngineerCommented:
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>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.