I'd actually rewrite it to use form builder or ad_form
but if you want to just dynamically build the query part of ad_page_contract
see
https://openacs.org/api-doc/proc-view?proc=bug%5ftracker%3a%3aget%5fpage%5fvariables
as an example of a procedure that does that.
You can build the list and return it from a procedure in the ad_page_contract call as is done in bug-tracker.