How to make a correct if-statement to filter out values from a xml-file

Posted by Garreth 00 on Stack Overflow See other posts from Stack Overflow or by Garreth 00
Published on 2013-07-01T09:25:56Z Indexed on 2013/07/01 17:08 UTC
Read the original article Hit count: 219

Filed under:
|

Edit 3:

As requested, I'm trying to simplify my question.

Here is a sample of some of my data from a xml file:

<entry>
    <title>Entry 1</title>
    <f:max_value_a>499 999</f:max_value_a>
    <f:max_value_b>999 999</f:max_value_b>
    <f:min_value_a>0</f:min_value_a>
    <f:min_value_b>500 000</f:min_value_b>
    <f:min_value_c>1 000 000</f:min_value_c>
    <f:value_for_a>5,10</f:value_for_a>
    <f:value_for_b>4,50</f:value_for_b>
    <f:value_for_c>3,90</f:value_for_c>
</entry>    


<entry>
    <title>Entry 2</title>
    <f:min_value_a>0</f:min_value_a>
    <f:value_for_a>4,20</f:value_for_a>
</entry>

<entry>
    <title>Entry 3</title>
    <f:max_value_a>1 999 999</f:max_value_a>
    <f:min_value_a>100 000</f:min_value_a>
    <f:min_value_b>2 000 000</f:min_value_b>
    <f:value_for_a>3,735</f:value_for_a>
    <f:value_for_b>3,445</f:value_for_b>
</entry>    

f:value_for_d is the highest value, and f:value_for_c is lower than d, and so on.

I have a dynamic targetvalue (lets just go with 2 000 000 in this example)

I want to get the value where max_value is greater than the targetvalue, but sometimes max_value is not defined and then set to "0". "0" in max_value should mean unlimited "roof". The min_value can not be greater than targetvalue, but sometimes min_value is not defined and then set to "0". "0" min_value should mean a unlimited "floor".

I have tried with this code

if ($value_for_d > 0 ){
    if (($min_value_d <= $targetvalue) xor ($min_value_d == 0)){
        if (($max_value_d >= $targetvalue) xor ($max_value_d == 0)){
            $query_result = TRUE;
            $value = $value_for_d;
        }
    }
}elseif ($value_for_c > 0 ){
    if (($min_value_c <= $targetvalue) xor ($min_value_c == 0)){
        if (($max_value_c >= $targetvalue) xor ($max_value_c == 0)){
            $query_result = TRUE;
            $value = $value_for_c;
        }
    }
}elseif ($value_for_b > 0 ){
    if (($min_value_b <= $targetvalue) xor ($min_value_b == 0)){
        if (($max_value_b >= $targetvalue) xor ($max_value_b == 0)){
            $query_result = TRUE;
            $value = $value_for_b;
        }
    }
}elseif ($value_for_a > 0 ){
    if (($min_value_a <= $targetvalue) xor ($min_value_a == 0)){
        if (($max_value_a >= $targetvalue) xor ($max_value_a == 0)){
            $query_result = TRUE;
            $value = $value_for_a;
        }
    }
}

If I run this code with a targetvalue of "2 000 000", I get this result:

Entry 1 - 3.9 (correct value is 3.9)
Entry 2 - 0 (correct value is 4.2)
Entry 3 - 3.445 (correct value is 3.445)

If I set the targetvalue to even lower, to 500 000, I get 0 on all my entries.

© Stack Overflow or respective owner

Related posts about php

Related posts about if-statement