gpt4 book ai didi

php - 从 foreach 循环 + mysql while 循环获取总计

转载 作者:行者123 更新时间:2023-11-29 08:35:45 25 4
gpt4 key购买 nike

我正在获取 $_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;

最佳答案

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

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

关于php - 从 foreach 循环 + mysql while 循环获取总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15274274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com