0

my table sap rfc

how can I add number in front of ID? if ID contain 3 digits it will add 000 in front of ID, if ID contain 4 digits it will add 00 in front of ID, if ID contain 5 digits it will add 0 in front of ID. For an example

  ID   |   FEE   |      NAME     |   PERIOD
000711 |  204000 |  YUDI MANDALA |   201807
000790 |   84000 |  AGUS WAHYUDI |   201807
001171 |  151500 |    SARJANA    |   201807

my code:

if ($FI_HPs!=0) 
{
 $array = [];
 for ($i=1;$i<=$FI_HPs;$i++) 
 {
   $FI_HP = saprfc_table_read ($fce,"FI_HP",$i);  
   $id = $FI_HP['ID'];

   if(isset($array[$id])) 
   {    
     $array[$id]['FEE'] += $FI_HP['FEE'];
   } 
    else 
   { 
       $array[$id] = $FI_HP;
   }
 }

 foreach($array as $item) 
 {
    echo '<tr>
    <td>'.$item['ID'].'</td>
    <td>'.($item['FEE']*100).'</td>
    <td>'.$item['NAME'].'</td>
    <td>'.$item['PERIOD'].'</td>
    </tr>';
 }
}
jimmy
  • 19
  • 3

2 Answers2

1

you could use str_pad

foreach($array as $item) 
 {
    echo '<tr>
    <td>'.str_pad($item['ID'], 6, "0", STR_PAD_LEFT) .'</td>
    <td>'.($item['FEE']*100).'</td>
    <td>'.$item['NAME'].'</td>
    <td>'.$item['PERIOD'].'</td>
    </tr>';
 }
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

$base = 100000 ;

'. substr($base+$item['ID'],1) .'

behzad m salehi
  • 1,038
  • 9
  • 23