表显示空值而不是零

时间:2017-06-02 08:18:18

标签: php

我想制作这张表



!  ID   !   A  !    B   !   C   !   D   !   average   !
-------------------------------------------------------
! 01    !      !    0   !   10  !       !      5      !
! 02    !      !        !       !       !             !
! 03    !   0  !    0   !       !   0   !      0      !




我使用此功能



	
  if ($A == "" ) {$countA="0";} else { $countA = "1"; }
  if ($B == "" ) {$countB="0";} else { $countB = "1"; }
  if ($C == "" ) {$countC="0";} else { $countC = "1"; }
  if ($D == "" ) {$countD="0";} else { $countD = "1"; }
            
$sum		= ($A + $B + $C + $D);
$ceksum =($countA + $countB + $countC + $countD);

		if ($ceksum == "0") {
             $average="";
			} else { $average = $sum/$ceksum; }

and php that i use to call the data is

echo ($averageled4amatt4h=="")?'':round($averageled4amatt4h,1);




但是该表显示了这个结果..(当零为唯一值时,平均为空数据)



!  ID   !   A  !    B   !   C   !   D   !   average   !
-------------------------------------------------------
! 01    !      !    0   !   10  !       !      5      !
! 02    !      !        !       !       !             !
! 03    !   0  !    0   !       !   0   !             !




我该怎么办?我还在学习编码...

2 个答案:

答案 0 :(得分:0)

试试这个:

if ($ceksum == "0") {
         $average="0"; // <- change this line
        } else { $average = $sum/$ceksum; }

答案 1 :(得分:0)

修改此类型的行: if ($A == "" ) {$countA="0";} else { $countA = "1"; }

 $countA = !empty($A) ? "1" : 0;
 $countB = !empty($B) ? "1" : 0;
 $countC = !empty($C) ? "1" : 0;