简体   繁体   中英

PHP with DOMXPath - Sum values after selection of items

I have this html structure:

<div class="wanted-list">
    <div class="headline"></div>
    <div class="entry">
        <div></div>
        <div></div>
        <div class="length">1100</div>
        <div></div>
        <div class="status">
            <img src="xxxx" alt="open">
        </div>
    </div>
    <div class="entry mark">
        <div></div>
        <div></div>
        <div class="length">800</div>
        <div></div>
        <div class="status">
            <img src="xxxx" alt="open">
        </div>
    </div>
    <div class="entry">
        <div></div>
        <div></div>
        <div class="length">2300</div>
        <div></div>
        <div class="status">
            <img src="xxxx" alt="closed">
        </div>
    </div>
</div>

I want to select only the items that are 'open', so I do:

$doc4 = new DOMDocument();
$doc4->loadHtmlFile('http://www.whatever.com');
$doc4->preserveWhiteSpace = false;
$xpath4 = new DOMXPath($doc4);
$elements4 = $xpath4->query("//div[@class='wanted-list']/div/div[5]/img[@alt='open']");

Now, if I'm not mistaken, we have isolated the 'open' items we wanted. Now, I need to get the 'length' values, and sum them to make a total length so I can echo it. I've spent several hours trying different solutions and researching, but I haven't found anything similar. Can you guys help?

Thanks in advance.

EDITED the wrong div's, sorry.

I'm not sure if you mean for the calculations all to be done in the xsl or whether you are just wanting the sum of the lengths to be available to you in php, however this captures and sums the lengths. As noted by @Chris85 in the comment - the html is invalid - there are spare closing div tags within each entry ~ presumably the image is supposed to be a child of div.status ? If that is so the below would need slight modification when trying to target the correct parent. That said, I received no warnings from DOMDocument whilst parsing it but better to fix than ignore!

$strhtml='
<div class="wanted-list">
    <div class="headline"></div>
    <div class="entry">
        <div></div>
        <div></div>
        <div class="length">1100</div>
        <div></div>
        <div class="status">
            <img src="xxxx" alt="open">
        </div>
    </div>
    <div class="entry mark">
        <div></div>
        <div></div>
        <div class="length">800</div>
        <div></div>
        <div class="status">
            <img src="xxxx" alt="open">
        </div>
    </div>
    <div class="entry">
        <div></div>
        <div></div>
        <div class="length">2300</div>
        <div></div>
        <div class="status">
            <img src="xxxx" alt="closed">
        </div>
    </div>
</div>';


$dom = new DOMDocument();
$dom->loadHtml( $strhtml );/* alternative to loading a file directly */
$dom->preserveWhiteSpace = false;

$xp = new DOMXPath($dom);               
$col=$xp->query('//img[@alt="open"]');/* target the nodes with the attribute you need to look for */
/* variable to increment with values found from DOM values */
$length=0;

foreach( $col as $n ) {/* loop through the found nodes collection */
    $parent=$n->parentNode->parentNode;/* corrected here to account for change in html layout ~ get the suitable parent node */

    /* based on original code, find value from particular node */
    $length += $parent->childNodes->item(5)->nodeValue; 
}
echo 'Length:'.$length;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM