views:

147

answers:

6

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;
}
A: 

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

Paul McMillan
+11  A: 

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);
rikh
Suggestion: how about just using `empty($item)` instead of `$item == '' || $item == '0'`?
brianreavis
Didn't know about empty. What a useful thing :-) I've updated the code above to use it.
rikh
This code does assume there is at least 1 item in $user and that it is an array. You can add your own checks for this...
rikh
It needs to have levels depending on what they have completed, not just a total count!
Pez Cuckow
+2  A: 

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.

asbjornu
+4  A: 

This might do it:

$percentComplete = count(array_filter($user)) / count($user);
nickf
+2  A: 
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;
}
bobince
A: 

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;
    } 
}
Ramuns Usovs