Hello
I have this table I wish to break up into an array using DOMdocument for inserting into a database. But why does it show every entry four times?
<?php
$table = "<TABLE width='100%' BORDER=0 CELLSPACING=1 CELLPADDING=1 ID='Table2'>
<tr>
<td colspan='6'><hr></td>
</tr>
<tr>
<td width='9%'><strong>Code</strong></td>
<td width='5%'></td>
<td width='19%'><strong>Name</strong></td>
<td width='19%' title='Status' onmouseover='this.bgColor='aqua'' onmouseout='this.bgColor='white''><strong>Status</strong></td>
<td width='39%'><strong>Programme</strong></td>
<td width='9%'><strong>Source</strong></td>
<td width='10%'> </td>
</tr>
<tr onMouseover='this.bgColor='yellow'' onMouseout='this.bgColor='white''>
<td>423423/3</td><td></td>
<td>Paul</td><td title=''>ENROLLED</td>
<td>Bread</td><td>Group</td>
<td width='10%'> </td>
</tr>
<tr onMouseover='this.bgColor='yellow'' onMouseout='this.bgColor='white''>
<td>92871/1</td><td></td>
<td>Eric</td><td title=''>ENROLLED</td>
<td>Cheese</td> <td>Group</td>
<td width='10%'></td>
</tr>
</TABLE>";
$data = getHTMLtable($table,"Table2");
function getHTMLtable($html, $id, $headings) {
$doc = new DOMDocument();
@$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$data = array();
$elements = $xpath->query("//table[@id='" . $id . "']/tr");
if (is_null($elements)) {
return array();
}
foreach ($elements as $item) {
$newDom = new DOMDocument;
$newDom->appendChild($newDom->importNode($item, true));
$xpath = new DOMXPath($newDom);
foreach ($item->attributes as $attribute) {
$c=0;
// var_dump($attribute);
for ($node = $item->firstChild; $node !== NULL; $node = $node->nextSibling) {
$val = trim($node->nodeValue);
if ($val) {
echo $c.": ".$val."<br>";
$c++;
}
}
echo "<hr>";
}
}
return $data;
}
?>
Can anyone help?
Thanks
Karl.