note 103083 added to domelement.getattribute

From: Date: Thu, 24 Mar 2011 16:45:22 +0000
Subject: note 103083 added to domelement.getattribute
Groups: php.notes 
Request: Send a blank email to php-notes+get-178170@lists.php.net to get a copy of this message
You can also use DOMXPath objects to interleave foreach() in this way is slightly shorter. If you need a special element or attribute value. The variable $xquery needs xpath expression. This example filters all group elements as described in xpath query ($xquery), then it filters again the results by last if condition and stores all attribute names in an object variable of type array ($this->menu). Benefit: Using class array variable $menu in other class methods. Whenever you need those fields. XML: <xml> <category menu="products"> <groups> <group menu="vegetables">...</group> </groups> </category> </xml> public class example { public $menu; function _construct(){} public function getGroupValueByCategory($xpath_object,$category) { $xquery = '//category[normalize-space(@menu)="'.$category.'"]/groups/group'; if($elements = $xpobject->evaluate($xquery)){ $this->menu = array(); } foreach ($elements as $element) { if($element->hasAttributes()){ $attributes = $element->attributes; if(!is_null($attributes)){ foreach ($attributes as $index=>$attr){ if($attr->name == 'menu'){ $this->menu[] = $attr->value; } } } } } } } I used this to implement simple menu structure. ---- Server IP: 69.147.83.197 Probable Submitter: 92.231.189.196 ---- Manual Page -- http://www.php.net/manual/en/domelement.getattribute.php Edit -- https://master.php.net/note/edit/103083 Del: integrated -- https://master.php.net/note/delete/103083/integrated Del: useless -- https://master.php.net/note/delete/103083/useless Del: bad code -- https://master.php.net/note/delete/103083/bad+code Del: spam -- https://master.php.net/note/delete/103083/spam Del: non-english -- https://master.php.net/note/delete/103083/non-english Del: in docs -- https://master.php.net/note/delete/103083/in+docs Del: other reasons-- https://master.php.net/note/delete/103083 Reject -- https://master.php.net/note/reject/103083 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#178170) next »