2009-11-11 102 views
1

我有一個if函數可以計算出用戶配置文件的完成程度,但我在下面包含的方式是我能想到的最好的方式,但它看起來效率很低。PHP If/Else - 更有效的方法來做到這一點?

有什麼更好的方法來做到這一點?

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

回答

2
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; 
} 
0

您可能會爲每個字段分配一個基準值,然後只需運行一個查詢來檢查非空字段的總數。

11

......怎麼

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

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

建議:如何使用'empty($ item)'而不是'$ item ==''|| $ item =='0''? – brianreavis 2009-11-11 13:11:43

+0

不知道空着。什麼是有用的東西:-)我已經更新了上面的代碼來使用它。 – 2009-11-11 13:19:29

+1

這段代碼確實假設$ user中至少有一個項目,並且它是一個數組。你可以添加你自己的支票... – 2009-11-11 13:20:33

2

通過$user併爲每個值的所有值只是循環將其添加到總成績。您可以定義哪些字段給出了在循環中查找得分的數組中的得分。

4

這可能做到這一點:

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

如何對這樣的事情 - 它似乎更長的時間,但它確實讓你改變在一個更易於管理的方式對於每個級別的值

$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; 
    } 
} 
相關問題