Second interesting part was to generate dropdown like field with dynamic option list, which depends on responces to other field. I used it to register participants to 3rd part events that were being registered at the same time.
Here it goes:
1. you have some form (e.g. FORM_1) with at least one text filed in it with id lets say XXX. Responces to this field will generate option list in new form (FORM_2).
2. create UDT "
data_from_other_form". It return an 2D array. values can be accessed
$array[resp_id][field_id]
Code: Select all
/*$params['field_ids']*/ /*mandatory, comma separated field_id list, spaces will be trimmed*/
/*$params['assign']*/ /*mandatory*/
$assign = FALSE;
if (!empty($params['field_ids']) && !empty($params['assign'])){
$db = cmsms()->GetDb();
$smarty = cmsms()->GetSmarty();
$field_ids = explode(',',$params['field_ids']);
$query = 'SELECT resp_id, field_id, value FROM '.cms_db_prefix().'module_fb_resp_val WHERE ';
foreach($field_ids as $key=>$field){
$field_ids[$key] = trim($field);
$query .= " field_id = ? or ";
}
$query = rtrim( $query, 'o r');
$query .= ' ORDER BY resp_id,field_id;' ;
$dbresult = $db->Execute($query,$field_ids);
$assign = $oneset = array();
$prev_resp_id = $temp_resp_id = $field = ' ';/* to make sure variables are initialized */
while ($dbresult && ($row = $dbresult->FetchRow())){
$value = $row['value'];
if($temp_resp_id != $row['resp_id']){
if($temp_resp_id != ' ') {
$prev_resp_id = $temp_resp_id;
$assign[$prev_resp_id] = $oneset;
}
$temp_resp_id = $row['resp_id'];
$oneset = array();
}
$oneset[$row['field_id']] = $value;
}
if ($temp_resp_id != ' '){
$assign[$temp_resp_id] = $oneset;
}
else{
$assign = FALSE;
}
}
$smarty->assign($params['assign'], $assign);
return;
3. In FORM_2 create "Static text" field listed below with smarty enabled. It will act as dummy dropdown list
.
"m9d767fbrp__" can be copied from any other field in FORM_2. And digits are id of this field (344 in my example). In an example i combine one option of 5 fields from FORM_1.
EDIT: {$entry->input_id} == m9d767fbrp__344. No need to guess field name and id.
Code: Select all
<select name="m9d767fbrp__344" id="m9d767fbrp__344">
{data_from_other_form assign='temp' field_ids='231,236,237,235,248'}
{if $temp}
{foreach from=$temp item=item}
<option value="{$item[231]},{$item[236]},{$item[237]},{$item[235]},{$item[248]} " >{$item[231]}, {$item[235]}</option>
{/foreach}
{/if}
<option value="Select one" selected="selected">Select one</option>
</select>
You can collect options from more forms - repeat part with coresponding field_ids "{data_from.............{/if}" as many times as there are different forms you want to include.
4. To save value selected in dummy list you have to create "Computed Field". In value to compute field write variable representing dummy field (in my case it was "$fld_344"). And select to interpret it as string.
Thats about it, you should have dynamic option list.