0

这是我对子组 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;
        }
).

如何解决这个问题,我该怎么办?

感谢提前。

4

1 回答 1

0

我已经解决了,

foreach($record1 as $group1 => $value){
  foreach($value as $group1a){
  $group1_price1 += $group1a->price1;
  $group1_price2 += $group1a->price2;
  $group1_sumprice += $group1a->sumprice;
  $group1_percentage += $group1a->percentage;
}
}

完毕 !

谢谢

于 2013-03-05T10:13:47.223 回答