user9165568
user9165568

Reputation:

counting html table rows with domDocument

I am using domDocument to count rows in html table. This is the code I have

if(isset($_POST['List']) && $_POST['List'] == true)
{
    $data = file_get_contents('File.php');
    $dom = new domDocument;
    @$dom->loadHTML($data);
    $dom->preserveWhiteSpace = false;
    $table = $dom->getElementById('Table');
    $rows = substr_count($table, "<tr>");

    for($x=0;$x<$rows;$x++)
    {

    }
}

Stepping through the code, $rows is null. This is the contents of $table

tagName:"table"
schemaTypeInfo:null
nodeName:"table" 
nodeValue:"\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\tCategoryAMA1A2ABBECCEC1C1EDDED1D1EW\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t"
nodeType:1
parentNode:"(object value omitted)"
childNodes:"(object value omitted)"
firstChild:"(object value omitted)"
lastChild:"(object value omitted)"
previousSibling:"(object value omitted)"
nextSibling:null
attributes:"(object value omitted)"
ownerDocument:"(object value omitted)"
namespaceURI:null
prefix:""
localName:"table"
baseURI:null
textContent:"\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\tCategoryAMA1A2ABBECCEC1C1EDDED1D1EW\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t"

I have changed the code as follows

$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$doc = new domDocument();
@$doc->loadHTMLFile($url);
$table = $doc->getElementById('Table');
$rows = $table->getElementsByTagName('tr');
$count = count($rows);

I submitted the form with 2 rows in the table, but the count is 1. This is the contents of $table

tagName:"table"
schemaTypeInfo:null
nodeName:"table" 
nodeValue: "\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\tCategoryAMA1A2ABBECCEC1C1EDDED1D1EW\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\tDate\r\n\t\t\t\t\t\t"
nodeType:1
parentNode:"(object value omitted)"
childNodes:"(object value omitted)"
firstChild:"(object value omitted)"
lastChild:"(object value omitted)"
previousSibling:"(object value omitted)"
nextSibling:null
attributes:"(object value omitted)"
ownerDocument:"(object value omitted)"
namespaceURI:null
prefix:""
localName:"table"
baseURI:null
textContent: "\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\tCategoryAMA1A2ABBECCEC1C1EDDED1D1EW\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\tDate\r\n\t\t\t\t\t\t"

Does loadHTMLFile have the same restriction as file_get_contents, only seeing the original html?

Upvotes: 0

Views: 648

Answers (1)

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41810

After you have selected the table, you can get its rows using getElementsByTagName.

$rows = $table->getElementsByTagName('tr');

The result will be a DOMNodeList object that has a length property, which should be the count you're looking for.

echo $rows->length;

It looks like you're just using the row count to set up a for loop, which may not be necessary, because you can iterate the set of rows with foreach.

foreach ($rows as $row) {
    // do something with the row
}

Upvotes: 1

Related Questions