WordPress.org

Ready to get started?Download WordPress

Forums

Participants Database
[resolved] Shortcode to display a specific field in a single record (20 posts)

  1. saz25
    Member
    Posted 4 months ago #

    Hi,
    This is a great plugin. I am using it to store information about cities and towns; not people.

    I have defined fields such as "town", "state", "longitude", "latitude", "population", etc.

    I imported my CSV file. So far, so good.

    Now I'd like to display information in pages I have created. But I'd like to be able to access a specific field in a specific record.

    For example, if I have a page about a certain town in New York, I'd like to display, within my own text, the longitude and population of the specific town.

    Do you have a shortcode that would allow me to display just a field from a specific record? The necessary data is in the database. I just need to access and display it.

    thanks,
    Steve

    https://wordpress.org/plugins/participants-database/

  2. xnau
    Member
    Plugin Author

    Posted 4 months ago #

    Steve, the best way to do this is to create a custom template that just prints the field data with no HTML around it. Generally, you would just take a pdb-single-default.php template and strip out all the HTML, leaving the PHP. Then, in the shortcode, you just need to specify the template and the field to show using the "fields" attribute. This is explained in the Other Notes section under "Determining Which Fields are Shown by Each Shortcode"

    Instructions for setting up a custom template can be found here.

  3. saz25
    Member
    Posted 4 months ago #

    Hi,
    I thank you for your quick reply. I have another question for clarification.

    To display a single record using your pdp-single template, how do you specify the particular record? The documentation says by a pdp=1333 argument.

    How do you determine pdp id?

    What is the syntax of the URL? Is it like this?:
    http://www.domain_name.com/page_name/&pdp=1333

    If I click on "List Participants" in the Dashboard, I see private IDs. Is that what you mean?

    Thanks,
    Steve

  4. xnau
    Member
    Plugin Author

    Posted 4 months ago #

    Sorry, I meant to touch on that...there's two ways: you can do in the the URL with a ?pdb=1333 or you can do it in the shorcode with the record_id attribute:

    [pdb_single record_id=1333]

    If you do it in the URL, all the single shortcodes on the page will get their value from that record.

  5. saz25
    Member
    Posted 4 months ago #

    I tried using the URL
    with /?pdb=1 but nothing displayed.

    I can't get pdp_single to work at all.

    Suggestions ?
    Thanks
    Steve

  6. saz25
    Member
    Posted 4 months ago #

    Hi again,
    I got it to work with the URL.

    Now I need to remove the html and just display the contents of a single cell/field. I tried but kept getting fatal errors. I guess I need to learn php.

    All I want to do is display a single field for example, if a field is named "town", why wouldn't this work?:

    <?php echo Participants_Db::$prefix.$this->$town->form_element ?>

    Do I just remove all the div classes?

    All I want to do is to echo a single character string for a single field within a specific record as indicated by the URL.

    Also, can I create more than one pdb_single short codes? If I want a shortcode that displays a field such as longitude, can I clone the first template and just call the new one php-single-latitude.php? How would I reference that shortcode?

    Thanks and I'm sorry for all the questions.
    Steve

  7. xnau
    Member
    Plugin Author

    Posted 4 months ago #

    No that won't work...anyway, too much to explain, I'll just give you the whole thing, it's very simple.

    I assume you have created your custom template, so just paste this in over everything in your template:

    <?php
    
    /*
     * template for displaying a single value
     */
    ?>
    <?php
    while ($this->have_groups()) : $this->the_group();
      while ($this->have_fields()) : $this->the_field();
        $this->field->print_value();
      endwhile;
    endwhile;
    ?>

    Now, to use it, use this in the shortcode... (I named my template "value" change that to the name of your template) and just give it one field to show.

    [pdb_single template="value" fields="last_name" ]

    The reason to do it this way is it allows you to use one template to show any value you want.

  8. saz25
    Member
    Posted 4 months ago #

    This is great. Its a terrific plug-in.

    I can now access any specific field I want within a given record.

    http://www.domain_name.com/page_name/?pdb=4

    The only small change I would make is allow IDs other than numbers. For example, if I could specify IDs to be names such as:
    newyork
    chicago
    baltimore

    That way the URL would make sense to a user. Using numbers forces me to make sure I never insert a record between existing records.

    Do the IDs need to be numbers that are defined by your code or can they be over-ridden?

    Is there no other way, via a URL, to select a specific/single record other than ?pdb=[number] ?

    This would be a nice addition:
    http://www.domain_name.com/page_name/?town=newyork
    (assuming town is a field and newyork is the contents of the town field for a single/unique record.

    Otherwise, I love this plugin.

    Thanks for your excellent help. I will definitely contribute/donate.
    Great work.
    Steve

  9. xnau
    Member
    Plugin Author

    Posted 4 months ago #

    Huh, so you would want multiple records to match the URL? Like a search result?

  10. saz25
    Member
    Posted 4 months ago #

    No,
    One record would match the URL.
    The name of the town in my application is if fact the unique identifier.
    It's just that I rather use a word (such as town name) in the URL if possible.

    It works as it is now, but it might be a bit more user friendly than an id number.

    But in any case, one record would march the URL.
    Steve

  11. xnau
    Member
    Plugin Author

    Posted 4 months ago #

    Well, you can do it, but you'd have to do some coding in your WP template to translate the unique field to a record id.

  12. saz25
    Member
    Posted 4 months ago #

    Thanks. I'll use this as an excuse to teach myself PHP.
    I've programmed using C and Java for years. It's about time I learned PHP.
    Thanks again,
    Steve

  13. saz25
    Member
    Posted 4 months ago #

    Hi,
    I've made some progress and a few observations.

    It seems that I can't pass any URL arguments to the pdb-list-default.php template. I tried adding a GET command like this:
    echo $_GET["townname"];

    It doesn't work.

    However I can pass arguments using the pdb-single templats but only if I have a ?pdb=[number] argument, which I don't want to do.

    So, I think I want to base my work on a modified version of the pdb-list template but with an argument. True?

    Shouldn't I be able to have a URL like this?:

    http://www.domain.com/testpage/?townname=newyork

    Is it true that the list templates doesn't accept URL arguments? I don't see where its called so I can't see the actual code that invokes the template.
    thanks again,
    Steve

  14. saz25
    Member
    Posted 4 months ago #

    Hi,
    I got it to work. I had to use the pdb-total template since that worked differently than the others and allowed me access to all the records and didn't require an id in the URL.

    Does that make sense?
    Thanks again,
    Steve

  15. xnau
    Member
    Plugin Author

    Posted 4 months ago #

    Yes, that will work...

  16. saz25
    Member
    Posted 4 months ago #

    Hi,
    In reference to this example that you gave, it works great, though I have a question about context and scope of variables.

    <?php
    
    /*
     * template for displaying a single value
     */
    ?>
    <?php
    while ($this->have_groups()) : $this->the_group();
      while ($this->have_fields()) : $this->the_field();
        $this->field->print_value();
      endwhile;
    endwhile;
    ?>

    Instead of that line containing print_value, which prints the contents of the current field, what if I wanted to reference the contents of another field?

    For example, in some cases if I use the shortcode for field X, I want to print it, in another case I want to use the shortcode for a field Y but I want to use the contents of field X (in a calculation) in my print/echo statement? But I don't want to just print it like I did in the first case.

    I know I can specify 2 fields in a shortcode, but they would just both print.

    I guess what I'm asking is how do I reference any specific field within a pdb_single template that's called using the ?pdb=123 url?

    thanks,
    Steve

  17. xnau
    Member
    Plugin Author

    Posted 4 months ago #

    Steve, there are several ways, but the standard approach is to use the PDb_Template class to give you access to all the values of a record. This is not constrained by the shortcode, so even if the shortcode is showing only one field, the template object will have all the values available for whatever purpose.

    Read this article for the basic idea: Using the PDb_Template class in your custom templates

  18. saz25
    Member
    Posted 4 months ago #

    Hi,
    I'm sorry. I must be missing something. What shortcode do you use to invoke the PDb_Template template?

    I've been playing with:
    [pdb_single]
    [pdb_record]
    [pdb_list]
    etc.

    Then editing the code in my own theme template directory.
    Thanks again and sorry for the "dumb" questions.
    Steve

  19. xnau
    Member
    Plugin Author

    Posted 4 months ago #

    It's not done with a shortcode, this is something you add to the custom template. The article explains all that.

  20. saz25
    Member
    Posted 4 months ago #

    I got it to work. Created a real cool custom template using your article and class.

    Awesome, terrific.
    Thanks for all your help.

    Great plug-in !!
    Steve

Reply

You must log in to post.

About this Plugin

About this Topic