0

我正在遍历一个带有循环的表,以将所有结果返回到一个漂亮的表中。直到子查询需要执行之前它都做得很好。子查询查看另一个表并使用起始循环中的 $office 值来查找信息,从那里它只是汇总整数值并将其分配给 $paid。

由于某种原因,两个循环在返回第一个结果并输出 $paid 后都会停止。甚至有可能完成我正在做的事情吗?我试图解决这个问题,似乎我可以对值进行硬编码并为每个结果集设置单独的变量,但这似乎需要做很多工作。我只想显示它从第一个循环返回的每个结果集的 $paid 整数。希望这一切都有意义。我非常感谢给出的任何建议。

$mysqli = new mysqli ( $db_hostname, $db_username, $db_password, $db_name );
    $query = "select * from `drawer` where date(`date`) = '$mysql_date' order by `office`"; // select result set for selected date and order by office name
    $result = $mysqli->query($query);
    while ( $row = $result->fetch_assoc () ) {

echo "<tr class=table_border>\n"; //display results
echo "<td class=table_rows nowrap width=10%>&nbsp;".$row['office']."</td>\n"; // return office name
$office = $row['office']; // grab the office from the first loop to use it in the second loop
echo "<td class=table_rows nowrap width=10%>&nbsp;".$row['manager']."</td>\n"; // return manager name
echo "<td class=table_rows nowrap width=10%>&nbsp;".$row['scash']."</td>\n"; // display integer value
echo "<td class=table_rows nowrap width=10%>&nbsp;".$row['ecash']."</td>\n"; // display integer value

$newquery = "select * from `offer_det` where `office` = '$office' and date(date) = curdate()"; // subquery to display data from another table and insert for each row when the first loop starts
$resultquery = $mysqli->query($newquery);
while ($row=$resultquery->fetch_assoc()) {
    $paid += $row['paid'];
}
echo "<td class=table_rows nowrap width=10%>&nbsp;".$paid."</td>\n"; // both loops end here
echo "<td class=table_rows nowrap width=10%>&nbsp;".$row['add']."</td>\n"; // should return integer value
echo "<td class=table_rows nowrap width=10%>&nbsp;".$row['remove']."</td>\n"; // should return integer
echo "<td class=table_rows nowrap width=10%>&nbsp;".$row['total']."</td>\n"; // should return integer
}
4

2 回答 2

0

您正在覆盖$row内部 while 循环

改变

while ($row=$resultquery->fetch_assoc()) {
    $paid += $row['paid'];
}

while ($row1=$resultquery->fetch_assoc()) {
    $paid += $row1['paid'];
}
于 2012-12-06T16:16:19.640 回答
0

您在内部 while 循环中分配 $row - 它已经分配给外部循环。尝试在内部循环中将其更改为不同的变量名,

while ($row1=$resultquery->fetch_assoc()) {
    $paid += $row1['paid'];
}
于 2012-12-06T16:16:54.447 回答