Подсчет строк в HTML-таблице с использованием php - PullRequest
0 голосов
/ 10 марта 2012

Я перебираю html-файл, используя htmldomparsing, ищу таблицу, например,

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

Я пытаюсь подсчитать количество строк в таблице, но каждый раз, когда я использую count function, он возвращает: 1111111 и т. Д.

При подсчете строк, есть ли способ подсчитать каждую строку, и счет будет увеличиваться, а не выбрасывать "1111 ...." и т. Д. Есть идеи?

Ответы [ 3 ]

3 голосов
/ 10 марта 2012

Это будет работать

foreach($html->find('table') as $table){ 
     // returns all the <tr> tag inside $table
     $all_trs = $table->find('tr');
     $count = count($all_trs);
     echo $count;
}
0 голосов
/ 12 августа 2018

Если с таблицей связана база данных, вы можете использовать следующий код:

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

echo "$ num_rows Rows \ n";`

0 голосов
/ 10 марта 2012

Я работал над этим некоторое время, но кто-то избил меня до удара. Мое решение не изящно, но если вы хотите отказаться от использования какой-либо библиотеки, вы можете написать ту же вещь изначально.

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.");
    }
}
}
...