Question

I´m using extra_user_details.php on wordpress to show user details in a private profile page. As I´m using a lot of extra fields I though about break the query and make the same output every X values in order to show as tabs:

function eud_extract_ExtraFields() {
if ( get_option( 'eud_fields' ) ) {

    $all_fields = unserialize( get_option( 'eud_fields' ) );

    if ( count( $all_fields ) > 0 ) {

        $output = '';

foreach ( $all_fields as $key => $value ) {
          if ( isset($value[3]) && ! empty($value[3]) ) {
            if ( ($value[3] == 'disable') || ! current_user_can($value[3]) ) {
                continue;
            }
          }
          $output .= '<tr>
                  <th><label for="eud' . esc_attr( $value[1] ) . '">' . esc_attr( $value[0] ) . '</label></th>
                  <td><input name="eud' . esc_attr( $value[1] ) . '" id="eud' . esc_attr( $value[1] ) . '" type="text" value="' . esc_attr( get_user_meta( get_user_id(), $value[1], true ) ) . '" class="regular-text code" />&nbsp;<span class="description">' . ( ( isset( $value[2] ) && $value[2] !== '' ) ? esc_attr( stripslashes( $value[2] ) ) : '' ) . '</span></td>
                </tr>';
        }

    }

    if ($output != '') {
        echo '<div><table class="form-table">';
        echo $output;
        echo '</table></div>';

}
}    }

Thanks!

I´m not sure if this is what I´m looking for. I´m just near...

function eud_extract_ExtraFields() {
if ( get_option( 'eud_fields' ) ) {

    $all_fields = unserialize( get_option( 'eud_fields' ) );

    if ( count( $all_fields ) > 0 ) {
        $output = '';


$i=0;
foreach ($all_fields as $key => $value ) {  

           if ( isset($value[3]) && ! empty($value[3]) ) {
            if ( ($value[3] == 'disable') || ! current_user_can($value[3]) ) {
                continue;
            }
          }


          $output .= '<tr>
                  <th><label for="eud' . esc_attr( $value[1] ) . '">' . esc_attr( $value[0] ) . '</label></th>
                  <td><input name="eud' . esc_attr( $value[1] ) . '" id="eud' . esc_attr( $value[1] ) . '" type="text" value="' . esc_attr( get_user_meta( get_user_id(), $value[1], true ) ) . '" class="regular-text code" />&nbsp;<span class="description">' . ( ( isset( $value[2] ) && $value[2] !== '' ) ? esc_attr( stripslashes( $value[2] ) ) : '' ) . '</span></td>
                </tr>';                 
     ++$i;                      

    if(!($i % 2)) {
        echo '<div><table class="form-table">';
        echo $output; 
        echo '</table></div>';

    }   
    }  

 }
}                   
}

But I need to split the echo, I mean, now the results are:

first tab echo 1, 2

second tab echo 1,2,3,4

third tab echo 1,2,3,4,5,6

and I need the $output to be just:

first tab echo 1,2

second tab echo 3,4

third tab echo 5,6

fourth tab echo 7 (if exists)

Was it helpful?

Solution 2

Solution came from a friend, thanks!

function eud_extract_ExtraFields() {
if (get_option('eud_fields')) {

    $all_fields = unserialize(get_option('eud_fields'));

    if (count($all_fields) > 0) {
        $output = '';


        $i = 1;
        $htmlTotal = '';
        foreach ($all_fields as $key => $value) {

            if (isset($value[3]) && !empty($value[3])) {
                if (($value[3] == 'disable') || !current_user_can($value[3])) {
                    continue;
                }
            }


            $output .= '<tr>
              <th><label for="eud' . esc_attr($value[1]) . '">' . esc_attr($value[0]) . '</label></th>
              <td><input name="eud' . esc_attr($value[1]) . '" id="eud' . esc_attr($value[1]) . '" type="text" value="' . esc_attr(get_user_meta(get_user_id(), $value[1], true)) . '" class="regular-text code" />&nbsp;<span class="description">' . ( ( isset($value[2]) && $value[2] !== '' ) ? esc_attr(stripslashes($value[2])) : '' ) . '</span></td>
            </tr>';
            $i++;
         /* number of fields to show per tab */
            if ($i % 2) {
                $htmlTotal .= '<div><table class="form-table">' . $output . '</table></div>';
                $output = '';
            }
        }

        echo $htmlTotal;
        }
     }
 }

OTHER TIPS

You could use modulus:

$i=0;
foreach ( $all_fields as $key => $value ) {

    if( $i++%5 === 0 ){ echo 'I was number 5';}
}

or if you prefer a binary comparison (should be faster):

if( $i++&101 === 0 ){ echo 'I was number 5';}

I'll give you an example, you can piece it together for your code:

Lets say you have an N amount of span, and you want them grouped per 5 in a div:

// You start with:
echo '<div>';
for($i=1; $i<=23; $i++){
    echo '<span> '.$i.' </span>'; // just an example, could be anything here
}
echo '</div>';

This will place 23 span in one big div. Now we add something to group them by 5:

// You start with:
echo '<div>';
for($i=1; $i<=23; $i++){
    echo '<span> '.$i.' </span>'; // just an example, could be anything here
    if( $i %5===0 ){
        echo '</div><div>'; // every 5th, close the div, and open a fresh one.
    }
}
echo '</div>';

This will result in 5 (=coincedence, nothing to do with %5) div's, 4 with 5 spans, and one with the remaining 3. You can do this trick with about any element.

Tip: in the modulus-if-statement you should add the max: $i %5===0 && $i!==23, to prevent </div></div><div> if $i is a number devidable by 5.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top