How to Make Automatic Merge Cells Using PHP

By . Category: PHP Tags: ,

irnanto.comcell 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 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.

Table merge cells

Table merge cells

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

Dinas AKegiatan A400.000
Dinas AKegiatan B300.000
Dinas AKegiatan C420.000
Dinas BKegiatan D401.000
Dinas BKegiatan E500.000
Dinas CKegiatan F600.000
Dinas CKegiatan G700.000
Dinas CKegiatan H8.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">
      <td rowspan="3">Dinas A</td>
      <td>Kegiatan </td>
      <td>Kegiatan B</td>
      <td>Kegiatan C</td>
      <td rowspan="2">Dinas B</td>
      <td>Kegiatan D</td>
      <td>Kegiatan E</td>
      <td rowspan="3">Dinas C</td>
      <td>Kegiatan F</td>
      <td>Kegiatan G</td>
      <td>Kegiatan H</td>

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.

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";
foreach($row as $cell){
  if(isset($total[$cell['instansi']]['jml'])) { 
echo "<table border=\"1\"> 
  echo '<tr>';
    echo '<td' .($total[$cell['instansi']]['jml']>1?' rowspan="' .($total[$cell['instansi']]['jml']).'">':'>') .$cell['instansi'].'</td>';
  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


one comment untuk “How to Make Automatic Merge Cells Using PHP”

  • Ryan Afreez

    can you explain more about this [‘jml’]?

Tinggalkan Komentar