Currentrow in a cf loop?!

Hi there,

Does anyone know a much better way of doing this. I want to only show a “remove all” icon on the first row of the “cart”…

There isn’t a currentrow attribute… and for some reason I can’t access the i variable…

It’s probably really obvious… can someone help.

I’ve bodged it for now with a simple counter called currentrow.

Does anyone have a more eligant solution?

Thanks


	<cfoutput>
		<cfset currentrow=0>
		<cfloop collection="#session.cart#" item="i">
		<cfset currentrow = currentrow + 1>
		<tr onmouseover="this.style.backgroundColor='##e5e5e5';" onmouseout="this.style.backgroundColor='';">
			<td>#session.cart[i][2]#</td>
			<td>#session.cart[i][3]#</td>
			<td>#session.cart[i][4]#</td>
			<td>#session.cart[i][5]#</td>
			<cfif currentrow eq 1>
     		<td align="center"><a href="#cgi.SCRIPT_NAME#?basket=empty" class="linknobold" onClick="return confirm('Are you sure you want to remove ALL of these items?');"><img src="media/icon_delete_all.gif" alt="delete all" title="remove all" width="16" height="16" hspace="2" vspace="2" border="0"></a></td>
        	<cfelse>
        	<td><img src="media/clearpixel.gif" width="20" height="1" alt=""></td>
      		</cfif>
			<td align="center" valign="middle"><a href="movie.cfm?mode=view&id=#session.cart[i][1]#"><img src="media/icon_view.gif" alt="view" title="view" width="16" height="16" hspace="2" vspace="2" border="0"></a></td>
			<td align="center" valign="middle"><a href="#cgi.SCRIPT_NAME#?basket=remove&barcode=#trim(session.cart[i][5])#" onClick="return confirm('Are you sure you want to remove #trim(session.cart[i][2])#?');"><img src="media/icon_delete.gif" alt="remove" title="remove" width="16" height="16" hspace="2" vspace="2" border="0"></a></td>
			</tr>
		</cfloop>		
	</cfoutput>

well, as far as the existence of a ‘currentrow’ inside of a cfloop…you got the solution. you just need to create a variable (you did) and increment it within the loop (you did).

as far as an overall more elegant solution…i’d assume that session.cart is a structure (by the way you’re using a collection loop)…but you’re referencing it as if it were a 2d array. so that seems a bit…interesting :smiley:

the way i would generally do a cart would be an array of structs. let’s say…to keep it simple, you have an itemID, a color, and a quantity.

you would basically have a struct for each item (itemID) in the cart. so it might look like:

item 1:
itemID: 12345
color: red
quantity: 5

item2:
itemID: 56789
color:mauve
quantity: 3

that would be represented as a 2 element array, with each aray position holding a structure.

to loop over the cart then, you would just do:

<cfloop from="1" to="#arrayLen(session.cart)#" index="i">
     Item: #session.cart[i].itemID# (#session.cart[i].color#)<br />
     Quantity: #session.cart[i].quantity#
</cfloop>

I find that it’s easier to loop over the array than a struct…and using the named keys in the structs makes it easier/more intuitive to reference the specific elements of the items in the cart.

i know it’s not what you asked for…but just thought i’d pass it along :slight_smile:

charlie

If you’re using CFLOOP to loop over a query then you’ll have access to the builtin variables of the query object, namely currentrow and recordcount.

Sadly CFLOOP doesn’t have a builtin incrementable variable.