0

I am getting the $_post array and running a query on each iteration, then trying to get the total points accummulated, it seems to be overwriting the total with the last point iteration. How do I fix this?

  $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;

1 Answer 1

2

You have to insert the $full_total in the foreach loop like this

  $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;
Sign up to request clarification or add additional context in comments.

3 Comments

Which completely reduces the need of $total_donatinos to 0. It isn't used anywhere else ;)
Wow, I was certain I tried that but it works perfectly. Thanks so much!
Any idea how I can speed up or optimise the query above seems to be very slow?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.