Posted on by

Showing a Record Edit Form Based on a Value in the Record

When using Participants Database to represent two or more classes of people or things, you sometimes need to customize the frontend edit form so that is only shows the fields that are relevant to the type of record that is getting edited. We can easily do this with a custom template.

If you’re not familiar with the use of custom templates, read this article: Using Participants Database Custom Templates

For our example, we are continuing the example used in “Multiple Databases” with Participants Database where you have a kid’s sports team and you have records for the players and the parents. We want to show a different record edit form for each of the two classes of people.

When someone wants to edit their record, they will typically use either a “private link” or a login page provided by the Particpant Login add-on. The way this works, when they go to their edit record page, we use a custom template to determine what kind of record edit form to show, and then the template invokes the record edit form with the desired parameters. It’s a template that uses shortcodes, as you will see.

As in the example, you have your fields organized into three groups: “contact”, “player” and “parent”. The “contact” group is used for all records and the other two are specific to the type of person. There is a field named “type” that tells us which type the record is for, and we use that to determine which set of field groups to show.

Here is a custom template that does that, it is named pdb-record-usertype.php

To use the template, use this shortcode on your record edit page:

[pdb_record template=usertype]

That’s it! Of course, you’ll need to customize this to your particular situation, but that is the general technique.

9 thoughts on “Showing a Record Edit Form Based on a Value in the Record

  1. Roland, I am trying to create a prototype where a user enters a date and the next x dates are calculated then, using a different plugin, show hide content based on the dates.

    I’m thinking if I can store just one date in the Participant Database, I can retrieve it based on user login id (i.e. email)

    Seems like this template might be the one to start with. True?

    Also, right now I am calculating dates in PHP h, could that be done in the database?

    Thanks

    1. HI Alan,

      While I’m not sure I understand what you’re trying to accomplish, I will point out that this tutorial is not for finding which record to show, but only which template to use. In this case, which record is shown is determined by the URL.

      You are looking for a way to find a record based on the logged-in user’s info, right? Take a look at this page, it will give you some ideas on that: Using Participants Database with WordPress Users

  2. Hello… Mr Roland…
    It been more than a week now that am trying to run the above code on my website to show two records… I followed your procedure word after word….
    But the code won’t run, instead, it copies itself on my Web page..
    Please what could I have possibly omitted?? I have verified the code thoroughly and it exactly as yours…
    Please help me… Thanks for your reply

    1. I can’t tell what is going wrong from your description, so here are the things to check. First, make sure you understand how custom template work by reading this article. If you have the file (with the name pdb-record-usertype.php) uploaded to the correct location, then the template will be used if you have the shortcode [pdb_record template=usertype] on the page. When that page is opened with the correct link (it must have the “pid” code) you will see the fields of the editable record.

  3. Thank you for your answer, I did not see it immediately.

    I picked up a similar code from your repository but used an if loop. In effect, we used $this->participant_valuesin this code and it worked. I adapted your code for my case.

    Thanks again,
    Best regards

  4. Hello Roland,

    Thank you for your prompt response.
    I modified the shortcode in my previous code and followed your advice. It still does not work.

    I did some tests:

    – I changed the variable $type by something else thinking it could be already used elsewhere.
    – I use another participant_record
    – I add a step to the switch (case”) to know if my variable $typetestvar was filled with the record participant I want.

    Now I can see that the problem is at $typetestvar = $this-> participant_record [‘type1’];
    because it is empty and show the last message don’t work.

    Do you have an idea ?
    Thanks in advance and have a nice weekend,
    Best regards,

    last edit: https://gist.github.com/Donma28/be6b9addfae7a141f2211acc9283be31

    1. The property you want to use is $this->participant_values

  5. Hello Mr Roland,

    I have a small problem with this code. Could you help me please?

    https://gist.github.com/Donma28/be6b9addfae7a141f2211acc9283be31

    I took example on your tutorial above and I just modified the values of the shortcode as well as added an additional box

    When I use the shortcode [pdb_record tabs = "true" template = usertype] I have nothing that appears on my page. Do you have an idea ?

    I take this opportunity to ask another question
    Is it possible to adapt this code for the shortcode [pdb_single] so that the right groups are displayed according to the record and not the user who looks at the record.

    I hope I have been clear, my English is not very good please excuse me.

    Thank you very much in advance for any help you can give me.
    Regards

    1. First, if you are using a custom template, you should not use the “tabs=true” attribute in the shortcode. Also, don’t put spaces around the =. So your shortcode should be:

      [pdb_record template=usertype]

      Now, your template will be used to show the record.

      Setting up a similar custom template for the pdb_single shortcode is the same, you just need to use the correct name for your custom template, then change all the shortcodes in the custom template to be pdb_single.

