Jump to content

How to access a Form Builder field entry?


Daniele
 Share

Recommended Posts

Hi there!

I just bought the formbuilder module (which is really great, thanks Ryan!),
I was wondering if it was possible to access in any template directly the entries without using the page creation option.

In other words is it possible to have something like this:

$forms->formtitle->fieldtitle

I searched for it but I have to say I'm kind of feeling that there is missing a little technical introduction in the readmefile provied at the download.

Thanks for the attention, and again, thanks Ryan for having build this, it's really useful for us and our clients!

  • Like 1
Link to comment
Share on other sites

Grüezi Daniele, you will get access to the form builder support forum once Ryan sees this.

I'm not sure what you need to archive, but since the entries are stored as json you can't search for entry fields. If you want to use API to find entries you need to store them as pages.

You can access the entries in DB through $forms

foreach($forms->get("contact-form")->entries->find() as $e){
    echo "<p>{$e['e_mail']}</p>";
}
  • Like 4
Link to comment
Share on other sites

Hi!

just two little more question with this code:

1. How can I get the url of a file?

if my field is named "file" for example it just shows "array". And I'm not shure how to select infos from an array (sorry php beginner)

2. How can I get the submission date?

In the Entires panel of the formbuilder I see that there is a submission date that is recorded how can I get this information too (still using the same code?)?

Thanks a lot :-)

Link to comment
Share on other sites

I haven't used form builder with files upload yet, and I can't find any "file" field in formbuilder 0.2.0, maybe I'm missing something.

An php array is accessed array[key] same as in Javascript or other languages.

You can get the created like this:

foreach($forms->get("contact-form")->entries->find() as $e){
    echo "<p>{$e['e_mail']} - {$e['created']}</p>";
}

If your 'file' field is itself an array you could try using 

echo "<pre>";
print_r($e['file']);
echo "</pre>";
 

and you'll see the array printed out recursively and you see all the entries and its keys and values.

Once you know the key you access it for example:

echo $e['file']['filename'];
  • Like 2
Link to comment
Share on other sites

I haven't used form builder with files upload yet, and I can't find any "file" field in formbuilder 0.2.0, maybe I'm missing something.

You have to enable it in your Form Builder module settings: InputfieldFormBuilderFile. On an existing FormBuilder installation, you may have to install the module before you can enable it. 

1. How can I get the url of a file?

The $e['file'] should be an array of filenames. In the case of a single file, it would just be an array of 1 file. The actual file URL is protected from direct access, so you have to ask Form Builder to generate one for you. It's not the prettiest in terms of API access, but I think this should work: 

$form = $forms->get('contact-form'); 
foreach($form->entries()->find() as $entry) {
  foreach($entry['file'] as $filename) {
    $path = $form->entries()->getFilesPath($entry['id']); 
    $url = $forms->getFileURL($form->id, $entry['id'], $path . basename($filename)); 
    echo $url . "<br />";
  }
}

Regarding submission date, Soma is correct that it would be contained in $entry['created']; as a unix timestamp. 

  • Like 2
Link to comment
Share on other sites

  • 3 weeks later...
  • 1 month later...

Regarding submission date, Soma is correct that it would be contained in $entry['created']; as a unix timestamp.

Is there a way in PW's Form Builder to convert hours from am/pm to proper 24h system? Now I get funny output, like this for example: 13:30 pm.

BTW: while messing around with module I made a Form Builder themes preview (v. 0.2.2) to avoid switching and saving settings. Maybe someone who wants to use the preset form styles will find it usefull. Here is the link to screen shots: http://bit.ly/123dcpZ

  • Like 1
Link to comment
Share on other sites

Is there a way in PW's Form Builder to convert hours from am/pm to proper 24h system? Now I get funny output, like this for example: 13:30 pm.

All date/time formats are configurable. Where are you seeing 13:30 pm? I can tell you where to change it. 

BTW: while messing around with module I made a Form Builder themes preview (v. 0.2.2) to avoid switching and saving settings. Maybe someone who wants to use the preset form styles will find it usefull. Here is the link to screen shots: http://bit.ly/123dcpZ

Looks cool–I like it! 

Link to comment
Share on other sites

Ryan thanks for your reply, two more things though:

1.

After changing timezone in config file I'm still getting wrong timestamp of DB entrties through Form Builder - its 2 hours late. Perhapse I'm doing something wrong - I just changed original zimezone America/New_York to Poland/Warsaw. I also tried other cities in the same timezone like Germany/Berlin - no luck.

2.

All date/time formats are configurable. Where are you seeing 13:30 pm? I can tell you where to change it.

I see it in directly in phpmyadmin and as an output on the webpage, too. If you could also advise how to convert time format to 24h it would be great.

Thank you!

Link to comment
Share on other sites

