猿问

统计数据库中特定数据的数量,如果有空白或空则显示0。PHP

我正在尝试计算特定级别的学生人数。在下拉选择中,您应该会看到该级别的学生人数。


在数据库中,目前有 3 名学生。2个是新手,1个是公开冠军。当我第一次编写代码并回显计数时,它将正确显示为新手 (2) 和公开冠军 (1)。然而,其余的都像这样显示为空白:初学者()。


我调整了我的代码以包含一个,if statement以便如果该级别没有学生,它将显示为 (0)。不过这次的问题是,当它应该是 2 和 1 时,一切都变成 (0),包括新手和公开冠军。


有人能告诉我我做错了什么吗?


代码:


<div class="col-sm-2 " style="text-align: right;">

              <select class="level">

                 <option value="all">All Levels</option>

                  <?php

                  $lvlnum = "SELECT * FROM `dancers` WHERE school = '$ownerSchool'";

                  $lvlres = mysqli_query($con,$lvlnum);




                  while($lvlrow = mysqli_fetch_array($lvlres)){

                      $placement[] = $lvlrow['current_lvl'];

                  }


                  $placeNum = array_count_values($placement);

                  $preBeginner = $placeNum["Pre Beginner"];

                  if($preBeginner = '' || $preBeginner = 'null'){

                      $preBeginner = 0;

                  }

                  $beginner = $placeNum["Beginner"];

                  if($beginner = '' || $beginner = 'null'){

                      $beginner = 0;

                  }

                  $novice = $placeNum["Novice"];

                  if($novice = '' || $novice = 'null'){

                      $novice = 0;

                  }

                  $prizeWinner = $placeNum["Prize Winner"];

                  if($prizeWinner = '' || $prizeWinner = 'null'){

                      $prizeWinner = 0;

                  }

                  $prelim = $placeNum["Prelim Champion"];

                  if($prelim = '' || $prelim = 'null'){

                      $prelim = 0;

                  }

                  $open = $placeNum["Open Champion"];

                  if($open = '' || $open = 'null'){

                      $open = 0;

                  }


烙印99
浏览 226回答 2
2回答

神不在的星期二

空字符串和空字符串与空值不同。三元运算符(又名内联 IF)可能会有所帮助:$preBeginner&nbsp;=&nbsp;isset($placeNum["Pre&nbsp;Beginner"])&nbsp;?&nbsp;$placeNum["Pre&nbsp;Beginner"]&nbsp;:&nbsp;0;

摇曳的蔷薇

也许你可以使用这个:<div class="col-sm-2 " style="text-align: right;">&nbsp; &nbsp; <select class="level">&nbsp; &nbsp; &nbsp; &nbsp;<option value="all">All Levels</option>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $lvlnum = "SELECT current_lvl as Placement, Count(Your Primary Key) as DancersOnPlacement FROM `dancers` WHERE school = '$ownerSchool'";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $lvlres = mysqli_query($con,$lvlnum);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while($lvlrow = mysqli_fetch_array($lvlres))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $Placement = $lvlrow['Placement'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $Count = $lvlrow['DancersOnPlacement'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<option value='".$Placement."'>".$Placement." (".$Count.")</option>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; </select></div>
随时随地看视频慕课网APP
我要回答