Reputation: 527
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..
Upvotes: 0
Views: 57
Reputation: 4711
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.
Upvotes: 2