We help IT Professionals succeed at work.

removing items from list using ListDeleteAt...

cath
cath asked
on
Medium Priority
688 Views
Last Modified: 2013-12-24
hi.
I'm having trouble removing items from a list using ListDeleteAt.

I have a list of items held in a session variable (session.choices), then I want to remove some so I output the list with a checkbox next to each record (name="id" value="#id#") and on the following page do this:

// this to remove form.id item(s) from the list

<cfif (len(form.id) GT 0)>
<cfloop list=#FORM.id# index=id>
<cfset sessionchoices = ListDeleteAt(session.choices,
ListFind(session.choices, id))>
</cfloop>
</cfif>

//this to get the fullname of the ones which were deleted, for checking purposes

<cfquery datasource="#dsn#" name="getDeleted">
SELECT fullname
FROM venue_information
WHERE id IN (#form.id#)
ORDER BY fullname
</cfquery>

// this to get fullnames of the items left

<cfquery datasource="#dsn#" name="getRemaining">
SELECT fullname
FROM venue_information
WHERE id IN (#session.choices#)
ORDER BY fullname
</cfquery>

<body...>

<p><b>These are the ones you just deleted:</b><br>
<cfoutput query="getDeleted">#fullname#<br></cfoutput></p>

<p><b>And this is what's left:</b><br>
<br><br>
<cfoutput query="getRemaining">#fullname#<br></cfoutput></p>

however, the output I get is this:

"These are the ones you just deleted:
Brighton Centre
Brighton Dome

And this is what's left:

Brighton Centre
Brighton Dome
Gardner Arts Centre
Georges House Gallery
Komedia
Metropole Galleries"

notice how the onews which are meant to be deleted are still contained in session.choices...

anyone any ideas? I don't get any error message or anything...

TIA

Ian


Comment
Watch Question

Commented:
try this

<cfif (len(form.id) GT 0)>
<cfloop from="1" to="#listlen("#FORM.id#")#" index=id>
<cfset sessionchoices = ListDeleteAt(session.choices,id)>
</cfloop>
</cfif>

Commented:
OK,
this is working for me....

<cfset session.choices = "1,2,3,4,5,6">
<cfset form.id="1,2,3">

<cfloop from="1" to="#listlen("#FORM.id#")#" index=id>
<cfset session.choices = ListDeleteAt(session.choices,id)>
</cfloop>

<cfoutput>#session.choices#</cfoutput>

Should work for u too...

Jimmy
Commented:
ur mistake was this

<cfset sessionchoices = ListDeleteAt(session.choices,
ListFind(session.choices, id))>


this should have been

<cfset session.choices = ListDeleteAt(session.choices,
ListFind(session.choices, id))>

U missed the "." in sessionchoices  :-)

HTH
Jimmy

Author

Commented:
Doh! that's what comes from staring at it too long ;-)

many thanks - really pleased to have this working.

cheers, Jimmy. You da _man_ ;-)

Ian

Commented:
Glad I could help!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.