How to update the maximum cycle length if the body length is shorter?

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?

+3
3

:

<input type="checkbox" name="del_#ItemHasUsers[i]["ID"]#">

:

<input type="checkbox" name="del" value="#ItemHasUsers[i]["ID"]#">


:

<cfloop index="i" from="#ArrayLen(ItemHasUsers)#" to="1" step="-1">
    <cfif ListFind(Form.Del,ItemHasUsers[i].Id)>
        <cfset ArrayDeleteAt(ItemHasUsers,i)/>
    </cfif>
</cfloop>


, :

<cfquery name="ItemHasUsers" dbtype="Query">
    SELECT id , assets
    FROM ItemHasUsers
    WHERE id NOT IN (<cfqueryparam list value="#Form.Del#" cfsqltype="cf_sql_integer"/>)
</cfquery>

!

+4

, . , , , .

<cfset ItemHasUsers = Item.getItemHasUsers() >
<cfloop index="i" from="#arrayLen(ItemHasUsers)#" to="1" step="-1">
 <cfif ItemHasUsers[i]["ID"] eq Id>
  <cfset arrayDeleteAt(ItemHasUsers,i) >
 </cfif>
</cfloop>
+9

Jayson , , .

- . , .

<cfset counter=1 />
<cfloop condition="#counter LTE arrayLen(myArray)#">
    <cfif iNeedToDelete>
        <cfset arrayDeleteAt(myArray,counter) />
    <cfelse>
        <cfset counter=counter+1 />
    </cfif>
</cfloop>

, , .

+1

Source: https://habr.com/ru/post/1712016/


All Articles