MCIT Trends
MCIT Trends

Reputation: 281

PHP array data to be printed in HTML Table

I have an array, which includes record set generated by codeigniter model.

I tried to print those records into a HTML table using my view. My view as follows:

<html>
<head>
    <title>Fuel Order:</title>
</head>
<body onload="window.print()">
<body>
<div class="col-xs-12 table-responsive">
<table class="table table-bordered"  style="font-size: 11px; ">
<tbody>
<?php
    if (!empty($printData)) {
        foreach ($printData as $item) {
?>
            <tr>        
                <td><p style="position: absolute;top: 20cm;right: 14cm"><?=$item->item_name?></p></td>
                <td><p style="position: absolute;top: 20cm;right: 11.5cm"><?=$item->fuel_qty?> Litres</p></td>
                <td><p style="position: absolute;top: 20cm;right: 6.5cm"><?=$this->amount_word?></p></td>                           
            </tr>           
</tbody>
</table>
</div>

<?php   
        }
    }
?>
</body>
</html> 

The view is working fine, except the records overprint each other.

Upvotes: 0

Views: 422

Answers (3)

mplungjan
mplungjan

Reputation: 177940

If you have more than one record you need to vary the top: by the height of the set of values or height of preprinted grid

Also make sure the loop is closed BEFORE the </tbody>

<table class="table table-bordered"  style="font-size: 11px; ">
<tbody>
<?php
if (!empty($printData)) {
  $offset=2; // cm
  $cnt=0;
  foreach ($printData as $item) {
?>
<tr>        
  <td><p style="position: absolute;top: <?= ($cnt*$offset)+20 ?>cm;right: 14cm"><?=$item->item_name?></p></td>
  <td><p style="position: absolute;top: <?= ($cnt*$offset)+20 ?>cm;right: 11.5cm"><?=$item->fuel_qty?> Litres</p></td>
  <td><p style="position: absolute;top: <?= ($cnt*$offset)+20 ?>cm;right: 6.5cm"><?=$this->amount_word?></p></td>                           
</tr> 
<? $cnt++; 
  } // close your foreach HERE
  ?>
</tbody>

Upvotes: 3

MMDM
MMDM

Reputation: 465

I believe you should close your foreach loop inside the table, but currently you closed it outside of it. Also put your if statement before table tag and ... (you have 2 body tag here!). It is better to use your statements like codes below (With : character to not been confused), remove position: absolute because it make your element(s) to have new coordinates according to the parent with non static position (If you need to move it set position: relative to them because it move your element according to its parent). As conclusion:

<html>
<head>
    <title>Fuel Order:</title>
</head>
<body onload="window.print()">
<div class="col-xs-12 table-responsive">
    <?php if (!empty($printData)): ?>
        <table class="table table-bordered" style="font-size: 11px;">
            <tbody>
            <?php foreach ($printData as $item): ?>
                <tr>
                    <td><p><?= $item->item_name ?></p></td>
                    <td><p><?= $item->fuel_qty ?> Litres</p></td>
                    <td><p><?= $this->amount_word ?></p></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>
</body>
</html>

I hope it helps :)

Upvotes: 0

LegendofPedro
LegendofPedro

Reputation: 1414

Remove the style attributes from the <p> tags; they can't all have identical positions.

Set the position of the table, instead.

Upvotes: 1

Related Questions