2013-03-07 2 views
0

$ _post 배열을 가져오고 각 반복마다 쿼리를 실행하고 전체 점수를 얻으려고하면 마지막 점 반복으로 전체를 덮어 쓰는 것 같습니다. 이 문제를 어떻게 해결할 수 있습니까?foreach 루프 + mysql while 루프에서 총 가져 오기

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

답변

2

당신은 0으로 그것은 다른 곳에서는 사용하지 total_donatinos 완전히 $의 필요성을 줄여이

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

같은 foreach 루프에서 $full_total을 삽입해야합니다) – ThaMe90

+0

그래, 당신 ' 맞아, 내가 대답을 편집 – rsz

+0

와우, 나는 그것을 시도했지만 확신 완벽하게 작동합니다. 정말 고마워! – Grant