How to display text if profile fields are not filled?

How to display any text/content if all buddypress user profile fields are not filled?

Here is my code which retrieves field data if the field is filled by the user:

$fields = array('Field 1', 'Field 2', 'Field 3', 'Field 4', 'Field 5');
foreach ( $fields as $field) {

    $data = xprofile_get_field_data($field);
    
    if ($data) {
        echo 'div class='. $field .' - '. $data .'/div';
    }
}

// How to display here information if all 5 fields are not filled by user?

Topic profiles buddypress users Wordpress

Category Web


You could use, what i call flag (don't remember the profitional term but im sure there is one).

The idea is to create a variable that has a boolean value, depends on the situation, for our example it will be false.

So basicaly from the very start we assume that all fields have no values and every thing is ok, in the foreach we will do the check if the field actualy has a value, if it has, we will set our flag to true.

Now we know that if our flag was false, we can display what we want, but if its true we don't display.

The code will be like this

$fields = array('Field 1', 'Field 2', 'Field 3', 'Field 4', 'Field 5');

$flag = false;

foreach ( $fields as $field) {

    $data = xprofile_get_field_data($field);
    
    if ($data) {
        echo '<div class="">'. $field .' - '. $data .'</div>';

        $flag = true;
    }
}

if (!$flag) {
    // display here information if all 5 fields are not filled by user
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.