如何制作动态的rowspan codeigniter?

时间:2018-07-25 02:23:04

标签: php html codeigniter

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

<table>
<tr>
    <td>No</td>
    <td>Data 1</td>
    <td>Data 2</td>
    <td>Qty</td>
    <td>Data 3</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['data3'] ?></td>
        </tr>
    <?php } ?>
    <?php $no++; } ?>
</table>

这是我的代码的结果:

table 1

如何使其像这样:

table 2

谢谢

2 个答案:

答案 0 :(得分:2)

table {
    border: 1px solid #000;
    border-collapse: collapse;
    width: 100%;
}

table td,
table th {
    border: 1px solid #000;
    text-align: center;
}
<table>
  <tr> 
    <th>No</th>
    <th>Data1</th> 
    <th>Data2</th> 
    <th>Qty</th>
    <th>Price</th>
    <th>Sub Total</th>
    <th>Total</th>
  </tr> 
  <tr> 
    <td rowspan="2">1</td> 
    <td rowspan="2">ABCDE</td> 
    <td>Data2 a</td> 
    <td>1</td>
    <td>100</td>
    <td>100</td>
    <td rowspan="2">620</td>
  </tr> 
  <tr> 
    <td>Data2 b</td> 
    <td>4</td> 
    <td>130</td> 
    <td>152</td>
  </tr>
  <tr> 
    <td rowspan="3">2</td> 
    <td rowspan="3">ABC</td> 
    <td>Data2 c</td> 
    <td>2</td>
    <td>400</td>
    <td>800</td>
    <td rowspan="3">1560</td>
  </tr> 
  <tr> 
    <td>Data2 d</td> 
    <td>2</td> 
    <td>200</td> 
    <td>400</td>
  </tr>
  <tr> 
    <td>Data2 e</td> 
    <td>3</td> 
    <td>120</td> 
    <td>360</td>
  </tr>
  <tr> 
    <td>3</td> 
    <td>ASS</td> 
    <td>Data 2 f</td> 
    <td>1</td> 
    <td>100</td> 
    <td>100</td> 
    <td>100</td> 
  </tr>
 </table>

答案 1 :(得分:0)

每当必须应用行跨度或列跨度时,请始终尝试将内容写在for-each之外

<table>
<tr>
    <td>No</td>
    <td>Data 1</td>
    <td>Data 2</td>
    <td>Qty</td>
    <td>Data 3</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>
    <?php } ?>
   <td rowspan="<?php echo $total_source2 ?>"><?php echo $source3['data3'] ?></td>
   </tr>
    <?php $no++; } ?>
</table>