Load values from database in form

Discuss everything related to FormBuilder. All versions. Any questions related to the FormBuilder developing or using should be posted to this forum.

Moderators: alexandrleonenko, goas

lapuella
Junior
Junior
Posts: 11
Joined: Sep 12th, '13, 18:08

Load values from database in form

Postby lapuella » Sep 13th, '13, 10:38

Is it possible to load values from a database into a select or checkbox field?
Is it possible to use your own html or php code between the fields in the form?

goas
Newbie
Newbie
Posts: 9
Joined: Apr 23rd, '12, 11:26

Re: Load values from database in form

Postby goas » Sep 16th, '13, 12:40

1. There is no way to do it directly, form builder does not work with data base. But you can get the values from db, form an array which looks like ($key => $value, ...) and pass it to form builder.
2. If you make a form with FB_Form class, then no, you can add footer or header but not data between the fields. You can, of course, make your own html form and then create fields directly like that

Code: Select all

$checkbox = new FB_Checkbox('name', 'label', true);
$checkbox->GetHtml();

And you will get a field html for you form. Then you can add anything in between them. But then you will have to make the rest manually too (run validations, form result letter, show success or failure messages and errors etc ).

lapuella
Junior
Junior
Posts: 11
Joined: Sep 12th, '13, 18:08

Re: Load values from database in form

Postby lapuella » Sep 23rd, '13, 18:47

Thank you very much for answering my question. I don't understand how I can pass the array to the form.
Do you have a example how to do that?

I use the following code:

$mysqli = new mysqli("localhost","username","password","database");

$query = "SELECT activiteit, datum FROM activiteiten_jongeren WHERE reserveringen <= '80'";
$result = $mysqli->query($query);

while($row = $result->fetch_array()) {
$rows[] = $row;

$form->AddSingleSelect('Datum', 'Datum', array($row['datum'] => $row['activiteit'],))
->SetNullValue('Selecteer een datum')
->IsRequired(1)
->SetValue(1);
}

$result->free();
$mysqli->close();

In my example only the last result from the database shows in the form.

Thank you for any help you can provide in this situation.

goas
Newbie
Newbie
Posts: 9
Joined: Apr 23rd, '12, 11:26

Re: Load values from database in form

Postby goas » Sep 25th, '13, 16:49

You should do something like that:

Code: Select all

while($row = $result->fetch_array()) {
    $rows[$row['datum']] = $row[$row['activiteit']];
}

Then your results from database will be stored in array $rows. This array will look like: array($datum1=>$activiteit1, $datum2=>$activiteit2 ), that is datum is going to be a value and activiteit is an option. To understand how select field is formed have a look at this html reference http://www.w3schools.com/tags/tag_select.asp.
Now let's create a form field (note that you do it outside the while).

Code: Select all

$form->AddSingleSelect('Datum', 'Datum', $rows)
    ->SetNullValue('Selecteer een datum')
    ->IsRequired(1)
    ->SetValue(1);

Then you will get a select field, where user will see a list of activiteit, and on form submit you will get a datum;
if you want a datum array with datum seen to user and sent to you likewise, you should make array as follows

Code: Select all

while($row = $result->fetch_array()) {
    $rows[$row['datum']] = $row['datum'];
}

Then if you want user to see some specific datum $datum from your array, you can set ->SetValue($rows[$datum]);

lapuella
Junior
Junior
Posts: 11
Joined: Sep 12th, '13, 18:08

Re: Load values from database in form

Postby lapuella » Sep 26th, '13, 00:35

Great, it works now! Thank you very much!

This did not work correct, the 'datum' (value) was showing, but 'activiteit' (option) not.

while($row = $result->fetch_array()) {
$rows[$row['datum']] = $row[$row['activiteit']];
}

This works correct:

while($row = $result->fetch_array()) {
$rows[$row['datum']] = $row['activiteit'];
}

Finaly i combined datum and activiteit for option like this:

while($row = $result->fetch_array()) {
$rows[$row['datum']] = $row['activiteit']." on ".date('d-m-Y', strtotime($row['datum']));
}

There was only one problem left, some activities take place on the same date (datum) and therefore not all results were showing, only 4 of 8.
I changed date to datetime (to prevent double rows) and now it works correct!

Excuse me for my bad english.

Thanks again for your help! :)

goas
Newbie
Newbie
Posts: 9
Joined: Apr 23rd, '12, 11:26

Re: Load values from database in form

Postby goas » Sep 29th, '13, 11:38

You are welcome!


Return to “ApPHP FormBuilder {developers/users}”