php-strpos做着奇怪的事情


php strpos doing weird things

我这里有这段代码,出于某种原因,它会接受前3个if为true或最后一个if。如果不删除其中一个,我似乎无法让它转到其中一个。

例如,输入是"TBN D664"或"其他",它会在下面排序。但无论我投入什么,"TAN D664"总是正确的。

即使我重新排列它们,它们仍然会说,无论哪种说法是第一位的,都是正确的。我在程序的其他地方也使用过类似的代码,它运行得很好,只是在这个地方给我带来了麻烦。

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;
        }
    }
}

如果它有帮助的话,这就是它获得特定选择的复选框。

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

这是我把它变成会话的地方

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

还有当我打印()时;每当我用复选框进行不同的选择时,我就会得到这个或它会改变

Array ( [0] => TBN D4739 )  

此外,如果我检查了所有这些,我会得到这个

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

附言:谢谢你帮助代码格式的家伙!我是一个长期堆叠的爬行者,这是我的第一个帖子:P

更新后,我认为错误非常明显。

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

应该是

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

由于表单有几个同名的复选框,因此这些值将被放入一个数组中,然后保存在会话中。您的代码在该数组上使用了字符串函数,这显然是不起作用的。您想检查数组中是否有特定的值,因此应该使用in_array([needle], [haystack])进行检查。