0

I am looping through a html file using htmldomparsing looking for a table e.g

foreach($html->find('table') as $table)
{
  foreach($table->find('tr') as $tr)
  {
    $count = count($tr);
    $test = explode(" ",$count);
    echo $count;
  }
 }

I am trying to count the number of rows in the table, but everytime I use the count function it returns: 1111111 etc.

When counting the rows , is there any way I can count each row and the count would increment rather than throw out "1111...." etc. Any ideas?

4
  • How are you parsing the html? Commented Mar 10, 2012 at 12:53
  • 1
    foreach($html->find('table') as $table){ foreach($table->find('tr') as $tr) { $count = count($tr); $test = explode(" ",$count); echo $count; } } Commented Mar 10, 2012 at 12:55
  • I am parsing using htmldom parsing the user submits a url into a form and it will then parse through the code looking for specific things for one of them I need to know how to get the number of rows in a table Commented Mar 10, 2012 at 12:56
  • It is doing what I said above I have specific guidelines which I need to follow as part of my project and for one of the guidelines I need to count the number of rows in a html table and then the number of <th> tags to check if they need to include an id attribute in the <th tag> Commented Mar 10, 2012 at 13:08

4 Answers 4

4

This will work

foreach($html->find('table') as $table){ 
     // returns all the <tr> tag inside $table
     $all_trs = $table->find('tr');
     $count = count($all_trs);
     echo $count;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hi it seems to echo out ArrayArrayArray when I follow that code maybe its to do with HTMLDomParsing I don't know any clue
Oops!! It should be echo $count. Updated!
Alrite that seems to do the job but can I ask another quick question if there are two tables in the html page is there any way I could do that loop for each table so say for the first table it will echo out 5 which is the number of rows and for the second it will echo 2
0

I was working on this for a while, but someone beat me to the punch. My solution is not elegant, but if you wanted to get away from using whatever library that is you could write the same thing natively.

function count_tr($data, $p=0)
{
//$data is a string that we assume to have some html in it.  
//$p is an indicator of where we are in the string
//$table_counts is an array of numRows
//$numRows is the number of rows we've counted this table
//$z is an indicator of where the next </table> tag is
$numRows = 0;
$table_counts = array();
$z = $p;
//First lets loop through it until we find a <table tag
while($p < strlen($data))
{
    //we will break under the condition that we have found the end of the data

    //If table is found, count its <tr tags.  Else, break
    if($p = strpos("<table", $data, $p) != FALSE)
    {
        //if we find a </table tag we want to know where it is.
        //else we need to stop execution.
        if($z = strpos("</table", $data, $p) != FALSE)
        {
            $numRows = 0;
            while($p < strlen($data))
            {
                //find the position of the next <tr
                if($p=strpos("<tr", $data, $p) != FALSE)
                {
                    //if the <tr tag is within this <table tag then we count it
                    //else we break to the next iteration
                    if($p<$z)
                    {
                        $numRows++;
                    }
                    else
                    {
                        //This will take us to the next <table iteration.
                        break;
                    }
                }
            }
            $table_counts[] = $numRows; 
        }
        else
        {
            die("The data is not formatted correctly.  That is beyond the scope of this snipped.  Please write a validation script.");
        }

    }
    else
    {
        die("There is not a <table tag.");
    }
}
}

Comments

0

If you have a database associated with the table then you can use the following code:

`$result = mysql_query("SELECT * FROM table1", $link); $num_rows = mysql_num_rows($result);

echo "$num_rows Rows\n"; `

Comments

0

Why just not use a simple count variable for that?

$count = 0;
foreach($res as $value){
   $count++;
}

1 Comment

I think it would be better if you did not use a rhetorical question when posting an Answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.