Tag: Views

Problem:

We have a form that we use to collect entries. This is our “database” which has a view to display all of our entries. Not all entries are relevant to all users.

We want the user to be able to select which database entries are relevant to their business and be able to display them on their dashboard page.

Finally, we want the user to be able to go back to update or change their selections and for their previous selections to still be there. 

Solution:

We created a second form with a user id field and a dynamic field (checkboxes) that holds all of the titles of each entry in our database. The user will use this form to make their selections. Now we create a view from that form which we will call “Dynamic Selection”. In the content section of this view we write “[3696 show=id],” // change 3696 to your dynamic field. (Make sure the comma is there after the closing bracket). Now we’ll add the standard “User ID = current_user” filter along with sorting by “Entry Creation Date =  Descending”. Finally, set the “limit = 1”. Now we have a view that has our most recent entry ID for the current user.

After everything is set up correctly, copy the view shortcode and go back to the database view (first form’s view). Here we will add the filter “Entry ID = [display-frm-data id=17347]” // paste your own view shortcode. Now we have a dynamic field being used to filter the databases entries based on the checkboxes that are selected for that current user. 

Now for the final part, we are simply going to take our entry ID shortcode and paste it into the default value of the field in the Dynamic Field. This will make it so our checkboxes are always prepopulated with the user’s last selection.

×Warning: This tutorial was created 2049 days ago. Some of the information may be out of date with more recent versions of Formidable. Please proceed with caution and always perform a backup before adding custom code.

The default shortcode for a view is

[display-frm-data id=172 filter=limited]

Here "filter" refers to the WordPress filter function NOT the Formidable Forms filter function.

To stop this behaviour remove or zero the filter term.

[display-frm-data id=172 filter=0] OR [display-frm-data id=172]
×Warning: This tutorial was created 2109 days ago. Some of the information may be out of date with more recent versions of Formidable. Please proceed with caution and always perform a backup before adding custom code.

Create A Staff Timesheet Using Formidable Forms

This article details how to create a basic staff timesheet using Formidable Forms, ideal for home or remote workers or anyone needing to record dates and times in an online form. This article assumes that you are using the latest version of Formidable Pro and know how to add PHP snippets to your site. Along with a basic understanding of HTML and CSS.

This timesheet application consists of three key parts:

  1. A form for entering the dates and times worked
  2. A filter form for filtering the dates viewable (optional)
  3. A view for displaying the information

We built this application using the following steps:

Step 1: Build the initial form

Create your form for users to log their times and dates worked. Include a hidden / admin only field for the time calculation to be inserted. Then add the following PHP snippet to your functions.php file, a custom plugin or the code snippets plugin:

/* Calculate Time Worked */
add_filter('frm_validate_field_entry', 'calculate_time', 11, 3);
function calculate_time($errors, $field, $value){
  if($field->id == 25){ //change 25 to the ID of the hidden or admin only field which will hold the calculation
    	$start = (strtotime($_POST['item_meta'][23])); //change 23 to the ID of the first field
    	$end = (strtotime($_POST['item_meta'][24])); //change 24 to the ID of the second field
$lunch = (strtotime($_POST['item_meta'][25])); //change 25 to the ID of the lunch hour field
    $totaltime = ($end – ($start + $lunch));
    $hours = intval($totaltime / 3600);   
    $seconds_remain = ($totaltime - ($hours * 3600)); 
    $minutes = intval($seconds_remain / 60);
    //$seconds = ($seconds_remain - ($minutes * 60)); Uncomment this line if you want seconds calculated.
    $leading_zero_for_minutes = $minutes < 10 ? '0' : '';
    //$leading_zero_for_seconds = $seconds < 10 ? '0' : '';//Uncomment this line if you're including seconds.
    $totaltime = $hours . ':' . $leading_zero_for_minutes . $minutes;	
    //$totaltime = $hours . ':' . $leading_zero_for_minutes . $minutes . ':' . $leading_zero_for_seconds . $seconds;//uncomment this line if you're including seconds
    $value = $_POST['item_meta'][26] = $totaltime; //change 26 to the ID of the hidden or admin only field which will hold the calculation
  }
  return $errors;
}

This code will calculate the number of hours worked for each period and insert the value into a hidden or admin only field within your form. This is a slightly edited version of the original snippet which can be found on the Formidable KB HERE.

Step 2: Build a Search form

This stage is optional if you want to be able to search and filter your results in your view (which we did).Create a new search form with any fields you wish to search on. Our search form is very basic and consists of 2 date fields (start_date and end_date). In the form settings we set the following options:

  1. Select “Do not store entries submitted from this form”
  2. Change the ‘On Submit’ option to ‘Redirect to URL’
  3. In the URL enter www.YourSite.com/YourView/?start_date=[551]&end_date=[552] and change 552 and 553 to the ID’s of your 2 date fields.
Step 3: Building Your View

Now you can create your view which will display the hours worked. You may wish to create 2 Views depending on your requirements as it may be useful to have 1 view for the employee to view their own hours and a 2nd view for Administrators to view everyone’s working hours.

In our view, under basic settings, we have selected Form 1 (our time sheet form) and ‘All Entries’ as the view type. Our view has been set up as a html table, but you may wish to build yours differently.

In order to calculate the total time worked for the numbers of rows on display in the table, we have created a new code snippet that will create a total of the hidden fields from Form 1 and add display them in a shortcode – [sum_544] – in the ‘After Content’ section of the view.

VIEW HERE

Change 7808 to the ID of your view and 544 to the ID of your hidden field in 2 places. This snippet will allow you to only total what is being displayed in the view and therefore will always display the correct total based on the filters being used.

That's pretty much it for the basics. For a more in-depth look at how this was achieved you can view the full article on our website HERE.

Thanks

Chris

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right