使用PHP

时间:2018-11-02 12:01:01

标签: php mysql mysqli

我有一个表“ tbl_userpersonal”,

我要实现的是检查哪些列已填充,哪些未填充,因此我可以计算或显示概要文件完成百分比。 \

到目前为止,我已经尝试了许多不同的技术和代码,我的代码正在计算所有列,但我希望它将空值列计算为0。 请为我提供解决方案。

到目前为止,即使在任何一列中只有一个用户条目,它都会给我100%,否则为0%,这之间就没有

当前,如果在user_authtoken =“ app_7837hfjd57hdj”的用户的“ father_name”和“ mother_name”和“ DOB”列中有值,则预期输出应为 3 50%,即(3/6)* 100

现在正在发生的事情

如果对于user_authtoken =“ app_7837hfjd57hdj”的用户,在这两列中都有值,则输出显示为

6 100%,即(6/6)* 100

或如果没有使用user_authtoken =“ app_7837hfjd57hdj”的用户条目,则输出为 0 0%

enter image description here

这是php代码

    $personal = mysqli_query($con,"

     SELECT   father_name,
     mother_name,
     DOB,
     adhar_no,
     address,
     religion,
     CASE WHEN father_name IS NOT NULL THEN 1 ELSE 0 END + 
     CASE WHEN mother_name IS NOT NULL THEN 1 ELSE 0 END + 
     CASE WHEN DOB IS NOT NULL THEN 1 ELSE 0 END + 
     CASE WHEN adhar_no IS NOT NULL THEN 1 ELSE 0 END +
     CASE WHEN address IS NOT NULL THEN 1 ELSE 0 END +
     CASE WHEN religion IS NOT NULL THEN 1 ELSE 0 END AS personal
     FROM  user WHERE `user_authtoken` = '$user_ath'

     ") or die(mysqli_error($con));

      $data=mysqli_fetch_assoc($personal);
      $pro_count =  $data['personal'];
      echo $pro_count ; 
      echo '<br>';
      $percentage = ($pro_count /6)*100;
      echo $percentage;

1 个答案:

答案 0 :(得分:5)

我认为您表中的值为空而不是null尝试

这将检查空值还是空值

$personal = mysqli_query($con,"

     SELECT   father_name,
     mother_name,
     DOB,
     adhar_no,
     address,
     religion,
     CASE WHEN father_name IS NOT NULL AND father_name<>'' THEN 1 ELSE 0 END + 
     CASE WHEN mother_name IS NOT NULL AND mother_name<>'' THEN 1 ELSE 0 END + 
     CASE WHEN DOB IS NOT NULL AND DOB<>'' THEN 1 ELSE 0 END + 
     CASE WHEN adhar_no IS NOT NULL AND adhar_no<>'' THEN 1 ELSE 0 END +
     CASE WHEN address IS NOT NULL AND address<>'' THEN 1 ELSE 0 END +
     CASE WHEN religion IS NOT NULL AND religion<>'' THEN 1 ELSE 0 END AS personal
     FROM  user WHERE `user_authtoken` = '$user_ath'

     ") or die(mysqli_error($con));