Dustin Campbell
Dustin Campbell

Reputation: 27

xml foreach skipping over if statement in php

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.

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.

My code looks like this

<?php 
$flag='start';
foreach($xml->product as $product)
{
    $attrs = $product->attributes();
echo "$attrs ($flag)"; // just used for testing results
if ($flag != $attrs) {
echo "| <a href='xmltest.php?menuitem=$attrs'>".$product->category." </a><br>"; 
$flag=$attrs;
}

}
?>

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.

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)...

Instead, it echoes it all 5 times, basically ignoring the if statement.

I can't see where I am going wrong with the if statement. Can anyone help?

Update Thanks Showerhead, I have been trying your suggestions and I am a bit closer than before.

I did the var_dump and the result I am seeing looks like this

flag var_dump result:string(5) "start" attrs var_dump result: object(SimpleXMLElement)#6 (1) { ["@attributes"]=> array(1) { ["id"]=> string(1) "0" } } 0 (0)

flag var_dump result:object(SimpleXMLElement)#6 (1) { ["@attributes"]=> array(1) { ["id"]=> string(1) "0" } }

attrs var_dump result: object(SimpleXMLElement)#5 (1) { ["@attributes"]=> array(1) { ["id"]=> string(1) "0" } } 0 (0)

flag var_dump result:object(SimpleXMLElement)#5 (1) { ["@attributes"]=> array(1) { ["id"]=> string(1) "0" } }

attrs var_dump result: object(SimpleXMLElement)#4 (1) { ["@attributes"]=> array(1) { ["id"]=> string(1) "0" } } 1 (0)

flag var_dump result:object(SimpleXMLElement)#4 (1) { ["@attributes"]=> array(1) { ["id"]=> string(1) "0" } }

attrs var_dump result: object(SimpleXMLElement)#8 (1) { ["@attributes"]=> array(1) { ["id"]=> string(1) "1" } } 1 (1)

flag var_dump result:object(SimpleXMLElement)#8 (1) { ["@attributes"]=> array(1) { ["id"]=> string(1) "1" } }

I understand now how the $flag var starts off as a string and then adopts the value of the array.

I tried if( ! is_array($flag) && $flag != $attrs) { but the results were the same.

Is there another way I can get the values to be similar for comparison?

Thanks again,

UPDATE

a simple explode helped solve it. Here is the code that is working for me.

<?php 
$flag='start';
foreach($xml->product as $product)
{
    $category = $product->category;
    $attrsvar = $product->attributes();
    $attrs = explode(" ", $attrsvar);

if( ! is_array($flag) && $flag != $attrs[0]) { 
echo " <a href='xmltest.php?menuitem=".$attrs[0]."'>".$category." </a> | "; 
$flag=$attrs[0]; 
    }
}
?>

Upvotes: 1

Views: 373

Answers (1)

mseancole
mseancole

Reputation: 1672

You are going to want to make sure it is not an array first.

if( ! is_array($flag) && $flag != $attrs) { etc... }

There that should be it, sorry if you were reading first post before I edited it. I completely misread your problem. Now the reason this should work is because you were originally trying to compare an array to a string, which was probably throwing silent errors.

Edit: If its still not working try var_dump() to make sure both variables are the same type when they are being compared.

Update: You know what, we are both over thinking this. I will assume you are doing something with this XML other than just getting categories, otherwise you can look into the much simpler XPATH solution below. If you are doing something more with this XML you could just do the following.

$products = array();
foreach($xml->product as $product) {
    $id = $product['id'];
    $category = $product->category;

    if( ! in_array($id, $products)) {
        $products[$id] = $category;
        echo "| <a href='xmltest.php?menuitem=$attrs'>".$category." </a><br>"; 

    }
}

XPATH for if you just want to work with categories. Let me know if my XPATH is not quite right, I'm not too good with it under normal circumstances, I'd hate to think how badly I could mangle it under sleep depravation.

$categories = $xml->xpath('//product/category');
$categories = array_unique($categories);
foreach($categories as $category) {
    echo "| <a href='xmltest.php?menuitem=$attrs'>".$category." </a><br>"; 
}

Upvotes: 0

Related Questions