Search code examples
phphtmlhtml-tablesimple-html-dom

PHP: How to join two HTML tables into one?


I have two HTML tables. Each has three rows and one column only. I'd like to join them programmatically such that I get one table with two columns and three rows.

Is there some function or hack to achieve this? How can I achieve this?

For example First Table:

<table id="table_one">
   <tbody>
    <tr>
      <td>
         <label>data one</label>
      </td>
    </tr>
    <tr>
      <td>
         <label>data two</label>
      </td>
    </tr>
    <tr>
      <td>
         <label>data three</label>
      </td>
    </tr>
  </tbody>
</table>

Second Table:

<table id="table_two">
  <tbody>
    <tr>
      <td>
         <label>data one</label>
      </td>
    </tr>
    <tr>
      <td>
         <label>data two</label>
      </td>
    </tr>
    <tr>
      <td>
         <label>data three</label>
      </td>
    </tr>
  </tbody>
</table>

This is what I want from the above two tables:

<table id="table_three">
  <tbody>
    <tr>
      <td>
         <label>data one</label>
      </td>
      <td>
         <label>data one</label>
      </td>
    </tr>
    <tr>
      <td>
         <label>data two</label>
      </td>
      <td>
         <label>data two</label>
      </td>
    </tr>
    <tr>
      <td>
         <label>data three</label>
      </td>
      <td>
         <label>data three</label>
      </td>
    </tr>
  </tbody>
</table>

Solution

  • Well, this is a way to proceed (check in-code comments):

    $table1 = <<<'_DATA_'
    <table id="table_one">
       <tbody>
        <tr>
          <td>
             <label>data one</label>
          </td>
        </tr>
        <tr>
          <td>
             <label>data two</label>
          </td>
        </tr>
        <tr>
          <td>
             <label>data three</label>
          </td>
        </tr>
      </tbody>
    </table>
    _DATA_;
    
    
    $table2 = <<<'_DATA_'
    <table id="table_two">
      <tbody>
        <tr>
          <td>
             <label>data one</label>
          </td>
        </tr>
        <tr>
          <td>
             <label>data two</label>
          </td>
        </tr>
        <tr>
          <td>
             <label>data three</label>
          </td>
        </tr>
      </tbody>
    </table>
    _DATA_;
    
    // Load teh 1st table
    // Create a DOM object
    $html1 = new simple_html_dom();
    // Load HTML from a string
    $html1->load($table1);
    
    
    // Load teh 2nd table
    // Create a DOM object
    $html2 = new simple_html_dom();
    // Load HTML from a string
    $html2->load($table2);
    
    // Find all rows from both tables
    $rows1 = $html1->find('tr');
    $rows2 = $html2->find('tr');
    
    // Build teh 3rd table
    $table3 =   '<table id="table_three">
                    <tbody>';
    
    // Insert rows cells from both initial tables
    for ($i=0; $i < count($rows1); $i++) { 
        $table3 .=  '<tr>';
    
            // get row's innerhtml
            $table3 .=  $rows1[$i]->innertext;
            $table3 .=  $rows2[$i]->innertext;
    
        $table3 .=  '</tr>';
    };
    
    // finish the table
    $table3 .=      '</tbody>
                </table>';
    
    // Clear DOM objects
    $html1->clear();
    unset($html1);
    $html2->clear();
    unset($html2);
    

    It creates this: