サブグループの foreach の場合は次のとおりです。
$result = mysql_query("select group1, , group2, code, name, price1, price2, sumprice, persentage FROM table") or die(mysql_error());
$set = array();
while ($record = mysql_fetch_object($result)) {
$set[$record->group1][$record->group2][]= $record;
}
foreach ($set as $group1 => $record1) {
echo "<tr><td></td>
<td align='center'>{$group1}</font></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>";
foreach ($record1 as $group2 => $record2 ) {
echo "<tr><td></td>
<td>{$group2}</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>";
foreach ($record2 as $name) {
echo"<tr><td>{$name->code}</td>
<td>{$name->name}</td>
<td align='right'>".number_format($name->price1, 0, ',', '.')."</td>
<td align='right'>".number_format($name->price2, 0, ',', '.')."</td>
<td align='right'>".number_format($name->sumprice, 0, ',', '.')."</td>
<td align='center'>".number_format($name->percentage, 0, ',', '.')."%</td>
</tr>";
}
$group2_price1= 0;
$group2_price2 = 0;
$group2_sumprice = 0;
$group2_percentage = 0;
foreach($record2 as $group2){
$group2_price1 += $group2->price1;
$group2_price2 += $group2->price2;
$group2_sumprice += $group2->sumprice;
$group2_percentage += $group2->percentage;
}
echo "<tr><td></td><td>Summary of {$group2}</td>
<td align='right'>".number_format($group2_price1 , 0, ',', '.')."</td>
<td align='right'>".number_format($group2_price2 , 0, ',', '.')."</td>
<td align='right'>".number_format($group2_sumprice , 0, ',', '.')."</td>
<td align='center'>".number_format($group2_percentage , 0, ',', '.')."%</td>
</tr>";
}
$group1_price1= 0;
$group1_price2 = 0;
$group1_sumprice = 0;
$group1_percentage = 0;
foreach($record1 as $group1){
$group1_price1 += $group1->price1;
$group1_price2 += $group1->price2;
$group1_sumprice += $group1->sumprice;
$group1_percentage += $group1->percentage;
}
echo "<tr><td></td><td>Jumlah {$group1}</td>
<td align='right'>".number_format($group1_price1 , 0, ',', '.')."</td>
<td align='right'>".number_format($group1_price2 , 0, ',', '.')."</td>
<td align='right'>".number_format($group1_sumprice , 0, ',', '.')."</td>
<td align='center'>".number_format($group1_percentage , 0, ',', '.')."%</td>
</tr>";
}
エラー :
Notice: Trying to get property of non-object in (
foreach($record1 as $group1){
$group1_price1 += $group1->price1;
$group1_price2 += $group1->price2;
$group1_sumprice += $group1->sumprice;
$group1_percentage += $group1->percentage;
}
).
この問題を解決するにはどうすればよいですか?
よろしくお願いします。