Support » Plugin: Participants Database » [pdb_record] shortcode with parameters

  • Resolved sondo

    (@sondo)


    Dear Xnau,
    I’m sorry to ask once again about a resolved topic.
    As I understand, [pdb_record] doesn’t accept filter parameters (as you answered in one of the post).
    Do you think that it’s good if [pdb_record] also accepts filter paramaters? And also, Readonly fields can be set as a parameters from shortcodes? That would make the plugin more flexible.

    Let me give 2 examples for the need:
    1. I have students from different departements signup with the field “dept” (selected from a dropdown list.) And then, I want in the record page, base on which dept he/she from, a proper dropdown list of subjects will be chosen. It can be done easily If I create many subject fields that associated with each departement (e.g subject_Phys_dept, subject_math_dept…) and if [pdb_record] accepts filter.

    2. About the read-only parameters, at the moment, it’s set fix from “Manage database”. However, if it’s a parameter of shortcodes, then I can create a field that Editor can modify but subscribers can read only.

    Thank you

    http://wordpress.org/extend/plugins/participants-database/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author xnau webdesign

    (@xnau)

    I appreciate your comments, and those features could be useful, as you’ve shown. All of these things can be accomplished with custom templates, so the functionality is available, it will just require a bit more work on your part to get it done.

    I have tried to give convenient access to the most-needed features, while other less commonly needed features are possible with a bit of deeper customization through the use of templates. I know this requires plugin users to have some knowledge of how to use PHP, but I’ve made it easy.

    For instance, your first request can be easily filled with a statement that chooses which courses dropdown to show based on the department the student is in. You wouldn’t be able to do this with a filter, by the way.

    In your custom template for the [pdb_record] shortcode, at the top, just after the first “?>” put something like this:

    <?php $department = $this->record->main->fields->department->value ?>

    “main” is the name of the group the field is in and “department” is the name of the field…you may need to change those.

    Now, after the line with “$this->the_field()” put:

    <?php switch ($this->field->name) {
    case 'subject_Phys_dept' :
       if ($department != 'phys') continue 2;
    case 'subject_math_dept' :
       if ($department != 'math') continue 2;
    } ?>

    An so forth…you will have to spell out a case for each courses dropdown because any field that is not skipped by the switch will be displayed.

    For the next one, it’s just a matter of adding a statement to your switch statement that catches the readonly field and sets the value…like:

    case 'department_head' :
       if ($department == 'phys') $this->field->value = 'Arnold Smith';
       elseif ($department == 'math' ) $this->field->value = 'Sam Walter';
       break;

    and so forth.

    You will note that the “break” statement is needed if execution would continue through the switch statement…the “continue 2” statement makes this unnecessary because it’s breaking out of the loop. The “2” after the continue is needed because at that point execution is inside two control structures and we need to continue out of both.

    Thread Starter sondo

    (@sondo)

    Thank you very much for your instruction!
    It works well if the subject field (e.g subject_phys_dept) is a text-line type.
    However, if the field is a dropdown-list then I don’t know how to assign/access its value.
    An assignment like this ($this->field->value = “abc”) doesn’t work for a dropdown list.

    Could you please show me how to deal with this type of field?

    (I plan to create 1 dropdown list for dept, 1 dropdown list for subject which is an empty list. Then in the template, I check the dept, base on the dept, I “feed” the subject a list of subjects)

    Comment: (it might be interested to other users)
    Actually, before reading your answer, I had tried another solution using multiple forms (and multiple groups of fields). I create as many templates as the number of departements. Then I create a wordpress-template for the report page, that check the dept and calls [pdb_record template=”dept_template”] where each “dept_template” selects a proper set of groups to show. It works fine as well, except that I have to set the subject as not-required field (because I can set the value for hidden dropdown-list, the same problem as above).

    Plugin Author xnau webdesign

    (@xnau)

    sondo,

    Dropdown fields have their option values stored as arrays, but the “value” is stored as a string, because the field can only have one value. Now, if your field is actually a “multiselect” then the value is also stored as an array, but your field is a dropdown, and there is no multiselect-dropdown.

    So, could you describe what you mean by it doesn’t work…because it should, as long as the value you are setting it to is a value that can be found in the list of options.

    But perhaps the first part of your question is related to the second part.

    I think there is a better way to deal with your subject dropdown. Instead of giving it option values dynamically, you should create a different dropdown for each department, each one with it’s own set of subjects. Then, when the record edit page is displayed, only the appropriate dropdown is shown, the rest being suppressed.

    If you really do want to do it dynamically, then dealing with the stored arrays requires you to “unserialize” them because PHP arrays can’t be stored directly in a database. Check into the “serialize” and “unserialize” functions in the PHP docs to understand how to work with flattened arrays.

    Thread Starter sondo

    (@sondo)

    Thanks Xnau,
    It works now!

    I did not work (i.e the subject dropdown list doesn’t show a list of subjects Or I couldn’t set value for a hidden dropdown-list-fields) because I assigned array-values to $this->field->value. The right one must be $this->field->values.

    Now I’m convinced that we don’t need filters for [pdb-record] and we can do many thing with custom templates.

    Let me resume the problem and solution:
    1. What I want:
    I want a subject type dropdown-list in Record page that lists out a list of subjects depending on the Department which a student chosed (on Signup page).
    2. Solution:

    1. Create a customer template pdb-record-mytemplate.php
    2. After the first ?> put something like this: <?php $department = $this->record->main->fields->department->value ?>main” is the name of the group the field is in and “department” is the name of the field.
    3. Prepare lists of subjects for each department
      $subjects_of_Phys = array('Mechanics', 'EM', 'QM');
      $subjects_of_Math = array('Algebra','Complex Analysis','Geometry');

      an so on

    4. After the line with $this->the_field() put:
      <?php if ($this->field->name == 'subject'){
                                  switch ($department) {
                                      case 'Physics' :
                                          $this->field->values = serialize($subjects_of_Phys);
                                          continue ;
                                      case 'Mathematics' :
                                          $this->field->values = serialize($subjects_of_Math);
                                          continue ;
                                   }

    And that’s it.

    There is a good command that I used to find the structure of fields var_dump($this->field);.

    Thank you very much for your support Xnau!

    Plugin Author xnau webdesign

    (@xnau)

    Nice solution!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[pdb_record] shortcode with parameters’ is closed to new replies.