My question is referring to my previous question in this link How to echo specific multiple same records only once using foreach loops in PHP? .
This is my current code
Code :
$sqlTXT = "SELECT * FROM TABLE";
$arr_old = DB::getInstance()->FetchArray($sqlTXT);
if(count($arr_old) > 0)
{
$arr = array();
foreach($arr_old as $key => $item)
{
if(!array_key_exists($item['ACCOUNT_NUMBER'], $arr))
{
$arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['ACCOUNT'] = $item['ACCOUNT'];
$arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['CATEGORY'] = $item['CATEGORY'];
$arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['VALUE'] = $item['VALUE'];
$arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['FUND'] = $item['FUND'];
$arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['AMOUNT'] = $item['AMOUNT'];
}
else
{
$arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['CATEGORY'] .= ",".$item['CATEGORY'];
$arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['VALUE'] .= ",".$item['VALUE'];
}
}
ksort($arr, SORT_NUMERIC);
echo "<table>";
echo "<tr>";
echo "<td>ACCOUNT</td>";
echo "<td>CATEGORY</td>";
echo "<td>VALUE</td>";
echo "<td>FUND</td>";
echo "<td>AMOUNT</td>";
echo "</tr>";
foreach($arr as $key => $item)
{
// Display Category
$xpl = explode(",",$item[$key]['CATEGORY']);
$n_category = "";
foreach($xpl as $b => $a){
$n_category .= ($b!=0) ? "<br>".$a : $a ;
}
// Display Value
$trl = explode(",",$item[$key]['VALUE']);
$n_value = "";
foreach($trl as $c => $d){
$n_value .= ($c!=0) ? "<br>".$d : $d ;
// $new = number_format($n_value, 2, '.', ',');
}
echo "<tr>";
echo "<td>".$item[$key]['ACCOUNT']."</td>";
echo "<td>".$n_category."</td>";
echo "<td>".$new."</td>";
echo "<td>".$item[$key]['FUND']."</td>";
echo "<td>".$item[$key]['AMOUNT']."</td>";
echo "</tr>";
}
echo "</table>";
}
And the output as shown as below.
Output :
ACCOUNT CATEGORY VALUE FUND AMOUNT
0001 Category1 10000 BIN 300,000.00
Category2 0
Category3 500
Category4 15000
0002 Category1 8500 BIN 70,000.00
Category2 7000
Category3 100
Category4 0
But my current issue is I can't convert the VALUE column into 2 decimals format. When I added code
$new = number_format($n_value, 2, '.', ',');
The output only shows the first value of array.
I hope that someone can help me to solve this issue.
Thanks in advance.
You need to convert each value in the column independently. Try changing your foreach loop to this:
foreach($trl as $c => $d){
if (is_numeric($d)) $d = number_format($d, 2, '.', ',');
$n_value .= ($c!=0) ? "<br>".$d : $d ;
}