简体   繁体   English

如何从MySQL的三个级别中将PHP分组

[英]How to php subgroup in three level from mysql

Here are my case with foreach for subgroup : 这是我的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>";
    }

Error : 错误:

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;
        }
).

How to solve this problem, how should I do ? 如何解决这个问题,我该怎么办?

Thanks for advance. 感谢前进。

I have solved it, 我已经解决了

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

done ! 做完了!

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM