• Resolved gorirrajoe

    (@gorirrajoe)


    does the multicheck field type save custom keys for arrays?

    $event_array = $wpdb->get_results("SELECT event_slug, event_location FROM DBNAME WHERE event_slug != 'series' ORDER BY event_location");
        $updated_event_array = array();
        foreach( $event_array as $event ) {
          $updated_event_array[$event->event_slug] = $event->event_location;
        }
    
        // multicheck
        $cmb->add_field( array(
            'name'    => 'Dropdown Events',
            'id'      => $prefix . 'vip_dropdown',
            'type'    => 'multicheck',
            'options' => $updated_event_array,
            'select_all_button' => false
        ) );

    however, when i print_r() this, it spits back:
    Array ( [0] => new-orleans )

    ignoring my key that i added

    https://ww.wp.xz.cn/plugins/cmb2/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    What’s the complete results for the print_r()? What you provided so far looks like just a portion.

    It is also possible that it doesn’t, but I’d need some sort of test case data to tinker with and confirm.

    Thread Starter gorirrajoe

    (@gorirrajoe)

    here’s more code:

    those are the complete results. if i select more options, however, it looks like this

    Array ( [0] => new-orleans [1] => portland [2] => san-diego )

    $vip_array = get_post_meta( get_the_ID(), '_rnr3_vip_dropdown', true );
    print_r($vip_array );

    serialized array looks like so:
    a:3:{i:0;s:11:"new-orleans";i:1;s:8:"portland";i:2;s:9:"san-diego";}

    Plugin Author Justin Sternberg

    (@jtsternberg)

    Reading through this, I’m not sure I fully understand the request? Multicheck type saves an array of the values selected, like your debug demonstrates.

    Side note, I would not do any queries to generate the options array for multicheck. It is far better if you create a callback and use the ‘options_cb’ to generate those results and do the query so that the query only happens when it’s needed (vs every page-load of the site).

    // multicheck
    $cmb->add_field( array(
    	'name'    => 'Dropdown Events',
    	'id'      => $prefix . 'vip_dropdown',
    	'type'    => 'multicheck',
    	'options_cb' => 'get_updated_event_array',
    	'select_all_button' => false
    ) );

    and

    function get_updated_event_array() {
    
    	$event_array = $wpdb->get_results( "SELECT event_slug, event_location FROM DBNAME WHERE event_slug != 'series' ORDER BY event_location" );
    	$updated_event_array = array();
    
    	foreach( $event_array as $event ) {
    		$updated_event_array[ $event->event_slug ] = $event->event_location;
    	}
    
    	return $updated_event_array;
    }

    Thread Starter gorirrajoe

    (@gorirrajoe)

    i guess i was trying to get a way to save a user’s option so that i could get 2 variables out of it.

    if that’s not even possible, i’ll work on a diff way to do it…

    the way i’m using it:

    <?php
      $vip_array = get_post_meta( get_the_ID(), '_rnr3_vip_dropdown', true );
      // print_r($vip_array );
    ?>
                  <select id="selectrace">
                    <option value="box">-- Select a race --</option>
                    <?php foreach( $vip_array as $key => $value ) {
                      echo '<option value="'.site_url( $key.'/the-weekend/vip/') .'">'. $value .'</option>';
                    } ?>
                  </select>

    ** thanks for the sidenote, that’s helpful (i didn’t know it worked that way — always thought it only made the call on that page in the admin)

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Yeah, looks like it’s not preserving the named indexes.

    Is there a particular reason you’re needing the event slugs preserved? Curious if there’s a better field type for your use-case.

    Plugin Author Justin Sternberg

    (@jtsternberg)

    If you don’t want to do the DB lookup again based on the saved slug, you could always try to combine the value/label into the value field:

    function get_updated_event_array() {
    
    	$event_array = $wpdb->get_results( "SELECT event_slug, event_location FROM DBNAME WHERE event_slug != 'series' ORDER BY event_location" );
    	$updated_event_array = array();
    
    	foreach( $event_array as $event ) {
    		$updated_event_array[ esc_attr( $event->event_slug . '::' . $event->event_location ) ] = $event->event_location;
    	}
    
    	return $updated_event_array;
    }

    and

    foreach ( $saved_values as $value ) {
    	list( $slug, $location ) = explode( '::', $value );
    }

    Thread Starter gorirrajoe

    (@gorirrajoe)

    love that solution, justin!

    thanks!

    Plugin Author Justin Sternberg

    (@jtsternberg)

    👍

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Multicheck not saving properly?’ is closed to new replies.