Babaji Babaji - 4 months ago 9
PHP Question

How to do formatting using CSS to table

I am trying to design the table but it seems coming into proper way but for some cases. In this table, that $cal_amount and $cal_amount_key part shouldbe print whole for any numbers of cars.

Screenshot

Code:

PHP

<?php

$TotV=array("car","bus","jeep");
$Nopkg=array(759,159,459);
$Avg=array(20,50,15);
$Qty=array(15000,58000,58922);
$rate=array(31.73,17.15,17.25);
$gro_tot=789456;
$Cex=781254;
$S_Tot=159254;
$Vat=12500;
$frieght=0;
$G_Tot=985251;


?>


CSS

table,td,th,tr {
border:1px solid black;
border-collapse: collapse;
text-align:left;
}


HTML

<table>
<tr>
<th>S.No</th><th align="center">Variety of Goods</th><th style="width:70px">No.and Description of<br/>Packages</th><th style="width:70px">Avg. Contents per Package<br/>(Pcs)</th><th style="width:70px">Total Quantity<br/>(Pcs)</th><th style="width:70px">Rate per pc.</th><th colspan="2" style="text-align:center;">Amount<br/>(Rs.)</th>
</tr>
<?php
for($i=0;$i<count($TotV);$i++)
{?>
<tr>
<td><?php echo $i+1;?></td>
<td><?php echo strtoupper($TotV[$i]);?></td>
<td><?php echo $Nopkg[$i];?></td>
<td><?php echo $Avg[$i];?></td>
<td><?php echo $Qty[$i];?></td>
<td><?php echo $rate[$i];?></td>

<?php
}?>

<td colspan="1">Total Value</td><td style="width:100px;text-align:right;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $gro_tot;?></td>
<tr><td></td><td></td>
<td></td><td></td><td></td><td></td><td style="width:150px;">C.Ex.Duty &nbsp;&nbsp;&nbsp; 12.5%</td><td style="width:150px;text-align:right;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <?php echo $Cex;?></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td></td><td style="width:150px;">SubTotal</td><td id="num" style="width:150px;text-align:right;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $S_Tot;?></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td style="width:150px;">VAT/CST &nbsp;&nbsp;&nbsp; 2%</td><td style="width:150px;text-align:right;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $Vat;?></td></tr>
<tr><td style="border:none;" colspan="3"><b>qwert</b></td><td style="border:none;"></td><td style="border:none;" colspan="2"><td><b>Freight</b></td><td style="width:150px;text-align:right;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<STRONG><?php echo $frieght;?></STRONG></td></tr>
<tr><td style="border:none;" colspan="3"><b>asdf</b></td><td style="border:none;"></td><td style="border:none;" colspan="2"><td><b>Grand Total</b></td><td style="width:150px;text-align:right;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<STRONG><?php echo $G_Tot;?></STRONG></td></tr>
</tr>
</table>


Please suggest some changes to be done in code so that It will start from first row instead of third row. Thanks in advance.

Answer

Try this:

First make PHP array for total calculation and label.

$TotV=array("car","bus","jeep");
$Nopkg=array(759,159,459);
$Avg=array(20,50,15);
$Qty=array(15000,58000,58922);
$rate=array(31.73,17.15,17.25);

$gro_tot=789456;
$Cex=781254;    
$S_Tot=159254;
$Vat=12500;
$frieght=0;
$G_Tot=985251;
$cal_amount_key = array('Total Value',
        'C.Ex.Duty &nbsp;&nbsp;&nbsp; 12.5%',
        'SubTotal',
        'VAT/CST &nbsp;&nbsp;&nbsp; 2%',
        'Freight',
        'Grand Total');
$cal_amount = array($gro_tot,
        $Cex,
        $S_Tot,
        $Vat,
        $frieght,
        $G_Tot);

Put the code inside loop:

<table>
    <tr>
        <th>S.No</th>
        <th align="center">Variety of Goods</th>
        <th style="width:70px">No.and Description of<br/>Packages</th>
        <th style="width:70px">Avg. Contents per Package<br/>(Pcs)</th>
        <th style="width:70px">Total Quantity<br/>(Pcs)</th>
        <th style="width:70px">Rate per pc.</th>
        <th colspan="2" style="text-align:center;">Amount<br/>(Rs.)</th>
    </tr>
    <?php
     for($i=0;$i<count($TotV);$i++) {?>
        <tr>
        <td><?php echo $i+1;?></td>
        <td><?php  echo strtoupper($TotV[$i]);?></td>
        <td><?php echo $Nopkg[$i];?></td>
        <td><?php echo $Avg[$i];?></td>
        <td><?php echo  $Qty[$i];?></td>
        <td><?php echo $rate[$i];?></td>
        <td><?php echo $cal_amount_key[$i];?></td>
        <td style="width:100px;text-align:right;"><?php echo $cal_amount[$i];?></td>
        </tr>
      <?php } ?>
    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td style="width:150px;">VAT/CST &nbsp;&nbsp;&nbsp; 2%</td>
        <td  style="width:150px;text-align:right;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $Vat;?></td>
    </tr>
    <tr>
        <td style="border:none;" colspan="3"><b>qwert</b></td>
        <td style="border:none;"></td>
        <td style="border:none;" colspan="2">
        <td><b>Freight</b></td>
        <td style="width:150px;text-align:right;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<STRONG><?php echo $frieght;?></STRONG></td>
    </tr>
    <tr>
        <td style="border:none;" colspan="3"><b>asdf</b></td>
        <td style="border:none;"></td>
        <td style="border:none;" colspan="2">
        <td><b>Grand Total</b></td>
        <td style="width:150px;text-align:right;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<STRONG><?php echo $G_Tot;?></STRONG></td>
    </tr>
</table>