After changing timezone in config file I'm still getting wrong timestamp of DB entrties through Form Builder - its 2 hours late. Perhapse I'm doing something wrong - I just changed original zimezone America/New_York to Poland/Warsaw. I also tried other cities in the same timezone like Germany/Berlin - no luck.

The timezone you use has to be one predefined in PHP. See here for an official list: http://www.php.net/manual/en/timezones.php

I'm thinking you want "Europe/Berlin" or "Europe/Warsaw" ?

I see it in directly in phpmyadmin and as an output on the webpage, too. If you could also advise how to convert time format to 24h it would be great.

I'm still not clear about where you are seeing this in ProcessWire? Dates/times are stored as either MySQL DATETIME fields or unix timestamps (integers), none of these are actually stored as formatted dates/times. So if you saw that format in PhpMyAdmin, then that would be because that's how PhpMyAdmin is formatting it, and I'm not sure that's within our ability to control. But output in ProcessWire is within our ability to control, I just need to understand specifically where you are talking about. If it's the front-end of your site, then you can change the output format by editing the field (Setup > Fields > your date field), clicking on "details" and selecting the format that you prefer. 

Link to comment
Share on other sites

The timezone works correctly now - my obvious mistake, thanks for pointing it.

I'm still not clear about where you are seeing this in ProcessWire

It's like this: when someone sends data via the form (created in Form Builder) the data is then inserted into the DB. When I check time of the entry (lets skip the date to simplify things) in phpMyAdmin it shows me for example 13:30 - and everything is OK by now.

But when I generate the data stored in DB as an output on the webpage the time of the entry is: 13:30 pm. To generate the date/time I use:

$entry['created']

So my question was how can I make am/pm not to appear :rolleyes:

To get things more transparent here is the code for generating DB entries, plain and simple:

     <?php
           foreach($forms->get("wall-form")->entries->find() as $e) {
               echo "<div class='post'>";
               echo "<h3>{$e['temat']}</h3>";
               echo "<p><b>Dodane:</b> {$e['created']}</p>";
               echo "<p><b>Autor:</b> {$e['autor']}</p>";
               echo "<p><b>E-mail:</b> {$e['e_mail']}</p>";              
               echo "<p>{$e['wpis']}</p>";
               echo "</div>";
            }
            ?>
  • Like 1
Link to comment
Share on other sites

Thanks, I understand now. If you look in the FormBuilderEntries.php file, you'll see this line:

protected $dateFormat = 'Y/m/d G:i a';

That 'G' is actually supposed to be a lowercase 'g' for 12-hour format, as 'G' is 24-hour format. I will correct this. You can also change it yourself in that file if you'd like, or you can can do this in your foreach loop above:

$e['created'] = date('Y/m/d g:i a', strtotime($e['created'])); 
Link to comment
Share on other sites

The main reason for the date setting being static is that it has to be string-sortable. Many date formats are not string-sortable, so we try to make sure that any shown in the PW admin are sortable among other dates. Not sure how important it is in this case, so I probably could make it configurable, but just wanted to mention the reasoning behind using ISO-8601 style dates in admin situations. 

Link to comment
Share on other sites

am / pm part is most problematic here, localization wise.

I think we need to switch to datatables or other plugin to allow real date sorting in the long run. Or just prepend dates with hidden timestamp.

Link to comment
Share on other sites

Actually, I'm not thinking. We already have date sorting in the Options section (which sorts at the DB level) so the column heading in this case doesn't matter for date sorting... or at least isn't that useful for it. I can make this date format configurable without any concerns. 

Link to comment
Share on other sites

  • 3 years later...

Hey Guys,

 

I'm trying to sort the foreach results from FormBuilder but I can't seem to sort it by a field, only by created.
How would you go about doing so? 

 

This works:
foreach($forms->get("reserveren")->entries->find("sort=created") as $res)

This is what I need but it doesn''t work:
foreach($forms->get("reserveren")->entries->find("sort={$entry['datum']}") as $res)

Thanks guys!

Link to comment
Share on other sites

Try this:

$forms->get("reserveren")->entries->find("id>0")->sort($entry['datum'])

 

1 hour ago, Gideon So said:

Sort or search by field other than system field like id, created, etc is not possible because all the custom form fields are saved as one array in the database.

It should however work if one is loading all entries beforehand and doing the sorting in memory.

  • Like 1
Link to comment
Share on other sites

Thanks LostKobrakai!

But sadly to no avail:

this:
foreach($forms->get("reserveren")->entries->find("id>0")->sort($entry['datum']) as $res){

Return this:
Error: Call to a member function sort() on array (line 210 of /public/sites/www.cafepool.nl/site/templates/agenda.php) 
This error message was shown because you are logged in as a Superuser. Error has been logged.

 

Gr,
Bram

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...