Support » Plugin: Participants Database » Chosen Multi Dropdown with populated data

  • Resolved DownRightGamer

    (@rcarmichael)


    I’m using your chosen multi drop down and thought I’d move the issue to the forum.

    Here’s my full code…

    /**
     * Plugin Name: Participants Database - Populate Custom Chosen Dropdown
     * Description: tests loading a chosen element with options
     *
     * sets our function to be called when the pdbcde-before_element_rendered action 
     * is triggered by the form just before the "Chosen Dropdown" is shown so we can 
     * change the list of options to show
     */
     
    // attach our function to the pdbcde-before_element_rendered action
    add_action( 'pdbcde-before_element_rendered', 'xnau_set_specialty_dropdown_options');
    /**
     * sets the options for the "specialty" dropdown
     *
     * @global wpdb $wpdb
     * @param PDb_FormElement object $field the current field
     */
    function xnau_set_specialty_dropdown_options ( $field )
    {
      // this is the name of the field we want to add options to
      $fieldname = 'team';
     
      if ( $field->name === $fieldname ) :  // check for our dropdown field
      
      global $wpdb; // grab the db helper object
      
      /*
       * define the query for getting the list saved specialties
       * 
       * note that the $wpdb->prefix method is used to get the table 
       * prefix; this is so it will work on all WP installs
       */
      $query = '
        SELECT first_name,last_name,job_category,record_slug
        FROM <code>' . $wpdb->prefix . 'participants_database</code> 
        WHERE job_category NOT LIKE "%staff%"
      ';
    	
      // now execute the query and get the results
      $raw_names = $wpdb->get_results( $query );
      
      /*
       * now expand the result array into an array for the options property of the dropdown
       */
      $options = array();
    
      foreach ( $raw_names as $record ) {
    	  if ($record->job_category == 'faculty') : $options[] = $record->first_name.' '.$record->last_name.' (Faculty)'; 
    	  elseif ($record->job_category == 'staff') : $options[] = $record->first_name.' '.$record->last_name.' (Staff)';
    	  elseif ($record->job_category == 'researcher') : $options[] = $record->first_name.' '.$record->last_name.' (Researcher)';
    	  elseif ($record->job_category == 'student') : $options[] = $record->first_name.' '.$record->last_name.' (Graduate Student)'; endif;
      }
      
      // now set the field object with the new options list
      $field->options = $options;
      
      endif;
    }

    Now, to reiterate my issue, it is inputting my data in fine but when I save my changes and go back to the drop down, it is merging my choices into one

    • tag. Please help and thank you!

    The page I need help with: [log in to see the link]

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

    (@xnau)

    first, tags don't belong in php code. This is for the benefit of anyone who might try to copy your code.

    There is a fundamental problem with your approach and that is that you are providing the user with options that are not defined options for the "team" field. While you can do that with one option, you cannot do that with multiple options, they will be combined into a single value. This is because a multiselect/other field can only have a single "other" value.

    What you need to do is a bit more complex because you need a way to actually save the staff names to the field definition as options, then your users can choose multiple names. You can do this manually, or given some custom code, automatically.

    Thread Starter DownRightGamer

    (@rcarmichael)

    Apologies. My code actually did not originally have tags in it, but had the marks in there that make it think i’m starting and ending code, when I in fact did not. I posted a continuation of my problem here because your website has limitations. I realized bringing it here I could better explain my issue.

    Now…. my struggle is trying to figure out your plugin. I realized even before yesterday I need to get values into the inputs. But it is proving to be quite complicated. I was just hoping you had a similar situation come up and could offer more advice. At this particular juncture, i’m running out of time and can’t fiddle with this for much longer. Unfortunately, that means i’ll have to resort to a less convenient way of adding team members, but at least it will work for the time being.

    If you have no further advice, i’ll close this thread. Thanks for your help.

    Thread Starter DownRightGamer

    (@rcarmichael)

    Closing. Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Chosen Multi Dropdown with populated data’ is closed to new replies.