Search code examples
phpstrpos

php strpos doing weird things


I have this code here and for some reason it will accept either the first 3 ifs as true or the last if. I can't seem to get it to go to one or the other without just removing one of them.

The input coming in for example is 'TBN D664' or 'Other' and it will sort out down there. But no matter what input I throw at it 'TAN D664' is always true.

Even if I rearrange them they still say that whatever statement is first, is true. I've used similar code elsewhere in my program and it works fine, only in this spot is it giving me troubles.

if($Category == 'Petrochemical') {

    if (strpos($_SESSION['Petrochemical_app'],'TAN D664') !== false) {
        $i=37;
    } 

    elseif (strpos($_SESSION['Petrochemical_app'],'TBN D2869') !== false) {
        $i=37;
    }

    elseif (strpos($_SESSION['Petrochemical_app'],'TBN D4739') !== false) {
        $i=37;
    }

    elseif ((strpos($_SESSION['Petrochemical_app'],'H2S') !== false) ||  
        (strpos($_SESSION['Petrochemical_app'],'Other') !== false)) {
        if( (strpos($sample,'75to120') !==false) || 
            (strpos($sample,'120to200') !==false) || 
            (strpos($sample,'200ormore') !==false)){
            $i=26;
        }
        if( (strpos($sample,'10to30') !== false) || 
            (strpos($sample,'less10') !== false)){
            $i=24;
        }
        if(strpos($sample,'30to75') !== false) {
            $i=25;
        }
    }
}

If it helps this is the checkbox where it gets this specific selection.

<fieldset>
  <b>Petrochemical</b><br />
  <input onclick="document.getElementById('selector_button').disabled = false; document.getElementById('charstype').disabled = true;"name="Petrochemical_app[]" type="checkbox" value="TAN D664" id="D664"> <label for="D664">TAN: D664</label>
  <input onclick="document.getElementById('selector_button').disabled = false; document.getElementById('charstype').disabled = true;"name="Petrochemical_app[]" type="checkbox" value="TBN D2869" id="D2869"> <label for="D2869">TBN: D2869</label>
  <input onclick="document.getElementById('selector_button').disabled = false; document.getElementById('charstype').disabled = true;"name="Petrochemical_app[]" type="checkbox" value="TBN D4739" id="D4739"> <label for="D4739">TBN: D4739</label>
  <input onclick="document.getElementById('selector_button').disabled = false; document.getElementById('charstype').disabled = true;"name="Petrochemical_app[]" type="checkbox" value="H2S and Mercaptan Bromine Number" id="H2S"> <label for="H2S">H2S and Mercaptan Bromine Number</label>
  <input onclick="document.getElementById('selector_button').disabled = false; document.getElementById('charstype').disabled = true;"name="Petrochemical_app[]" type="checkbox" value="Other" id="Other"> <label for="Other">Other: specify below</label>
  <label for="Other5">Please Specify:</label> <input name="Other5" type="text" id="Other5">
  </fieldset>

Here's where I turn it into a session

$_SESSION['Petrochemical_app']=$_POST['Petrochemical_app'];

Also when I do a print_r(); I get this or it changes whenever I make a different selection with the checkboxes

Array ( [0] => TBN D4739 )  

Also if I have all of them checked off I get this

Array ( [0] => TAN D664 [1] => TBN D2869 [2] => TBN D4739 [3] => H2S and Mercaptan Bromine Number [4] => Other ) 

P.S. Thanks for helping with the code format guys! I'm a long time stackoverflow creeper and this is my first post :P


Solution

  • After your updates the error is quite clear, I think.

    if (strpos($_SESSION['Petrochemical_app'],'TAN D664') !== false) {
        $i=37;
    } 
    

    should be

    if (in_array('TAN D664', $_SESSION['Petrochemical_app'])) {
        $i=37;
    } 
    

    Since the form has several checkboxes with the same name, the values are put into an array which you then save in the session. Your code used string functions on that array which obviously can't work. You want to check if a specific value is in the array and thus should use in_array([needle], [haystack]) to check that.