dongyili5843 2015-05-09 08:13
浏览 16
已采纳

数组在PHP上的每个类别上仍然重复或重复

I've code, data on table has success display, but my problem on array.. i explain here, may you know solution for my code . thanks for help

//this code for get kategori 
$arr=array();
$qs=mysql_query("select * from kt_barang",$con);
while($s=mysql_fetch_array($qs)){
        $arr=$s['kd_kategori'];
}


$n = 0;
$c = count($arr);
while ($n < $c) {
   $a = $arr[$n];
?>
<table border="1">
<?php

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

echo '<tr>';
echo '<td style="width:30px;text-align:center;">'.$a.'</td>';  
echo '</tr>';

while($h=mysql_fetch_array($q)){
    $kd=$h['kd_barang'];
    $barang[]=array($h['kd_barang']);
    echo '<tr>';
    echo '<td style="width:30px;text-align:center;">'.$kd.'</td>';  
    echo '</tr>';
}
print_r($barang);

$n++;
}
?>
</table>

output now

//this output array $barang
Array ( [0] => Array ( [0] => 089686048704 ) [1] => Array ( [0] => 089686060027 ) [2] => Array ( [0] => 089686060065 ) 

//this output the table 
---------------------
kategori 66 
089686048704    
089686060027    
089686060065    
-----------------------
//this output array $barang still repeat from kategori 66
//This is the problem
Array ( [0] => Array ( [0] => 089686048704 ) [1] => Array ( [0] => 089686060027 ) [2] => Array ( [0] => 089686060065 ) [3] => Array ( [0] => 8996001524015 ) [4] => Array ( [0] => 8996001524039 ) [5] => Array ( [0] => 8996001524060 ) 
----------------------
kategori 67 
089686010015    
089686010046    
089686010077    
----------------------------

i want to second array no repeat.

//this output array $barang
Array ( [0] => Array ( [0] => 089686048704 ) [1] => Array ( [0] => 089686060027 ) [2] => Array ( [0] => 089686060065 ) )

//this output the table 
---------------------
kategori 66 
089686048704    
089686060027    
089686060065    
-----------------------

//this output array $barang
Array  ([3] => Array ( [0] => 8996001524015 ) [4] => Array ( [0] => 8996001524039 ) [5] => Array ( [0] => 8996001524060 ) )
----------------------
kategori 67 
089686010015    
089686010046    
089686010077    
----------------------------

how to make array like that from my code Thanks for your solution..

  • 写回答

1条回答 默认 最新

  • douwen0647 2015-05-09 08:47
    关注

    You need to reset the variable $barang before using it:

    $barang = array(); // <-- add this
    
    while($h=mysql_fetch_array($q)){
        $kd=$h['kd_barang'];
        $barang[]=array($h['kd_barang']);
        echo '<tr>';
        echo '<td style="width:30px;text-align:center;">'.$kd.'</td>';  
        echo '</tr>';
    }
    

    It is good practise to always set a variable to an defined state.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?