I have the following array.
<cfset ItemHasUsers = arrayNew(1)>
<cfloop query="qReadData">
<cfset ItemHasUsers[qReadData.currentrow]["ID"] = qReadData.ID >
<cfset ItemHasUsers[qReadData.currentrow]["Asset"] = qReadData.COUNTOFITEMS >
</cfloop>
I get some records from my database which i put into a table and which manipulate through a form.
<form action="same-site.cfm method="post">
<table>
<tr>
<th>ID</th>
<th>Asset</th>
<th>Delete</th>
<tr>
<cfset ItemHasUsers = Item.getItemHasUsers() >
<cfoutput>
<cfloop index="i" from="1" to="#arrayLen(ItemHasUsers)#">
<td>#ItemHasUsers[i]["ID"]#</td>
<td><input type="text" name="upd_#ItemHasUsers[i]["ID"]#" maxlength="6" size="6" value="#ItemHasUsers[i]["Asset"]#"></td>
<td><input type="checkbox" name="del_#ItemHasUsers[i]["ID"]#"></td>
</tr>
</cfloop>
</cfouput>
</table>
<input type="submit" value="OK">
</form>
Dependend on my input i want to update my database. Currently i loop through the form struct to clear the User i want to delete. Looks ugly, but i do not know a better way -> watch the beginner tag ;)
<cfset ItemHasUsers = Item.getItemHasUsers() >
<cfloop collection="#form#" item="key">
<cfif left(key,len("DEL_")) eq ("DEL_")>
<cfset Id = listLast(key,"_") >
<cfloop index="i" from="1" to="#arrayLen(ItemHasUsers)#">
<cfif ItemHasUsers[i]["ID"] eq Id>
<cfset structClear(ItemHasUsers[i]) >
</cfif>
</cfloop>
</cfif>
</cfloop>
<cfset ItemHasUsers = Item.getItemHasUsers() >
<cfloop index="i" from="1" to="#arrayLen(ItemHasUsers)#">
<cfif ItemHasUsers[i]["ID"] eq Id>
<cfset arrayDeleteAt(ItemHasUsers,i) >
</cfif>
</cfloop>
This works only if i check the last element in my input form for deletion. If i check any other i get the following error
The element at position 3 of dimension 1, of array variable "ITEMHASUSERS," cannot be found.
Ok, arrayDeleteAt resizes the array and deletes the gaps automatically. How do i update the loop length for the next iteration?