Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中计算表中的小计和总计?_Php_Loops_Html Table - Fatal编程技术网

如何在PHP中计算表中的小计和总计?

如何在PHP中计算表中的小计和总计?,php,loops,html-table,Php,Loops,Html Table,我的剧本是这样的: <?php $data = array( array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '002', 'quantity' => '2','cost' => '2000'),

我的剧本是这样的:

<?php
    $data = array(
        array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '002', 'quantity' => '2','cost' => '2000'),
        array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '004', 'quantity' => '3','cost' => '2000'),
        array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '005', 'quantity' => '4','cost' => '2000'),
        array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '006', 'quantity' => '5','cost' => '2000'),
        array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '008', 'quantity' => '1','cost' => '2000'),
        array('transaction_number' => 'AB-0002','date' => '2018-08-02', 'item_number' => '0101010', 'desc' => 'This is b', 'variant_code' => '013', 'quantity' => '2','cost' => '2000'),
        array('transaction_number' => 'AB-0002','date' => '2018-08-02', 'item_number' => '0101010', 'desc' => 'This is b', 'variant_code' => '020', 'quantity' => '3','cost' => '2500'),
        array('transaction_number' => 'AB-0002','date' => '2018-08-02', 'item_number' => '0101010', 'desc' => 'This is b', 'variant_code' => '022', 'quantity' => '4','cost' => '2500'),
        array('transaction_number' => 'AB-0003','date' => '2018-08-03', 'item_number' => '0101010', 'desc' => 'This is c', 'variant_code' => '007', 'quantity' => '1','cost' => '2500'),
        array('transaction_number' => 'AB-0003','date' => '2018-08-03', 'item_number' => '0101010', 'desc' => 'This is c', 'variant_code' => '015', 'quantity' => '7','cost' => '2500')
    );
?>
<table>
    <tr>
        <th>transaction_number</th>
        <th>date</th>
        <th>item_number</th>
        <th>desc</th>
        <th>variant_code</th>
        <th>quantity</th>
        <th>cost</th>
    </tr>
    <?php
    foreach($data as $key=>$value) {
    ?>
    <tr>
        <td><?php echo $value['transaction_number'] ?></td>
        <td><?php echo $value['date'] ?></td>
        <td><?php echo $value['item_number'] ?></td>
        <td><?php echo $value['desc'] ?></td>
        <td><?php echo $value['variant_code'] ?></td>
        <td><?php echo $value['quantity'] ?></td>
        <td><?php echo $value['cost'] ?></td>
    </tr>
    <?php 
    }
    ?>
</table>

交易编号
日期
项目编号
描述
变体代码
量
费用
如果执行脚本,结果如下所示:

我想得出这样的结果:


我该怎么做呢?

要实现您的目标,可能有几种方法可以做到这一点-我选择使用数组来存储新发现的事务编号和日期。通过测试项目是否已在数组中,您可以声明新的变量/对象,这些变量/对象随后将在渲染输出中使用

<?php
    $data = array(
        array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '002', 'quantity' => '2','cost' => '2000'),
        array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '004', 'quantity' => '3','cost' => '2000'),
        array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '005', 'quantity' => '4','cost' => '2000'),
        array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '006', 'quantity' => '5','cost' => '2000'),
        array('transaction_number' => 'AB-0001','date' => '2018-08-01', 'item_number' => '0101010', 'desc' => 'This is a', 'variant_code' => '008', 'quantity' => '1','cost' => '2000'),
        array('transaction_number' => 'AB-0002','date' => '2018-08-02', 'item_number' => '0101010', 'desc' => 'This is b', 'variant_code' => '013', 'quantity' => '2','cost' => '2000'),
        array('transaction_number' => 'AB-0002','date' => '2018-08-02', 'item_number' => '0101010', 'desc' => 'This is b', 'variant_code' => '020', 'quantity' => '3','cost' => '2500'),
        array('transaction_number' => 'AB-0002','date' => '2018-08-02', 'item_number' => '0101010', 'desc' => 'This is b', 'variant_code' => '022', 'quantity' => '4','cost' => '2500'),
        array('transaction_number' => 'AB-0003','date' => '2018-08-03', 'item_number' => '0101010', 'desc' => 'This is c', 'variant_code' => '007', 'quantity' => '1','cost' => '2500'),
        array('transaction_number' => 'AB-0003','date' => '2018-08-03', 'item_number' => '0101010', 'desc' => 'This is c', 'variant_code' => '015', 'quantity' => '7','cost' => '2500')
    );

    $symbol='£';
