3

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.

2
  • 1
    number of <td>s in each row(<tr>) must be the same. If one row has 5 tds, next row must match. You could also have 3 tds, and 1 td with colspan=2, it is still 5 in total. Follow this rule.
    – Muhammed
    Commented Mar 7, 2016 at 6:43
  • @MuhammedM.Thanks for your advice, but problem didn't solved.
    – Babaji
    Commented Mar 7, 2016 at 6:55

1 Answer 1

2

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>
2
  • Hey.I am facing problem now.If there are two cars, then it will only print gro_tot and $Cex.It should print all the remaining details.Hope you understand.
    – Babaji
    Commented Jul 26, 2016 at 7:23
  • Even if there are three cars or six cars, $cal_amount and $cal_amount_key arrays should be print whole in any cases.
    – Babaji
    Commented Jul 26, 2016 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.