gpt4 book ai didi

php - 如何自动循环每个组(类别)

转载 作者:行者123 更新时间:2023-11-30 22:44:15 27 4
gpt4 key购买 nike

伙计们,我想自动循环..但我不知道是我放错了 while 还是循环错了?

我的数据库

标签:kt_barang

    kd_kategori | nama_kategori
1 | A
2 | B
3 | C

标签:t_barang

kd_barang  | kategori 
11 | 1
12 | 1
13 | 2
14 | 3
15 | 3
16 | 3

查询表

CREATE TABLE kt_barang
(`kd_kategori` int, `nama_kategori` varchar(9))
;
INSERT INTO kt_barang
(`kd_kategori`, `nama_kategori`)
VALUES
(1, ' A '),
(2, ' B '),
(3, ' C ');

CREATE TABLE t_barang
(`kd_barang` int, `kategori` int)
;
INSERT INTO t_barang
(`kd_barang`, `pnaik`)
VALUES
('11', 1),
('12', 1),
('13', 2),
('14', 3),
('15', 3),
('16', 3)
;

我的代码

$arr=array();
$qw=mysql_query("select * from kt_barang",$con);
while($h=mysql_fetch_array($qw)){
$arr[]=$h['kd_kategori'];
}
$n = 0;
$c = count($arr);

while ($n < $c) {
$a = $arr[$n];

echo "kategori :".$a ."<br>";

$q=mysql_query("select * from t_barang where kategori='$a'",$con);


while($h=mysql_fetch_array($q)){
$daftar.='
<tr>
<td>'.$h['kd_barang'].'</td>';
$daftar.='
</tr>
';
}

$n++;
}


?>

<table class="table table-striped table-hover table-bordered" border="1">
<thead>
<tr>
<th style="text-align:center;" width="100">KODE</th>
</tr>
</thead>
<tbody>
<?php echo $daftar;?>
</tbody>
</table>

现在输出还是这样 table 还是一张..

kategori 1
kategori 2
kategori 3
---------------
Kode |
11 |
12 |
13 |
14 |
15 |
16 |
---------------

但是我想要这个输出伙计们..我希望表格显示三个,因为 kt_barang 有 3 个,

kategori 1
---------------
Kode |
11 |
12 |
---------------

kategori 2
---------------
Kode |
13 |
---------------

kategori 3
---------------
Kode |
14 |
15 |
16 |
---------------

我可能放错了 while 还是放错了循环?我不知道.. 你知道怎么修吗?非常感谢

最佳答案

一个查询就可以完成

$q=mysql_query("select a.kd_kategori,a.nama_kategori, b.kd_barang from kt_barang as a, t_barang as b where a.kd_kategori = b.kategori";);

关于php - 如何自动循环每个组(类别),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30124026/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com