【问题标题】:Avoiding Cummulative addition in my result在我的结果中避免累积加法
【发布时间】:2012-03-07 07:40:45
【问题描述】:

我有这段代码在一个两行表上执行一个while循环,其中每一行都乘以其他对应的值。我遇到的问题是 $grade_point 显示每行的所有相乘值并将它们累加。但我实际上需要相乘值的总和。

$grade_point = 0;
while ($row8 = mysql_fetch_assoc($query8)) {
  if ($row8['score'] >= 70) {
    $score = 5;
  }elseif ($row8['score'] >= 60) {
    $score = 4;
  }elseif ($row8['score'] >= 50) {
    $score = 3;
  }elseif ($row8['score'] >= 45) {
    $score = 2;
  }elseif($row8['score'] >= 40) {
    $score = 1;
  }else{
    $score = 0;
  }
  $grade_point += $score * $row['course_unit'];
  echo "$grade_point";
}

【问题讨论】:

  • 移动 echo $grade_point;在循环之外

标签: php loops while-loop sum


【解决方案1】:

您在循环内回显;)如果您只想查看最终结果,则需要在循环外回显值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2016-03-14
    • 2012-11-08
    • 2013-07-18
    相关资源
    最近更新 更多