Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP and XML. Looping through an XML file with PHP
    primarykey
    data
    text
    <p>I am knee deep in foreach purgatory right now trying to come up with a way to traverse this XML file (actual XML text below) with PHP(following the XML file content.) What I am trying to do is the following:</p> <ol> <li>Get all folder element names</li> <li>If the folder element has yes as a subfolder attribute, then move a level down and grab that folder element's name</li> <li>If not move on to the next folder element</li> </ol> <p>gallerylist.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt; &lt;gallerylisting exists="yes"&gt; &lt;folder subfolder="yes"&gt; Events &lt;folder subfolder="yes"&gt; Beach_Clean_2010 &lt;folder subfolder="no"&gt; Onna_Village &lt;/folder&gt; &lt;folder subfolder="no"&gt; Sunabe_Sea_Wall &lt;/folder&gt; &lt;/folder&gt; &lt;/folder&gt; &lt;folder subfolder="no"&gt; Food_And_Drink &lt;/folder&gt; &lt;folder subfolder="no"&gt; Inside &lt;/folder&gt; &lt;folder subfolder="no"&gt; Location &lt;/folder&gt; &lt;folder subfolder="no"&gt; NightLife &lt;/folder&gt; &lt;/gallerylisting&gt; </code></pre> <p>gallerylisting.php</p> <pre><code>&lt;?php $xmlref = simplexml_load_file("gallerylisting.xml"); foreach($xmlref-&gt;children() as $child) { foreach($child-&gt;attributes() as $attr =&gt; $attrVal) { print $child; if($attrVal == "yes") { foreach($child-&gt;children() as $child) { echo $child; foreach($child-&gt;attributes() as $attr =&gt; $attrVal) { if($attrVal == "yes") { foreach($child-&gt;children() as $child) { echo $child; } } } } } } } </code></pre> <p>I am...counting...5 foreach loops deep into this PHP script and I do not like it at all, plus if my folders had another subfolder, I would have to add this same</p> <pre><code>$if(attrVal=="yes")...etc. </code></pre> <p>in again and well...no! Is there anyway at all that I can avoid this. I'm new to PHP, and especially PHP and XML.</p> <p>Thanks for any help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload