phpzend-frameworkdomzend-dom-query

Digging deeper into DOMElement


I've used Zend_Dom_Query to extract some <tr> elements and I want to now loop through them and do some more. Each <tr> looks like this, so how can I print the title Title 1 and the id of the second td id=categ-113?

<tr class="sometr">
  <th><a class="title">Title1</a></th>
  <td class="category" id="categ-113"></td>
  <td class="somename">Title 1 name</td>
</tr>

Solution

  • You should just play around with the results. I've never worked with it, but this is how far i got (and im kinda new to Zend myself):

    $dom = new ZEnd_Dom_Query($html);
    $res = $dom->query('.sometr');
    
    foreach($res as $dom) {
      $a = $obj->getElementsByTagName('a');
      echo $a->item(0)->textContent; // the title
    }
    

    And with this i think you're set to go. For further information and functions to be used of the result look up DOMElement ( http://php.net/manual/de/class.domelement.php ). With this information you should be able to grab all that. But my question is: Why doing this so complicated, i don't really see a use-case for doing this. As the title and everything else should be something coming from the database? And if it's an XML there's better solutions than relying on Dom_Query.

    Anyways, if this was helpful to you please accept and/or vote the answer.