Pez Cuckow
Pez Cuckow

Reputation: 14412

PHP If/Else - More efficent way to do this?

I have a if function that works out how much of a users profile is completed however the way I include below was the best I could think of, however it seems really inefficient.

What is the better way to do this?

if($user['first_name']!==""&&$user['last_name']!==""&&$user['pemail']!==""&&$user['dob']!==""&&$user['ambitions']!==""&&$user['memories']!==""&&$user['thoughts']!==""&&$user['message_1']!=="0"&&$user['message_2']!=="0"&&$user['message_3']!=="0"&&$user['v1']!=="0"&&$user['v2']!=="0"&&$user['v3']!=="0"&&$user['v4']!=="0"&&$user['v5']!=="0"&&$user['v6']!=="0"&&$user['v7']!=="0"&&$user['v8']!=="0"&&$user['v9']!=="0"&&$user['image_1']!==""&&$user['image_2']!==""&&$user['image_3']!=="") {
    $completed = 4;
} elseif($user['first_name']!==""&&$user['last_name']!==""&&$user['pemail']!==""&&$user['dob']!==""&&$user['ambitions']!==""&&$user['memories']!==""&&$user['thoughts']!==""&&$user['v1']!=="0"&&$user['v2']!=="0"&&$user['v3']!=="0"&&$user['v4']!=="0"&&$user['v5']!=="0"&&$user['v6']!=="0"&&$user['v7']!=="0"&&$user['v8']!=="0"&&$user['v9']!=="0"&&$user['image_1']!==""&&$user['image_2']!==""&&$user['image_3']!=="") {
    $completed = 3;
} elseif($user['first_name']!==""&&$user['last_name']!==""&&$user['pemail']!==""&&$user['dob']!==""&&$user['ambitions']!==""&&$user['memories']!==""&&$user['thoughts']!==""&&$user['message_1']!=="0"&&$user['message_2']!=="0"&&$user['message_3']!=="0"&&$user['image_1']!==""&&$user['image_2']!==""&&$user['image_3']!=="") {
    $completed = 2;
} elseif($user['first_name']!==""&&$user['last_name']!==""&&$user['pemail']!==""&&$user['dob']!==""&&$user['ambitions']!==""&&$user['memories']!==""&&$user['thoughts']!==""&&$user['image_1']!==""&&$user['image_2']!==""&&$user['image_3']!=="") {
    $completed = 1;
} else {
    $completed = 0;
}

Upvotes: 1

Views: 204

Answers (6)

Rik Heywood
Rik Heywood

Reputation: 13972

How about...

$total = count($user);
$missing = 0;
foreach ($user as $item)
{
    if (empty($item))
        $missing++;
}

// work out a percentage complete.
$percentcomplete = intval((($total-$missing)/$total)*100);

Upvotes: 11

Ramuns Usovs
Ramuns Usovs

Reputation: 1454

how about something like this - it seems longer, but it does let you change the values for each of the levels in a more manageable way

$l1 = array(
    'first_name','last_name','pemail','dob','ambitions',
    'memories','thoughts','image_1','image_2','image_3'
);
$l2 = array_merge(
    array(
        'message_1','message_2','message_3',
    )
    ,$l1);
$l3 = array_merge(
    array(
        'v1','v2','v3','v4','v5','v6','v7','v8','v9'            
    ),
    $l1
);
$l4 = array_unique(array_merge($l2,$l3));

$completed = 4;
for ($i = 4; $i > 0 $i-- ) {
    $arr = 'l'.$i;
    foreach ( $$arr as $key ) {
        if ( $user[$key] == '' || $user[$key] == '0' ) {
            $completed--;
            break;
        } 
    }
    if ( $i == $completed ) {
        break;
    } 
}

Upvotes: 0

bobince
bobince

Reputation: 536329

function allset($arr, $names) {
    foreach ($names as $name)
        if ($arr[$name]=='')
            return FALSE;
    return TRUE;
}

$completed= 0;
if (allset($user, array('first_name', 'last_name', 'pemail', 'dob', 'ambitions', 'memories', 'thoughts', 'image_1', 'image_2', 'image_3'))) {
    $completed+= 1;
    if (allset($user, array('message_1', 'message_2', 'message_3')))
        $completed+= 1;
    if (allset($user, array('v1', 'v2', 'v3', 'v4', 'v5', 'v6', 'v7', 'v8', 'v9')))
        $completed+= 2;
}

Upvotes: 2

nickf
nickf

Reputation: 545985

This might do it:

$percentComplete = count(array_filter($user)) / count($user);

Upvotes: 4

Asbjørn Ulsberg
Asbjørn Ulsberg

Reputation: 8820

Just loop through all values in $user and for each value add it to a total score. You can defined which fields give what score in arrays that you look up the score in within the loop.

Upvotes: 2

Paul McMillan
Paul McMillan

Reputation: 20107

You might assign a baseline value to each field, and then simply run a query checking the total for non-empty fields.

Upvotes: 0

Related Questions