0

我正在获取 $_post 数组并在每次迭代时运行一个查询,然后尝试获取累积的总点数,它似乎用最后一个点迭代覆盖了总数。我该如何解决?

  $full_total = 0;
    foreach($postid as $key => $value){

      $array = explode(',', $value);

      if($value[0]!=''){
        $id = $array[0];
        $query = "SELECT * FROM products WHERE id = '$id'";
        $result = mysqli_query($dbc, $query);

          while ($row = mysqli_fetch_array($result)) {
            echo '<tr valign="bottom">';
            echo '<td>' . stripslashes($row['rangeCode']) . '-' . stripslashes($row['pointsType']) . '</td>';
            echo '<td>' . stripslashes($row['category']) . '</a></td>';
            echo '<td>' . stripslashes($row['itemDesc']) . '</a></td>';
            echo '<td class="middle">' . stripslashes($row['points']) . '</a></td>';
            echo '</tr>';
            $total_donations = $row['points'];
          }
      }
      }
  $full_total += $total_donations;
  echo $full_total;
4

1 回答 1

2

您必须$full_total像这样在 foreach 循环中插入

  $full_total = 0;
foreach($postid as $key => $value){

  $array = explode(',', $value);

  if($value[0]!=''){
    $id = $array[0];
    $query = "SELECT * FROM products WHERE id = '$id'";
    $result = mysqli_query($dbc, $query);

      while ($row = mysqli_fetch_array($result)) {
        echo '<tr valign="bottom">';
        echo '<td>' . stripslashes($row['rangeCode']) . '-' . stripslashes($row['pointsType']) . '</td>';
        echo '<td>' . stripslashes($row['category']) . '</a></td>';
        echo '<td>' . stripslashes($row['itemDesc']) . '</a></td>';
        echo '<td class="middle">' . stripslashes($row['points']) . '</a></td>';
        echo '</tr>';
        $full_total += $row['points'];
      }
  }
  }
 echo $full_total;
于 2013-03-07T14:54:51.987 回答