如何使动态rowspan php codeigniter?

时间:2018-07-26 00:27:04

标签: php html codeigniter

我在CodeIgniter应用程序上具有动态的rowspan表:

<table>
<tr>
    <td>No</td>
    <td>Data</td>
    <td>Data 2</td>
    <td>Qty</td>
    <td>Price</td>
    <td>Sub Total</td>
    <td>TOtal</td>
</tr>
<?php

$source1 = $this->db->query("select * from table")->result_array(); 
$no=1;
foreach($source1 as source1){ ?>
    <tr>
        <?php 
        $source2 = $this->db->query("select * from table where data1='$source1[data1]'");
        $total_source2 = $source2->num_rows();
        $source3 = $source2->result_array();
        ?>
        <td rowspan="<?php echo $total_source2 ?>"><?php echo $no; ?></td>
        <td rowspan="<?php echo $total_source2 ?>"><?php echo $source1['data1']; ?></td>
        <?php foreach($source3 as $source3){ ?>
            <td><?php echo $source3['data2'] ?></td>
            <td><?php echo $source3['qty'] ?></td>
            <td><?php echo $source3['price'] ?></td>
            <td><?php echo $source3['sub_total'] ?></td>
            <td><?php echo $source3['total'] ?></td>
        </tr>
    <?php } ?>
    <?php $no++; } ?>
</table>

这是我的代码的结果:

table 1

如何使其像这样

table 2

谢谢。

1 个答案:

答案 0 :(得分:1)

这几乎和没有实际SQL文件时我能想到的效率一样,我不确定它是否有效:

<table>
    <tr>
        <td>No</td>
        <td>Data</td>
        <td>Data 2</td>
        <td>Qty</td>
        <td>Price</td>
        <td>Sub Total</td>
        <td>Total</td>
    </tr>
    <?php
    $source1 = $this->db->query("select * from table")->result_array();
    $no = 1;
    foreach ($source1 as $source1) {
        ?>
        <tr>
            <?php
            $source2 = $this->db->query('select * from table where data1 = ' . $source1['data1']);
            $total_source2 = $source2->num_rows();
            $source3 = $source2->result_array();
            $rowspan = true;
            ?>
            <td rowspan="<?php echo $total_source2 ?>"><?php echo $no; ?></td>
            <td rowspan="<?php echo $total_source2 ?>"><?php echo $source1['data1']; ?></td>
            <?php foreach ($source3 as $source3) { ?>
                <td><?php echo $source3['data2'] ?></td>
                <td><?php echo $source3['qty'] ?></td>
                <td><?php echo $source3['price'] ?></td>
                <td><?php echo $source3['sub_total'] ?></td>
                <?php
                if ($rowspan) {
                    $q = $this->db->query('SELECT SUM(`sub_total`) as `nb` FROM `table` WHERE `data1` = ' . $source1['data1']);
                    echo "<td rowspan='{$total_source2}'>" . $q->row()->nb . '</td>';
                    $rowspan = false;
                }
                ?>
            </tr>
        <?php } ?>
        <?php
        $no++;
    }
    ?>
</table>