Leave a Reply
You have to agree to the comment policy.

9 thoughts on “Showing a Record Edit Form Based on a Value in the Record

  1. Roland, I am trying to create a prototype where a user enters a date and the next x dates are calculated then, using a different plugin, show hide content based on the dates.

    I’m thinking if I can store just one date in the Participant Database, I can retrieve it based on user login id (i.e. email)

    Seems like this template might be the one to start with. True?

    Also, right now I am calculating dates in PHP h, could that be done in the database?

    Thanks

    1. HI Alan,

      While I’m not sure I understand what you’re trying to accomplish, I will point out that this tutorial is not for finding which record to show, but only which template to use. In this case, which record is shown is determined by the URL.

      You are looking for a way to find a record based on the logged-in user’s info, right? Take a look at this page, it will give you some ideas on that: Using Participants Database with WordPress Users

  2. Hello… Mr Roland…
    It been more than a week now that am trying to run the above code on my website to show two records… I followed your procedure word after word….
    But the code won’t run, instead, it copies itself on my Web page..
    Please what could I have possibly omitted?? I have verified the code thoroughly and it exactly as yours…
    Please help me… Thanks for your reply

    1. I can’t tell what is going wrong from your description, so here are the things to check. First, make sure you understand how custom template work by reading this article. If you have the file (with the name pdb-record-usertype.php) uploaded to the correct location, then the template will be used if you have the shortcode [pdb_record template=usertype] on the page. When that page is opened with the correct link (it must have the “pid” code) you will see the fields of the editable record.

  3. Thank you for your answer, I did not see it immediately.

    I picked up a similar code from your repository but used an if loop. In effect, we used $this->participant_valuesin this code and it worked. I adapted your code for my case.

    Thanks again,
    Best regards

  4. Hello Roland,

    Thank you for your prompt response.
    I modified the shortcode in my previous code and followed your advice. It still does not work.

    I did some tests:

    – I changed the variable $type by something else thinking it could be already used elsewhere.
    – I use another participant_record
    – I add a step to the switch (case”) to know if my variable $typetestvar was filled with the record participant I want.

    Now I can see that the problem is at $typetestvar = $this-> participant_record [‘type1’];
    because it is empty and show the last message don’t work.

    Do you have an idea ?
    Thanks in advance and have a nice weekend,
    Best regards,

    last edit: https://gist.github.com/Donma28/be6b9addfae7a141f2211acc9283be31

    1. The property you want to use is $this->participant_values

  5. Hello Mr Roland,

    I have a small problem with this code. Could you help me please?

    https://gist.github.com/Donma28/be6b9addfae7a141f2211acc9283be31

    I took example on your tutorial above and I just modified the values of the shortcode as well as added an additional box

    When I use the shortcode [pdb_record tabs = "true" template = usertype] I have nothing that appears on my page. Do you have an idea ?

    I take this opportunity to ask another question
    Is it possible to adapt this code for the shortcode [pdb_single] so that the right groups are displayed according to the record and not the user who looks at the record.

    I hope I have been clear, my English is not very good please excuse me.

    Thank you very much in advance for any help you can give me.
    Regards

    1. First, if you are using a custom template, you should not use the “tabs=true” attribute in the shortcode. Also, don’t put spaces around the =. So your shortcode should be:

      [pdb_record template=usertype]

      Now, your template will be used to show the record.

      Setting up a similar custom template for the pdb_single shortcode is the same, you just need to use the correct name for your custom template, then change all the shortcodes in the custom template to be pdb_single.

Leave a Reply
You have to agree to the comment policy.