?>
<!doctype html>
<html>
    <head>
        <meta charset='utf-8' />
        <title>HTML table based upon PHP array data</title>
        <style>
            table{ border:1px solid gray;font-family:calibri,verdana,arial;float:none;margin:auto; }
            th{background:gray;color:white;padding:0.5rem;}
            td{padding:0.5rem;border:1px dotted gray;}
            td[colspan]{background:whitesmoke;}
            .currency:before{
                content:'<?=$symbol;?>';
                color:green;
                font-weight:bold;
            }
        </style>
    </head>
    <body>
        <table>
            <tr>
                <th>transaction_number</th>
                <th>date</th>
                <th>item_number</th>
                <th>desc</th>
                <th>variant_code</th>
                <th>quantity</th>
                <th>cost</th>
            </tr>
            <?php

                $trans=array();
                $dates=array();

                $total=new stdClass;
                $total->qty=0;
                $total->cost=0;


                foreach( $data as $index => $a ){
                    /*
                        Transaction number & date variables
                        - empty unless not in array
                    */
                    $tn='';
                    $dt='';

                    /* check if current transaction is already in the array - if not add it and create a new subtotal object */
                    if( !in_array( $a['transaction_number'], $trans ) ) {
                        /* assign `$dt` variable to newly discovered transaction and add to array */
                        $tn = $trans[] = $a['transaction_number'];

                        $subtotal=new stdClass;
                        $subtotal->qty=0;
                        $subtotal->cost=0;
                    }
                    /* Check if the date is in it's array - if not, add it */
                    if( !in_array( $a['date'], $dates ) ) {
                        /* assign `$dt` var to newly discovered date and add to array */
                        $dt = $dates[] = $a['date'];
                    }

                    /* update subtotals */
                    $subtotal->qty += floatval( $a['quantity'] );
                    $subtotal->cost += floatval( $a['cost'] );


                    /* output the table row with data including vars defined above */
                    printf('
                    <tr>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                    </tr>', $tn, $dt, $a['item_number'], $a['desc'], $a['variant_code'], $a['quantity'], $a['cost'] );


                    /* Show the sub-total for current transaction number */
                    if( ( $index < count( $data ) - 1 && $trans[ count( $trans )-1 ] != $data[ $index + 1 ]['transaction_number'] ) or $index==count( $data )-1 ){
                        printf('
                        <tr>
                            <td colspan=4>&nbsp;</td>
                            <td>SUB-TOTAL</td>
                            <td>%s</td>
                            <td class="currency">%s</td>
                        </tr>', $subtotal->qty, $subtotal->cost );

                        $total->qty += floatval( $subtotal->qty );
                        $total->cost += floatval( $subtotal->cost );
                    }
                }

                /* Show the final totals */
                printf('
                <tr><td colspan=7>&nbsp;</td></tr>
                <tr>
                    <td colspan=4>&nbsp;</td>
                    <td>TOTAL</td>
                    <td>%s</td>
                    <td class="currency">%s</td>
                </tr>', $total->qty, $total->cost );

            ?>
        </table>
    </body>
</html>

基于PHP数组数据的HTML表
表{边框:1px纯灰;字体系列:calibri、verdana、arial;浮点:无;边距:自动;}
th{背景:灰色;颜色:白色;填充:0.5rem;}
td{填充:0.5rem;边框:1px点灰色;}
td[colspan]{背景:白烟;}
.货币:以前{
内容:'';
颜色:绿色;
字体大小:粗体;
}
交易编号
日期
项目编号
描述
变体代码
量
费用