Foreach循环嵌套3次麻烦

时间:2016-12-18 04:17:52

标签: php foreach

我正在尝试以下

每个屋顶测量的循环(fastbid_inspection_measurement_roof)行

然后Foreach行抓住roof_pitch然后对

执行foreach循环查询

fastbid_labor_tearoff其中pitch$Pitch['roof_pitch']

相同

然后在fastbid_labor_tearoff中获取该行匹配的结果并计算其成本。

现在正确

如果有一条记录它完美无缺,但是如果fastbid_inspection_meaasurement_roof内有两条记录,那么它将回显两个结果。我希望它能回显两个或所有记录的SUM TOTAL。

我哪里错了?

fastbid_inspection_measurement_roof计划

-------------------------------
| id | roof deck | roof_pitch |
-------------------------------
| 01 |    1359   |      5     |
-------------------------------
| 02 |    1211   |      6     |
-------------------------------

fastbid_labor_tearoff计划

---------------------------------------------
| id | labor_cost | ground_drop |   pitch  | 
---------------------------------------------
| 01 |    22.50   |      7       |    5    |
---------------------------------------------
| 02 |    23.50   |      7       |    6    |
---------------------------------------------

这是我的Foreach循环

$RoofDecksssss = "SELECT * FROM fastbid_inspection_measurement_roof WHERE Prospect_API = '".$prosapi."'";           
foreach ($pdo->query($RoofDecksssss) as $RoofDeck) { 
                 foreach ($RoofPitch as $Pitch) { 
                    $TearOff_Labor_PerPitch = "SELECT * FROM fastbid_labor_tearoff WHERE pitch = '".$Pitch['roof_pitch']."'";
                        foreach ($pdo->query($TearOff_Labor_PerPitch) as $TearOffCost) {
                            $LaborCost = $TearOffCost['labor_cost'];
                            $RoofDeckSQFT = $RoofDeck['roof_deck'];
                            $GroundDropCost = $TearOffCost['ground_drop'] * DeckSqFT_toRawSquare($RoofDeckSQFT);
                            $Total_Labor += $LaborCost *  DeckSqFT_toRawSquare($RoofDeckSQFT);

                            $TotalLaborTearOffCost = $GroundDropCost + $Total_Labor;

                            echo $TotalLaborTearOffCost;
                        }

                } 

            }

1 个答案:

答案 0 :(得分:1)

我认为如果您希望它正确汇总,您需要将此代码移到它当前的循环之外。

echo $TotalLaborTearOffCost;

你现在拥有它的方式,你的循环每次迭代都会获得1个输出。