问题
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 consequential, 'td' (number 4 and 5):
<tr>
(1) <td class="tdClass" ....</td>
(2) <td class="tdClass" ....</td>
(3) <td class="tdClass" ....</td>
(4) <td class="tdClass" align="center" nowrap="">No</td>
(5) <td class="tdClass" align="center" nowrap="">No</td>
</tr>
The strings could be "No" in the first 'td' and "Yes' in the second one, and vice versa, both "Yes" or both "No".
I want to count how many 'td', of the 5 kind, contains "No"
By now I counted other 'td'-s by looping through them (see the selected answer for my previous question linked at the top) and selecting only ones matching a target string.
This could be done because those target strings appear only one time in each row.
In this case, instead, the target string ("No") is not unique for each row, because, as in the example above, could exist two times (in the 'td' 4 & 5) in the same 'tr'.
By that, I really don't know how to select only the second (5) 'td' for each row, which match the target string "No", and to exclude the (4) 'td' (which could match the same string) from the count.
Obviously, these two 'td'-s are under different column titles, but this couldn't be useful to distinguish them.
The only solution I have thought about, is to count the 'td' position from left, and to select only the 5th one, but I don't know if it's possible.
回答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($rows as $row) {
$tds = $row->find('td');
for (i = 0; i < count($tds); $i++) {
// Check for the 4th and 5th element
if (($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.
回答2:
Taking the code from your previous question, you should already have this:
$targetString = 'TARGET STRING';
$rows = $table->find('.trClass');
$count = 0;
foreach($rows as $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($rows as $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++;
}
}
来源:https://stackoverflow.com/questions/10453380/php-parsed-html-table-and-count-specific-td-similar-to-another