• Resolved Pascal CESCATO

    (@pcescato)


    I’m trying to get some datas from a mysql database, here is the tag:

    [dynamic_select licencie id:licencie class:select "source:filter"]

    And here is the full code snippet:

    add_filter( 'cf7sg_custom_dynamic_select','licencie_dynamic_options',10,3);
    function licencie_dynamic_options($options, $tag, $cf7_key){
      if('form-1'!==$cf7_key || 'licencie' !== $tag->name){
        return $options;
      }
      global $wpdb;
      $sql = "SELECT id, CONCAT(firstname, ' ', lastname) AS fullname FROM {$wpdb->prefix}_mytable";
      $datas = $wpdb->get_results($sql);
    	foreach($datas as $row){
    		$data[$row->id] = $row->fullname;
    	}
      foreach($data as $value=>$label){
        $options[$value]=$label;
      }
      return $options;
    }

    But I can’t get values, all I get is the first letter of fullname. What did I missed? Shall I return a json array instead of a PHP one?

    If I display $options (var_export), I have this:

    array (
      1 => 'John Doe',
      2 => 'Mary Doe',
    )

    But in my form, there is:

    <select value="" id="licencie" name="licencie" class="wpcf7-form-control cf7sg-dynamic-list cf7sg-dynamic_select wpcf7-dynamic_select select">
    <option value="1">J</option>
    <option value="2">M</option>
    </select>

    Can you help me please?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Pascal CESCATO

    (@pcescato)

    finally, I found the solution; here is the full code snippet:

    add_filter( 'cf7sg_custom_dynamic_select','licencie_dynamic_options',10,3);
    function licencie_dynamic_options($options, $tag, $cf7_key){
      if('saisie-1'!==$cf7_key || 'licencie' !== $tag->name){
        return $options;
      }
      global $wpdb;
      $sql = "SELECT id, CONCAT(firstname, ' ', lastname) AS fullname FROM {$wpdb->prefix}_mytable";
      $datas = $wpdb->get_results($sql);
      foreach( $datas as $row ){
        $options[ $row->id ] = array( $row->fullname );
      }
      return $options;
    }

    So you have to put the option text in an array, or it won’t be displayed correctly. So you must write this:

    $options[ $value ] = array( $text );

    to have:

    <option value="$value">$text</option>
    Plugin Author Aurovrata Venet

    (@aurovrata)

    apologies for the late reply. I was down with COVID.

    glad you found the solution and thanks for posting it here ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘dynamic select’ is closed to new replies.