Skip to main content

Menginput hasil tampilan tabel ke database

<?php
$dom = new DOMDocument();

//load the html
$html = $dom->loadHTMLFile("coba_dom.html");

  //discard white space  
$dom->preserveWhiteSpace = false;  

  //the table by its tag name
$tables = $dom->getElementsByTagName('table');  


    //get all rows from the table
$rows = $tables->item(0)->getElementsByTagName('tr');  
  // get each column by tag name
$cols = $rows->item(0)->getElementsByTagName('th');  
$row_headers = NULL;

foreach ($cols as $node) {
    //print $node->nodeValue."\n";  
    $row_headers[] = $node->nodeValue;
}  

$table = array();
  //get all rows from the table
$rows = $tables->item(0)->getElementsByTagName('tr');  
foreach ($rows as $row)  
{  
   // get each column by tag name
 
    $cols = $row->getElementsByTagName('td');  
    $row = array();
    $i=0;
    foreach ($cols as $node) {
        # code...
        //print $node->nodeValue."\n";  
        if($row_headers==NULL)
            /* $row[] = $node->nodeValue."\n"; */
echo "KOSONG";
        else
            $row[$row_headers[$i]] = $node->nodeValue;
echo "INSERT INTO " . $row_headers[$i] . " VALUES " .  $row[$row_headers[$i]]."</br>";
        $i++;
    }  
    $table[] = $row;
}  

/* var_dump($table); */

?>

Comments