Buy Now
Sign in

How to fill a dropbox from DB?

Alcor , September 07 2015
A
Alcor

Hello.

I need your help, please. I don't know how to fill a dropbox from a database read.

In On Load event the first one is a DB read, at this moment I can read the data. This data is only a number named a40 and this number indicates how options are set in the dropbox named na40. If the data number is 98 the na40's last option is 98 and the previous ones decrement in ten, so the options would be: 0, 10, 20, 30, 40... 90, 98

My problem is that I don't know how to fill the dropbox with these numbers.

GreyHead

Hi Alcor,

You will need to use PHP in a Custom Code action to build the options array and then you can link it to the drop-down using the Dynamic Data tab. Your PHP should create a sub-array in the $form-data array something like this:

				$form->data['options'] = array(
[0] => array( 'value' => 'aaa', 'text' => 'bbb',
[1] => array( 'value' => 'ccc', 'text' => 'ddd',
. . .
);
Then you can use options, value and text as the entries in the dynamic data settings.

Bob

ChronoForms technical support
If you'd like to buy me a coffee or two, thank you very much

A
Alcor

Thanks Bob.

But how to do it for multiple dropboxes in the same form?

A
Alcor

Solved.

I answer myself

In custom code:

				<?php
$form->data['n40'] = array();
if ($form->data['a40'] % 10 != 0) {
$form->data['n40'][($form->data['a40']/10)+1]['v40'] = $form->data['a40'];
$form->data['n40'][($form->data['a40']/10)+1]['d40'] = $form->data['a40'];
}
else if ($form->data['a40'] == 0) {
$form->data['n40'][0]['v40'] = 0;
$form->data['n40'][0]['d40'] = 0;
}
for ( $i = intval($form->data['a40']/10); $i > 0 ;$i--) {
$form->data['n40'][$i]['v40'] = $i*10;
$form->data['n40'][$i]['d40'] = $i*10;
}
}

$form->data['n42'] = array();
if ($form->data['a42'] % 10 != 0) {
$form->data['n42'][($form->data['a42']/10)+1]['v42'] = $form->data['a42'];
$form->data['n42'][($form->data['a42']/10)+1]['d42'] = $form->data['a42'];
}
else if ($form->data['a42'] == 0) {
$form->data['n42'][0]['v42'] = 0;
$form->data['n42'][0]['d42'] = 0;
}
for ( $i = intval($form->data['a42']/10); $i > 0 ;$i--) {
$form->data['n42'][$i]['v42'] = $i*10;
$form->data['n42'][$i]['d42'] = $i*10;
}
}

$form->data['n45'] = array();
if ($form->data['a45'] % 10 != 0) {
$form->data['n45'][($form->data['a45']/10)+1]['v45'] = $form->data['a45'];
$form->data['n45'][($form->data['a45']/10)+1]['d45'] = $form->data['a45'];
}
else if ($form->data['a45'] == 0) {
$form->data['n45'][0]['v45'] = 0;
$form->data['n45'][0]['d45'] = 0;
}
for ( $i = intval($form->data['a45']/10); $i > 0 ;$i--) {
$form->data['n45'][$i]['v45'] = $i*10;
$form->data['n45'][$i]['d45'] = $i*10;
}
}
?>


And in the designer in each dropdown box, Dynamic data tab... Data path: n40, n42 and n45; Value key: v40, v42 and v45; and Data key: d40, d42 and d45