Home  >  Article  >  PHP Framework  >  How to merge thinkphp tables

How to merge thinkphp tables

WBOY
WBOYOriginal
2023-05-29 16:18:08551browse

How to merge ThinkPHP tables?

ThinkPHP is an excellent open source PHP development framework. It is loved by developers for its simplicity, ease of use, efficiency, stability, and strong scalability. In actual development, tables are a very commonly used function, and the merging of tables is also a common requirement.

This article will introduce how to use ThinkPHP to merge tables.

1. Merge cells

Before merging cells, you need to build a data table, as follows:

<table>
   <tr>
      <th rowspan="2">姓名</th>
      <th colspan="2">课程</th>
      <th rowspan="2">总分</th>
   </tr>
   <tr>
      <th>语文</th>
      <th>数学</th>
   </tr>
   <tr>
      <td>张三</td>
      <td>90</td>
      <td>80</td>
      <td>170</td>
   </tr>
   <tr>
      <td>李四</td>
      <td>75</td>
      <td>85</td>
      <td>160</td>
   </tr>
   <tr>
      <td>王五</td>
      <td>80</td>
      <td>75</td>
      <td>155</td>
   </tr>
</table>

In this table, you need to combine the first column and The cells in the fourth column are merged. Using ThinkPHP's view template, you can write like this:

<table>
   <tr>
      <th rowspan="2">姓名</th>
      <th colspan="2">课程</th>
      <th rowspan="2">总分</th>
   </tr>
   <tr>
      <th>语文</th>
      <th>数学</th>
   </tr>
   <?php foreach($data as $k => $v):?>
   <tr>
      <?php if($k == 0):?>
      <td rowspan="<?=count($data)?>"><?=$v['name']?></td>
      <?php endif;?>
      <td><?=$v['chinese']?></td>
      <td><?=$v['math']?></td>
      <?php if($k == 0):?>
      <td rowspan="<?=count($data)?>"><?=$v['total']?></td>
      <?php endif;?>
   </tr>
   <?php endforeach;?>
</table>

In this code, a foreach loop is used to output the data one by one in rows. If it is the data in the first row, set the cells in the first column to span rows, and the number of rows spanned is the total number of rows of data; if it is the data in the last row, set the cells in the fourth column to span rows, and span The number of rows is also the total number of rows of data. Other cells can directly output the corresponding data. In this way, the merging of cells can be achieved.

2. Merge tables

In addition to merging cells, sometimes you also need to merge multiple tables into one large table. This can be achieved with the help of ThinkPHP's template engine. Suppose there are two data tables as follows:

Table 1:

<table>
   <tr>
      <th>姓名</th>
      <th>年龄</th>
   </tr>
   <tr>
      <td>张三</td>
      <td>22</td>
   </tr>
   <tr>
      <td>李四</td>
      <td>23</td>
   </tr>
   <tr>
      <td>王五</td>
      <td>24</td>
   </tr>
</table>

Table 2:

<table>
   <tr>
      <th>学科</th>
      <th>成绩</th>
   </tr>
   <tr>
      <td>语文</td>
      <td>90</td>
   </tr>
   <tr>
      <td>数学</td>
      <td>80</td>
   </tr>
   <tr>
      <td>英语</td>
      <td>85</td>
   </tr>
</table>

Both of these two tables can be merged using the method described above. . If you need to merge these two tables into one large table, you can write it as follows:

<table>
   <tr>
      <th>姓名</th>
      <th>年龄</th>
      <th colspan="2">成绩</th>
   </tr>
   <tr>
      <td>张三</td>
      <td>22</td>
      <td>语文</td>
      <td>90</td>
   </tr>
   <tr>
      <td>李四</td>
      <td>23</td>
      <td>数学</td>
      <td>80</td>
   </tr>
   <tr>
      <td>王五</td>
      <td>24</td>
      <td>英语</td>
      <td>85</td>
   </tr>
</table>

In this code, the cells in the third and fourth columns are merged into a new form. The other cells can be output in the original order.

3. Summary

This article introduces the method of merging tables using ThinkPHP. By adjusting the rowspan and colspan properties of cells, you can merge cells; through the function of the template engine, you can merge multiple tables into one large table. These techniques are very useful for table operations in actual development.

The above is the detailed content of How to merge thinkphp tables. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn