irnanto.com – cell merge function is combining two or more cells into one in a table. In HTML, combining the two into a single cell or a “two cells merge into one” there are two types of colspan and rowspan. Merge cells colspan used to combine two or more columns into one and merge cells rowspan used for auto-merge two or more lines into one.
Merge cells are often used in Microsoft Office applications such as Microsoft Office Word and Microsoft Office Excel to combine two or more cells into one in a table. This time irnanto.com discusses the use merge cells in an HTML table, especially the appearance of dynamic data using PHP, for which data is manipulated if the same will be made appearance in the HTML table into cells merge.
To create a table or display table from the database using the format merge cells in cell specific table then it would be nice if we look at the example html table merge cells below as an illustration how to display dynamic data from a database using PHP.
The above table is a table that has been in the merged cell. But before in the merged cells, the data is shown as below
Instansi | Kegiatan | Anggaran |
---|---|---|
Dinas A | Kegiatan A | 400.000 |
Dinas A | Kegiatan B | 300.000 |
Dinas A | Kegiatan C | 420.000 |
Dinas B | Kegiatan D | 401.000 |
Dinas B | Kegiatan E | 500.000 |
Dinas C | Kegiatan F | 600.000 |
Dinas C | Kegiatan G | 700.000 |
Dinas C | Kegiatan H | 8.800.000 |
How to create a merge cells
The previously mentioned types of cells merge HTML to combine two cells into one. Example HTML script merge cells taken from the use rowspan in the first table and script as below.
<table border="1" align="center"> <tbody> <tr> <th>Instansi</th> <th>Kegiatan</th> <th>Anggaran</th> </tr> <tr> <td rowspan="3">Dinas A</td> <td>Kegiatan </td> <td>400</td> </tr> <tr> <td>Kegiatan B</td> <td>300</td> </tr> <tr> <td>Kegiatan C</td> <td>420</td> </tr> <tr> <td rowspan="2">Dinas B</td> <td>Kegiatan D</td> <td>401</td> </tr> <tr> <td>Kegiatan E</td> <td>500</td> </tr> <tr> <td rowspan="3">Dinas C</td> <td>Kegiatan F</td> <td>600</td> </tr> <tr> <td>Kegiatan G</td> <td>700</td> </tr> <tr> <td>Kegiatan H</td> <td>8800</td> </tr> </tbody> </table>
Ok, we’ve seen the results of the appearance of the model script data in the table that has been in the merged cell, this time how to create a PHP script that can automatically adjust the display data of the same data in order to make the merge table cell. PHP script merge table cells as below.
<?php mysql_connect("localhost","root","") or die("gagal terkoneksi ke server "); mysql_select_db("db_instansi") or die("gagal memilih database"); $query="SELECT * FROM kantor order by instansi"; $proses=mysql_query($query); $i=0; while($data=mysql_fetch_assoc($proses)){ $row[$i]=$data; $i++; } foreach($row as $cell){ if(isset($total[$cell['instansi']]['jml'])) { $total[$cell['instansi']]['jml']++; }else{ $total[$cell['instansi']]['jml']=1; } } echo "<table border=\"1\"> <tr> <th>Instansi</th> <th>Kegiatan</th> <th>Anggaran</th> </tr>"; $n=count($row); $cekinstansi=""; for($i=0;$i<$n;$i++){ $cell=$row[$i]; echo '<tr>'; if($cekinstansi!=$cell['instansi']){ echo '<td' .($total[$cell['instansi']]['jml']>1?' rowspan="' .($total[$cell['instansi']]['jml']).'">':'>') .$cell['instansi'].'</td>'; $cekinstansi=$cell['instansi']; } echo "<td>$cell[kegiatan]</td><td>$cell[anggaran]</td>"; echo "</tr>"; } echo "</table>"; ?>
Explanation script briefly above is the data that is captured from a database stored in a variable row. Calculate the amount of each twin data (name of institution), create tables and display the data in the previous iteration in check first to make rowspan merge table cells.
So the discussion on How to Make Automatic Merge Cells Using PHP, next time in the continued again with other interesting articles
can you explain more about this [‘jml’]?