Link to home
Start Free TrialLog in
Avatar of 14_east
14_eastFlag for United States of America

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...
Avatar of 14_east
14_east
Flag of United States of America image

ASKER

THIS did nto work...

<cfif listFind(myNewProdList, "1")>
    Perishable ITEMS are in the cart.
 </cfif>
Avatar of 14_east

ASKER

NOR does this:

<cfif find("1",myNewProdList)>Perishable ITEMS are in the cart.</cfif>
Avatar of 14_east

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?


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

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
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>
Avatar of 14_east

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
ASKER CERTIFIED SOLUTION
Avatar of Brijesh Chauhan
Brijesh Chauhan
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 14_east

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