?
Solved

Check for a certain value in a list

Posted on 2011-04-18
11
Medium Priority
?
293 Views
Last Modified: 2012-05-11
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...
0
Comment
Question by:14_east
  • 6
  • 5
11 Comments
 

Author Comment

by:14_east
ID: 35420433
THIS did nto work...

<cfif listFind(myNewProdList, "1")>
    Perishable ITEMS are in the cart.
 </cfif>
0
 

Author Comment

by:14_east
ID: 35420439
NOR does this:

<cfif find("1",myNewProdList)>Perishable ITEMS are in the cart.</cfif>
0
 

Author Comment

by:14_east
ID: 35420791
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35421272
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
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35421281
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
 

Author Comment

by:14_east
ID: 35421294
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
 
LVL 11

Accepted Solution

by:
Brijesh Chauhan earned 500 total points
ID: 35421302
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
 

Author Comment

by:14_east
ID: 35421314
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
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35421324
>>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
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35421337
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
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35421407
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Integration Management Part 2
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question