Forum OpenACS Q&A: How to manage arrays in ad_form?

Collapse
Posted by Miguel Marin on
Hi, Im having troubles with the manage of arrays in ad_form.
Im declaring elements with a for like this

set elements [list]
for {set i 1} {$i <= 5} {incr i} {
    set element [list bullet.$j:text(text),optional \
                {label "Bullet $j"} \
                            {html { size 60 }} \
                            {before_html "<ul><li>" } \
                            {after_html "</ul>"} \
                            {value $def}]
    lappend elements [list $element]
}

and that works just fine but, when I try to make a list of the bullets to send them to a procedure that inserts the list in the data base, I can't.

Im trying to do something like this

set bullet_list [list]
    for {set i 1} {$i <= $array_max} {incr i} {
        if { ![empty_string_p $bullet($i) ] } {
            lappend bullet_list $bullet($i)
      }
}

and the error I get is:

can't read "bullet(1)": no such variable
    while executing
"empty_string_p $bullet($i) "

Any help, from someone

Collapse
Posted by Rocael Hernández Rizzardini on
I think, more than array problem is an variable *name* problem, since is not used as array.

You are creating dynamic values for ad_form, then, to get the values of each input, first, you need to know the name of the dynamic input, then you can get the value (using [set $var_name]). Have a look on:
packages/acs-subsite/www/shared/parameters.tcl to see how this is handled.

So you need to know, how many bullets you have created, then in your for:
for {set i 1} {$i <= $j} {incr i} {
  #to get the value:
  set bullet_value [set $bullet.$i]
  if { ![empty_string_p $bullet_value ] } {
            lappend bullet_list $bullet_value
      }
}

Collapse
Posted by Miguel Marin on
Thanks, it works that way. Just a correction, you have to put:

set bullet_value [set bullet.$i] instead of [set $bullet.$i]

for {set i 1} {$i <= $j} {incr i} {
  #to get the value:
  ------> set bullet_value [set $bullet.$i]
  if { ![empty_string_p $bullet_value ] } {
            lappend bullet_list $bullet_value
      }
}