Include “required questions” in a random selection

后端 未结 2 1059
情深已故
情深已故 2021-01-18 05:16

I\'m selecting a set of random questions without any duplicates using the following:



        
2条回答
  •  悲&欢浪女
    2021-01-18 05:38

    The Question doesn't state it, but all suggests this is an Advanced Custom Fields set up using the Repeater Add-on.

    In that case, this is the test configuration I've done:

    Note that here I'm using $repeater_row['title'] instead of the OP's $repeater_row['question']. Also, I removed the answer_options part. See comments for details:

    // Get fields
    $amount = get_field( 'select_number_of_questions' );
    $repeater = get_field( 'step_by_step_test' );
    
    // Auxiliary arrays to separate fields by Field Name
    $not_enabled = array();
    $enabled = array();
    
    // Separate
    foreach( $repeater as $field )
    {
        if( 'no' == $field['enabled'] )
            $not_enabled[] = $field;
        else
            $enabled[] = $field;
    }
    
    // Discount the enabled from the the total amount
    $amount = (int)$amount - count( $enabled );
    
    // Shuffle before slicing
    shuffle( $not_enabled );
    $repeater_limit = array_slice( $not_enabled, 0, $amount );
    
    // Add enabled fields and shuffle again
    $final_array = array_merge( $repeater_limit, $enabled ); 
    shuffle( $final_array );
    
    foreach( $final_array as $repeater_row ) {
        echo "

    " . $repeater_row['title'] . "

    "; }

提交回复
热议问题