PHP - 从mysql为每个名称分组数据

时间:2014-11-12 02:14:51

标签: php mysql

在表格 tb_usulan 中,我有一个这样的字段:

id_usulan | name             | merk    | katalog
-------------------------------------------------
1         | Pipet 1 ml       | Pyrex   | 1234123
2         | Pipet 1 ml       | Example2| 1322134
3         | Alkohol 70%      | Brataco | 5938291
4         | Aquadest         | Amidis  | 9586830
5         | Alkohol 70%      | Example | 2312314

我想在我的页面中按名称分组数据,如下所示:

enter image description here

如果我使用此代码:

 $queryalat1="select * from tb_usulan Order by name ";
 $hasil = mysql_query($queryalat1);

      $no = 1;
      while ($data=mysql_fetch_array($hasil)){
      $id= $data['id_usulan'];

      echo" <tr style='color:#000000;'>
      <td align='center'><input type='checkbox' name='cek[]' value='$data[id_usulan]' ></td>
      <td align='center'>$no.</td>
      <td style='text-align:left; padding-left:5px;'>$data[name]</td>
      <td style='text-align:left; padding-left:5px;'>$data[merk]</td>
      <td style='text-align:left; padding-left:5px;'>$data[katalog]</td>

      <td align='center'><a href ='edit2.php?id=$id'>Edit</a>&nbsp;|&nbsp;<a href ='del2.php?id=$id'>Hapus</a></td>
      </tr>";
 $no++; }

将显示如下表格:

enter image description here

所以,如果有任何人可以帮我解决这个问题,我真的很感激。

感谢。

1 个答案:

答案 0 :(得分:1)

<?php $query1="select * from tb_usulan group by name ";
      $hasil = mysql_query($query1);
      $no = 1;
  while ($data1=mysql_fetch_array($hasil)){
      echo "<tr><td colspan=6>".$data1[name]."</td><tr>";
      $queryalat1="select * from tb_usulan where name like '%".$data1['name']."%' Order by merk ";
      $res = mysql_query($queryalat1);
          while ($data=mysql_fetch_array($res)){
          $id= $data['id_usulan'];
          echo" <tr style='color:#000000;'>
          <td align='center'><input type='checkbox' name='cek[]' value='$data[id_usulan]' ></td>
          <td align='center'>$no.</td>
          <td style='text-align:left; padding-left:5px;'>$data[name]</td>
          <td style='text-align:left; padding-left:5px;'>$data[merk]</td>
          <td style='text-align:left; padding-left:5px;'>$data[katalog]</td>
          <td align='center'><a href ='edit2.php?id=$id'>Edit</a>&nbsp;|&nbsp;<a href ='del2.php?id=$id'>Hapus</a></td>
          </tr>";
   $no++; }
}?>