Buy Now
Sign in

How to add a custom array as datasource of dropdown

Collie-IT , August 01 2020, 11:02
C
Collie-IT 46
August 01 2020, 11:02 #393510
Dear Support,
in Version 5 and 6 chronoforms was able to set a custom array as source of dropdown list.
like
v5
$form->data['dropdown'] = array(...);
v6
$this->data['dropdown'] = array(...);
How is this behavior posible in Chronoforms 7?
Best regards
Collie-IT
admin 29
August 03 2020, 00:46 #393522
use the options or multiline settings for a list of defined options!
If you want a dynamic list of options, from a PHP action for example, then use {var:php_name} in the "data source" of the "Dynamic options"
Max
If your main question got answered then please mark the answer using the button!
Did you try the new ChronoForms7 ? if yes then please send us your comments!!
C
Collie-IT 46
August 10 2020, 11:48 #393607
Dear Max,
My problem is that I have informations in 3 tables stored and need a custom SQL read (Wordpress, user, user_blocked and user_meta).The user_meta table writes values to a user_id like this
id user_id valuename value
1 3 name Test
1 3 vorname Teo
1 2 name otto
The problem is that I can't build the querry via data read.
My Solutuion is to generate with PHP an string and save it as variable
$args = array(
'exclude' =>$blockedUser,
'role' => 'Berater',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$user = get_users( $args );

$counselors ="";
foreach($user as $key =>$councelor){

$counselors = $counselors . "\n" .$councelor->data->ID . " = " . $councelor->data->display_name;
}

$this->set("counselors", $counselors);
Use the multiline setting.
[img=13215
  • 2020-08-10_13h43_20.png[/img]
  • and then I have modifide chronoforms7\chronog3\admin\extensions\chronofc\behaviors\views\field_multiline_options\field_multiline_options_output.php to following
    <?php
    if(!empty($unit['multiline_options'])){

    $gtext=$unit['multiline_options'];
    if( substr($unit['multiline_options'], 0, 5) === "{var:"){
    $gtext = $this->controller->Parser->parse($unit['multiline_options']);
    }


    $lines = explode("\n", $gtext);

    $lines = array_map('trim', $lines);
    $lines = array_filter($lines, 'strlen');

    foreach($lines as $line){
    $option = ;
    $odata = explode('=', $line);

    $option = [
    'value' => $odata[0
  • ,
    'content' => $this->controller->Parser->parse($odata[1] ?? $odata[0]),
    ];

    $unit['foptions']
  • = $option;
    }
    }
    Feel free to use this solution
    Attachments
    2020-08-10_13h43_20.png
    2020-08-10_13h43_20.png
    (33.51 KiB)
    20 Downloads/Views
    admin 29
    August 10 2020, 15:09 #393610
    you better return an array from the PHP and use the "Dynamic options" instead, write {var:counselors} in the "Data sources", and set the paths to the value and text with the {var:path_to_value_in_the_array_item}
    so you should return an array in this structure:
    $options = array(['value' => 1, 'text' => 11], ['value' => 2, 'text' => 22]);
    then you would use {var:value} and {var:text} for the option data!
    please confirm this solution works for you ?
    Max
    If your main question got answered then please mark the answer using the button!
    Did you try the new ChronoForms7 ? if yes then please send us your comments!!
    C
    Collie-IT 46
    August 10 2020, 16:53 #393617
    The problem was that {var:counselors} not worked. I tryed to make a data Read first know the $unit['data_source'] stick with 22 and the
    {var:counselors} will not be saved.
    admin 29
    August 11 2020, 01:17 #393621
    so my solution did not work for you ?
    Max
    If your main question got answered then please mark the answer using the button!
    Did you try the new ChronoForms7 ? if yes then please send us your comments!!
    C
    Collie-IT 46
    August 11 2020, 04:52 #393626
    Yes out of the reason you solution not worked.