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 that I put in a table and that are manipulated using 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>
Depends on my input. I want to update my database. I am currently going through a form structure to clear the user I want to delete. It looks ugly, but I don't know a better way -> watch 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 only works if I checked the last item in my input form for deletion. If I check any other, I get the following error:
An element was not found in position 3 of dimension 1 of the variable array "ITEMHASUSERS".
Ok, arrayDeleteAt resizes the array and automatically removes spaces. How to update the loop length for the next iteration?