Hello! How should I use Dynamic options loading? After enabling, where do I write the code to retrieve the options and where do I set the possible values?
Forums
Dynamic options loading in Select Box element
Hi ciovo,
The option value & text should be preloaded into the $form->data array before the Show HTML action executes. The format is a two dimensional array like this
The key values e.g. 'text' and 'value' here can be any alphanumberic string and should be identified by entering them in the two boxes on the 'Dynmaic Data' tab.
The options array name e.g. 'options' can be a single string, identified in the Data Path box or a nested array in which case the path is entered with dots so ['articles']['options'] is entered as articles.options
The data source can be from a database table (e.g. from a DB Multi Record Loader action); or calculated in PHP e.g. for a range of years; or read from a file e.g. for a list of countries or states.
Bob
PS My Custom Select [GH] action (a paid action) comes with a range of pre-made and editable option sets using this feature. You can see it working here.
The option value & text should be preloaded into the $form->data array before the Show HTML action executes. The format is a two dimensional array like this
$form->data['options'][0] = array ('value' => 99, 'text' =>'aaaa' )
$form->data['options'][1] = array ('value' => 879, 'text' =>'zzzw' )
$form->data['options'][2] = array ('value' => 'yes, 'text' =>'perpendicular' )
$form->data['options'][3] = array ('value' => 'alphabet', 'text' =>9996 )
. . .
The 'text' will be displayed in the form and the values returned when the form is submitted; they may be the same.The key values e.g. 'text' and 'value' here can be any alphanumberic string and should be identified by entering them in the two boxes on the 'Dynmaic Data' tab.
The options array name e.g. 'options' can be a single string, identified in the Data Path box or a nested array in which case the path is entered with dots so ['articles']['options'] is entered as articles.options
The data source can be from a database table (e.g. from a DB Multi Record Loader action); or calculated in PHP e.g. for a range of years; or read from a file e.g. for a list of countries or states.
Bob
PS My Custom Select [GH] action (a paid action) comes with a range of pre-made and editable option sets using this feature. You can see it working here.
Hi ciovo ,
I think that the normal 'Selected' option on the Select element will still work if the default option is static. If it's dynamic that could be trickier but I think it's possible.
Bob
I think that the normal 'Selected' option on the Select element will still work if the default option is static. If it's dynamic that could be trickier but I think it's possible.
Bob
Hello again,
I successfully built a simple form using Dynamic options loading but the same code doesn't work if the forms uses DB Record Loader.
I tried to add the custom code before or after the Load, all before Show HTML, but I get the same error: "Fatal error: Call to a member function get_array_value() on a non-object in C:\xampp\htdocs\components\com_chronoforms\libraries\includes\data_republish.php(24) : eval()'d code on line 40".
This is the code I added in the code of the Custom form to show the options:
The custom code to get the options is:
What's wrong?!
Thank you
I successfully built a simple form using Dynamic options loading but the same code doesn't work if the forms uses DB Record Loader.
I tried to add the custom code before or after the Load, all before Show HTML, but I get the same error: "Fatal error: Call to a member function get_array_value() on a non-object in C:\xampp\htdocs\components\com_chronoforms\libraries\includes\data_republish.php(24) : eval()'d code on line 40".
This is the code I added in the code of the Custom form to show the options:
<select size="1" label_over="0" hide_label="0" style="width:236px" class="validate['required']" title="" type="select" name="categoria">
<option value="">Choose category</option>
<?php
$options_data = $form->get_array_value($form->data, explode(".", "options"));
if(!is_null($options_data) && is_array($options_data)){
foreach($options_data as $option_data){
if(isset($option_data["value"]) && isset($option_data["text"])){
echo '<option value="'.$option_data["value"].'"'.(in_array($option_data["value"], array (
0 => '',
)) ? ' selected="selected"' : "").">".$option_data["text"]."</option>";
}
}
}
?>
</select>
The custom code to get the options is:
<?php
$options = array();
$db =& JFactory::getDBO();
$db->setQuery(
'SELECT alias, title'.
' FROM #__categories'.
' WHERE extension = "com_myextension"'.
' ORDER BY title'
);
$categories = $db->loadAssocList();
foreach ($categories as $category)
{
$options[] = array('value' => $category['alias'], 'text' => $category['title']);
}
$form->data['options'] = $options;
?>
What's wrong?!
Thank you
Hi ciovo,
The error means that the $form var is not defined, where did you add the first chunk of code exactly ? and do you have the latest CF V4 RC3.3 ?
Regards,
Max
The error means that the $form var is not defined, where did you add the first chunk of code exactly ? and do you have the latest CF V4 RC3.3 ?
Regards,
Max
Hi ciovo,
And this line looks very odd to me:
But, in the current release or ChronoForms this is mostly done for you. Use a DB Multi-Record Loader action to get the values of `alias` and `title` from the table with a model ID of say category; then use the Dynamic Data tab of a Drop Down element to link the data to the Drop Down.
Bob
And this line looks very odd to me:
$options_data = $form->get_array_value($form->data, explode(".", "options"));
You've aleady defined $form->data['options'] to be the options array so you can use that.But, in the current release or ChronoForms this is mostly done for you. Use a DB Multi-Record Loader action to get the values of `alias` and `title` from the table with a model ID of say category; then use the Dynamic Data tab of a Drop Down element to link the data to the Drop Down.
Bob
I'm using CF V4 RC3.21 and the first part of code is in edit form -> code (Form Type is Custom).
Hi ciovo,
Please upgrade (by downloading the latest version and installing it OVER the one you have) and please check Bob's post because he explained the default method to load the select box with some table's data.
you may also try to use:
Let us know if you can't get it to work.
Regards,
Max
Please upgrade (by downloading the latest version and installing it OVER the one you have) and please check Bob's post because he explained the default method to load the select box with some table's data.
you may also try to use:
<?php print_r2($form); ?>
to make sure that the form object is loaded correctly, it should be though.Let us know if you can't get it to work.
Regards,
Max
I tried using the GreyHead's way, but the result is still the same because $form is null.
Hi ciovo,
The method explained by Bob should work fine and I have already used it many times here with different tables.
Also I agree that the $form var is null when used it in:
but try
and it should work fine, also please make sure that you have V4 RC3.3 installed before testing.
Regards,
Max
The method explained by Bob should work fine and I have already used it many times here with different tables.
Also I agree that the $form var is null when used it in:
<?php print_r2($form); ?>
but try
<?php print_r2($form->data); ?>
and it should work fine, also please make sure that you have V4 RC3.3 installed before testing.
Regards,
Max
Hi Max and thank you for your patience.
I upgraded to V4 RC3.3 but still not working.
If I use a Debugger Action I can correctly see all the options, and also if I print_r($form->data) in a Custom Code on Load action.
But I have to access $form->data in the HTML code of the form because it's the only place (as far as I know) where I can add the <option> dinamically loaded. In this part of the code, $form->data is null, so I can't use it.
This behaviour only happens in the edit form because in the create form (the code is the same except for data load) $form->data is not null: that's the strange thing.
I upgraded to V4 RC3.3 but still not working.
If I use a Debugger Action I can correctly see all the options, and also if I print_r($form->data) in a Custom Code on Load action.
But I have to access $form->data in the HTML code of the form because it's the only place (as far as I know) where I can add the <option> dinamically loaded. In this part of the code, $form->data is null, so I can't use it.
This behaviour only happens in the edit form because in the create form (the code is the same except for data load) $form->data is not null: that's the strange thing.
Hi ciovo,
Please see my earlier post and use that with the Dynamic Data tab in a Drop-down element in your form.
Bob
I have to access $form->data in the HTML code of the form because it's the only place (as far as I know) where I can add the <option> dinamically loaded.
That's not how to do it!! At least not the way we are describing here.Please see my earlier post and use that with the Dynamic Data tab in a Drop-down element in your form.
Bob
Hi ciovo,
I have just met the same situation!!!🙂
But I couldn't use teh wizard solution since I'm working on a custom code form.
After debugging, I found that its the "DB Record loader" causing the problem, disable the "Load Fields/Curly replacer" settings and that should fix it, I have removed them from the action.
The "Show html" action does this task.
Regards,
Max
I have just met the same situation!!!🙂
But I couldn't use teh wizard solution since I'm working on a custom code form.
After debugging, I found that its the "DB Record loader" causing the problem, disable the "Load Fields/Curly replacer" settings and that should fix it, I have removed them from the action.
The "Show html" action does this task.
Regards,
Max
This topic is locked and no more replies can be posted.