我有一个if函数可以计算出有多少用户个人资料已经完成,但是我在下面提到的方式是我能想到的最好的方式,但它似乎效率很低。
更好的方法是什么?
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;
}
答案 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);
答案 1 :(得分:4)
这可能会这样做:
$percentComplete = count(array_filter($user)) / count($user);
答案 2 :(得分:2)
只需遍历$user
中的所有值,并为每个值添加总分。您可以定义哪些字段在数组中给出您在循环中查找分数的分数。
答案 3 :(得分: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;
}
答案 4 :(得分:0)
您可以为每个字段分配一个基线值,然后只需运行一个查询,检查非空字段的总数。
答案 5 :(得分: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;
}
}