duaeim2874 2014-11-12 02:14
浏览 51
已采纳

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

In table tb_usulan,I have a field like this:

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

I want to grouping data per name in my page, like this:

enter image description here

If i use this code:

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

It will be show the table like this:

enter image description here

So if there's anyone who can help me with this problem, I really appreciate it.

Thanks.

  • 写回答

1条回答 默认 最新

  • donglian4770 2014-11-12 03:06
    关注
    <?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++; }
    }?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用
  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启