Note that there are some explanatory texts on larger screens.

plurals
  1. POxml foreach skipping over if statement in php
    text
    copied!<p>Here's what I am trying to achieve. I am importing an xml file of products (the product tag has an id attribute: as an example.</p> <p>There is 1 tag (category) inside the product tag that I am trying to retrieve but I only want one instance of it. So I created a flag to help distinguish when the product id changes. </p> <p>My code looks like this</p> <pre><code>&lt;?php $flag='start'; foreach($xml-&gt;product as $product) { $attrs = $product-&gt;attributes(); echo "$attrs ($flag)"; // just used for testing results if ($flag != $attrs) { echo "| &lt;a href='xmltest.php?menuitem=$attrs'&gt;".$product-&gt;category." &lt;/a&gt;&lt;br&gt;"; $flag=$attrs; } } ?&gt; </code></pre> <p>What should happen on the first run is the flag doesn't match the attrs, the link is echoed, the flag now matches the attrs. </p> <p>If I have 5 product tags with ID's of 1,1,2,2,2, the code should echo the link twice (first when $flag=start while $attrs=1, and when $flag=1 while $attrs=2)...</p> <p>Instead, it echoes it all 5 times, basically ignoring the if statement.</p> <p>I can't see where I am going wrong with the if statement. Can anyone help?</p> <p><em><strong>Update</em></strong> Thanks Showerhead, I have been trying your suggestions and I am a bit closer than before.</p> <p>I did the var_dump and the result I am seeing looks like this</p> <blockquote> <p>flag var_dump result:string(5) "start" attrs var_dump result: object(SimpleXMLElement)#6 (1) { ["@attributes"]=> array(1) { ["id"]=> string(1) "0" } } 0 (0)</p> <p>flag var_dump result:object(SimpleXMLElement)#6 (1) { ["@attributes"]=> array(1) { ["id"]=> string(1) "0" } } </p> <p>attrs var_dump result: object(SimpleXMLElement)#5 (1) { ["@attributes"]=> array(1) { ["id"]=> string(1) "0" } } 0 (0)</p> <p>flag var_dump result:object(SimpleXMLElement)#5 (1) { ["@attributes"]=> array(1) { ["id"]=> string(1) "0" } } </p> <p>attrs var_dump result: object(SimpleXMLElement)#4 (1) { ["@attributes"]=> array(1) { ["id"]=> string(1) "0" } } 1 (0)</p> <p>flag var_dump result:object(SimpleXMLElement)#4 (1) { ["@attributes"]=> array(1) { ["id"]=> string(1) "0" } } </p> <p>attrs var_dump result: object(SimpleXMLElement)#8 (1) { ["@attributes"]=> array(1) { ["id"]=> string(1) "1" } } 1 (1)</p> <p>flag var_dump result:object(SimpleXMLElement)#8 (1) { ["@attributes"]=> array(1) { ["id"]=> string(1) "1" } } </p> </blockquote> <p>I understand now how the $flag var starts off as a string and then adopts the value of the array.</p> <p>I tried if( ! is_array($flag) &amp;&amp; $flag != $attrs) { but the results were the same.</p> <p>Is there another way I can get the values to be similar for comparison?</p> <p>Thanks again,</p> <p>UPDATE</p> <p>a simple explode helped solve it. Here is the code that is working for me.</p> <pre><code>&lt;?php $flag='start'; foreach($xml-&gt;product as $product) { $category = $product-&gt;category; $attrsvar = $product-&gt;attributes(); $attrs = explode(" ", $attrsvar); if( ! is_array($flag) &amp;&amp; $flag != $attrs[0]) { echo " &lt;a href='xmltest.php?menuitem=".$attrs[0]."'&gt;".$category." &lt;/a&gt; | "; $flag=$attrs[0]; } } ?&gt; </code></pre>
 

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