Skip to content Skip to sidebar Skip to footer

Php Parsed Html Table And Count Specific Similar To Another

This question follows another, just solved here Now I want to do a different count, more difficult to figure out. In my parsed HTML table, every rows contains two very similar, and

Solution 1:

You will indeed have to update some parts. First you will want the 4th and 5th element, so you'll have to check for that (keep a counter or use a for loop). Secondly you don't need the break in this case, since it stops the loop.

Code:

<?php$targetString = 'No';
$rows = $table->find('.trClass');

$count = 0;
foreach($rowsas$row) {
    $tds = $row->find('td');
    for (i = 0; i < count($tds); $i++) {
        // Check for the 4th and 5th elementif (($i === 3 || $i === 4) && $tds[$i]->innertext === $targetString) {
            $count++;
        }
    }
}

Here I use a for loop instead of a foreach loop because I don't want to keep manually a counter. I can use the $i easily for this and just use it as an index as well.

Solution 2:

Taking the code from your previous question, you should already have this:

$targetString = 'TARGET STRING';
$rows = $table->find('.trClass');

$count = 0;
foreach($rowsas$row) {
    foreach($row->find('td') as$td) {
        if ($td->innertext === $targetString) {
            $count++;
            break;
        }
    }
}

Since you're already going through the td's, it would be quite simple to do what you said - "count the 'td' position from left, and to select only the 5th". As long as you know that it is definitely the fifth td you can do:

foreach($rowsas$row) {
    $tdcount = 0;
    foreach($row->find('td') as$td) {
        //...//Bear in mind the first td will have tdcount=0, second tdcount=1 etc. so fifth:if($tdcount === 4 && ( 'Yes'===$td->innertext || 'No'===$td->innertext) ) {
            //do whatever you want with this td
        }

        $tdcount++;
    }
}

Post a Comment for "Php Parsed Html Table And Count Specific Similar To Another"