Dropdown with array value?

esl 19 Dec, 2014
Is it possible to load a dropdown select with the value of field with array values?

example:
I have a this dataarray
Array
(
    [option] => com_chronoforms5
    [chronoform] => timeselect
    [Group] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [created] => 2014-12-18 17:08:15
                    [department] => Sales
                    [interviewer] => Bob
                    [interviewdate] => 12/31/2014
                    [time] => 9:00 AM,9:15 AM,9:20 AM,10:00 AM,4:00 PM,4:15 PM
                )

        )

    [time] => 
)


I want the "time" dropdown to show like this:
9AM
10AM
11AM
12PM



once selected it will push the results back to the database as
                    [department] => Sales
                    [interviewer] => Bob
                    [interviewdate] => 12/31/2014
                    [time_selected] => 9:00 AM

Is this too complicated? I have the drop down already pulling in the values but right now it shows
9AM,10AM,11AM as the text and value of the dropdown....Thanks in advance!
GreyHead 21 Dec, 2014
1 Likes
Hi esl,

Can you look the data up again on the On Submit event? I'm not clear where it comes from?

Or a variant on the code in this FAQ might help??

Bob
esl 23 Dec, 2014
I am basically trying to explode the result of [time] => 9:00 AM,9:15 AM,9:20 AM,10:00 AM,4:00 PM,4:15 PM
for drop down options...

time =
[0] =>9:00AM
[1] =>9:15AM
esl 23 Dec, 2014
[attachment=0]Screen-Shot-2014-12-23-at-5.57.42-AM.jpg[/attachment] Screen shot attached of what the form looks like
esl 23 Dec, 2014
Here is where the time values come from...
[attachment=0]Screen-Shot-2014-12-23-at-6.03.36-AM.jpg[/attachment]
esl 23 Dec, 2014
<?php

if ( !$form->data['Data'] || count($form->data['Data']) < 1 ) {
    // no result was found
    $options[] = 'Please select a category';
} else {
    foreach ( $form->data['Data'] as $d ) {
       foreach(explode(',', $d['time']) as $time){
          $options[]= array($d['interviewdate'] => $time);
       }
    }
}
echo json_encode ($options);
?>



This code is outputting Object in dropdown...
[attachment=0]Screen-Shot-2014-12-23-at-9.47.33-AM.jpg[/attachment]
esl 23 Dec, 2014
I checked that FAQ but it doesnt seem as this is what im trying to accomplish😟
GreyHead 27 Dec, 2014
Hi esl,

Looking at the debug output the data is here:
    [Group] => Array
            [0] => Array
                    [time] => 9:00 AM,9:15 AM,9:20 AM,10:00 AM,4:00 PM,4:15 PM

That is $form->data['Group'][0]['time']

It isn't JSON encode, it's just a text string so this should convert it to an array with code like this in a Custom Code action:
<?php
$time = $form->data['Group'][0]['time'];
$time = explode(',', $time);
$form->data['time'] = array();
foreach ( $time as $t ) {
  $form->data['time'][] = array ( 'text' => $t, 'value' => $t);
}
?>

Then you can use the time as the path and text and value as the keys in the dropdown.

Bob
esl 30 Dec, 2014
Looks like it worked but when I submit the form I get [time] => 0 if I select the first one [time] => 1 if I select the second one etc. Is there a way to "Enable" dynamic data for dropdown 2 once and have it recognize the "text key" an "value key" that is in the Dynamic tab?

When I enable Dynamic Data for dropwdown2 onload I can see the values are correct but th problm is that the values need to be loaded depending on the first dropdown selection.

I need the values and text of the dynamic dropdown to be the same when submitted like [time] => 9AM not [time] => 0

Am i doing it wrong?
GreyHead 31 Dec, 2014
Hi esl,

The code I posted should set both the Value and Text for the options to be the same.

Please drag a Debugger action into the On Submit event, then submit the form and post the debug results here. (Preferably as text, not an image).

Bob
esl 31 Dec, 2014
Answer
Thank you so much for your help, I got it to work with this code below
<?php
$time = $form->data['Data'][0]['time'];
$time = explode(',', $time);
foreach ( $time as $t ) {
$options[$t]= $t;
}
echo json_encode($options);
?>


This gave me exactly what i needed!
This topic is locked and no more replies can be posted.