PHP with DOMXPath - Sum values after selection of items -


i have 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 select items 'open', 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, have isolated 'open' items wanted. now, need 'length' values, , sum them make total length can echo it. i've spent several hours trying different solutions , researching, haven't found similar. can guys help?

thanks in advance.

edited wrong div's, sorry.

i'm not sure if mean calculations done in xsl or whether wanting sum of lengths available in php, captures , sums lengths. noted @chris85 in comment - html invalid - there spare closing div tags within each entry ~ presumably image supposed child of div.status? if below need slight modification when trying target correct parent. said, received no warnings domdocument whilst parsing better fix 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 loading file directly */ $dom->preservewhitespace = false;  $xp = new domxpath($dom);                $col=$xp->query('//img[@alt="open"]');/* target nodes attribute need */ /* variable increment values found dom values */ $length=0;  foreach( $col $n ) {/* loop through found nodes collection */     $parent=$n->parentnode->parentnode;/* corrected here account change in html layout ~ suitable parent node */      /* based on original code, find value particular node */     $length += $parent->childnodes->item(5)->nodevalue;  } echo 'length:'.$length; 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -