Coldfusion CFLOOP multiple lists.

Bang-O-Matic
Bang-O-Matic used Ask the Experts™
on
Coldfusion / Mysql

I have a database field "photos" that holds a comma delimited string of photo URL's and feeds a slideshow.   the code below works,  but I also now have a field "thumbs" that contains another comma delimited string of photo URL's. I want to populate these as my thumbnail images instead of just using the large image as I have in the below code snip.  How would I accomplish this? Any help appreciated.


<!---this code works but doesnt uses the main photo as a thumbnail--->

<cfloop list="#photos#" index="ListItem" delimiters=",">
	
     <cfoutput>
	 <a class="rsImg" data-rsh="500" data-rsBigImg="#trim(ListFirst(ListItem))#" href="#trim(ListFirst(ListItem))#" alt="#details.AddressNumber# #details.AddressStreet#, #details.City#, #details.State# #details.zipcode#">
		<img width="96" height="72" class="rsTmb" src="#ListItem#" /></a> 
	</cfoutput>

</cfloop>




<!---this code doesnt work--->

<cfloop list="#photos#" index="ListItem" delimiters=",">
<cfloop list="#thumbs#" index="ListItem2" delimiters=",">
	

	 <a class="rsImg" data-rsh="500" data-rsBigImg="#trim(ListFirst(ListItem))#" href="#trim(ListFirst(ListItem))#" alt="#details.AddressNumber# #details.AddressStreet#, #details.City#, #details.State# #details.zipcode#">
		<img width="96" height="72" class="rsTmb" src="#ListItem2#" /></a> 

	</cfloop>
	</cfloop>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2015
Commented:
Assuming there's a 1-to-1 relationship between the list of large image url's and the list of thumbnail url's,  you don't need a double list loop. Instead, use a "from/to" loop and use the position to grab the desired elements. (Also, converting the lists to arrays makes for easier code IMO ;-)

<!--- both lists MUST be the same size or loop may error --->
<!--- convert lists to arrays -->
<cfset photoArray = listToArray(photos, ",")>
<cfset thumbArray = listToArray(thumbs, ",")>

<cfloop from="1" to="#arrayLen(photoArray)#" index="pos">

     <!--- extract current set of big/thumb image --->
    <cfset bigImage = photoArray[pos]>
    <cfset thumbImage = thumbArray[pos]>

	<!--- 
		create link with big image and thumbnail variables here 
	--->	
</cfloop>

Open in new window

Author

Commented:
Works perfect! As always, Thanks for your help!
Most Valuable Expert 2015

Commented:
Anytime. Glad it helped :-)

(As noted in the comments, obviously it's assumed the lists are the same size.  If it's possible they may not be, add some error handling).

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial