Home > Enterprise >  How to Dynamically display this kind of data from an Array in PHP
How to Dynamically display this kind of data from an Array in PHP

Time:06-03

sample image

$branch = [
 '7232' => [
        'name' => 'United State Branch',
        'branch_id' => 7232
 ],
 '5431' => [
        'name' => 'Brazil Branch',
        'branch_id' => 5431
 ];

On each loop of the branch i want to get the branch rates data and display this to align with the sample image. (Just the display is what i need, and can't just figure a way to align the html well on each loop.

SAMPLE DATA OF RATES after getting it from DB

$getRate = [
     '0' => [
            'kg' => '1',
            'amount' => 50,
            'branch_id' => 7232
     ],
     '1' => [
            'kg' => '2',
            'amount' => 110,
            'branch_id' => 7232
     ];

Please Community, how can one align this well according to the attached image?? Thanks

CodePudding user response:

It is necessary to assemble a new nesting data structure which will allow you to get the required data in the correct order. An example of collecting such a structure:

// kg => branch_id => item
$indexedItems = [];

foreach ($getRate as $item) {
    $kg = $item['kg'];
    $branchId = $item['branch_id'];

    $indexedItems[$kg][$branchId] = $item;
}

Then the resulting structure can be used to build, for example, a table:

<table>
  <thead>
    <tr>
      <?php foreach ($branch as $column): ?>
        <th><?= $column['name'] ?></th>
      <?php endforeach ?>
    </tr>
  </thead>
  <tbody>
    <?php foreach ($indexedItems as $row): ?>
      <tr>
        <?php foreach ($branch as $column): ?>
          <?php $item = $row[$column['branch_id']]; ?>
          <td><?= $item['kg'] ?>kg - $<?= $item['amount'] ?></td>
        <?php endforeach ?>
      </tr>
    <?php endforeach ?>
  </tbody>
</table>

CodePudding user response:

Thanks all, am very grateful to all contribution and according to @7-zete-7. Followed same procedure and was able to resolve it. It was a bit tricky for me.

Below is a full replica of the code;

// kg => branch_id => item
$indexedItems = [];


<thead>
    <tr>
        <?php foreach(GLOBALS::init('lh')['branch'] as $value) :?>
            <th><?php echo $value['name'];?></th>

            <?php 
                $getRate = getRatesByBranch($value['branch_id']);
                $r_currency = getCurrencyData($value['currency'])['symbol'];
            ?>

            <?php foreach($getRate as $key => $item) :?> 
                <?php
                    $item['currency'] = $r_currency;
                    $indexedItems[$key][$item['branch_id']] = $item; 
                ?>
            <?php endforeach;?>

        <?php endforeach;?>
    </tr>
</thead>

<tbody>
    <?php foreach ($indexedItems as $row): ?>
        <tr>
            <?php foreach (GLOBALS::init('lh')['branch'] as $column): ?>

                <?php 
                    $item = NULL;
                    if(isset($row[$column['branch_id']]))
                        $item = $row[$column['branch_id']]; 
                ?>
                
                <?php if(!is_null($item)) :?>
                    <td><?= $item['kg'] ?>kg - <?= "{$item['currency']} {$item['amount']}" ?></td>
                <?php endif;?>
                
            <?php endforeach ?>
            
        </tr>
    <?php endforeach ?>
</tbody>

Thank you all once again and Happy to be here!!!

  • Related