Forum Moderators: coopster
I in the process of writing a script to deal with a form on my site. I want to use a if statement that will check if a form value == several different things for example ($c)
if ($a == "one"){
if ($b == "black"){
if ($c == "two, three, four, five, six, seven"){
$d = 50;}}}
this works fine when $c looks like
if ($c == "two"){
But although it doesn't come up as an error, when $c looks like the first example it does not pass any value to $d
Can you tell me what I doing wrong? It would seem a waste of time to write a seperate if for each == in $c
Thankyou
You could add two, three etc... to an array called $possibilities and then use code similar to this:
if(in_array($c, $possibilities))
...
(There may be a better / more correct way to do this! ;) )
"I think that the if statement is testing whether $c is literally the string "two, three..." etc... "
That was the feeling I was getting, Can someone confirm this for us?
I am not very familiar with arrays, but I will give it a shot.
Are there any better/correct ways to do this?
Thanks
[edit]
I think it might help if I clarified that $c is the value of a
<select name="c" size="1">
<option value="one">first</option>
<option value="two">second</option>
<option value="three">third</option>
in the form that this code references, hope I was not stating the obvious. But after reading my first post, it may have seemed that $c was pulling values from checkboxes and not a drop down menu, if that even makes a difference :)
Please let me know if I need to post more of the code.
Check out [php.net...]
Thankyou, I have gotten this to work putting it into an array.
Incase someone has this questions later, I Changed the code in the first example to look like this:
$e = array("two", "three", "four", "five", "six", "seven");
if ($a == "one"){
if ($b == "black"){
if (in_array($c, $e)){
$d = 50;}}}
"I think that the if statement is testing whether $c is literally the string "two, three..." etc... "That was the feeling I was getting, Can someone confirm this for us?
$c = "two, three, four, five, six, seven";
if ($c == "two") {
print "match";
} else {
print "no match"; // prints this one
}
exit;
But I'm not sure why it would evaluate as true if $c was simply "two".
That is because of the second comparison example, barns101. ineedmoney was showing that if that piece of code was used as opposed to the original, it returns TRUE, which it indeed should:
$c = "two";
if ($c == "two") {
print "match"; // prints this one
} else {
print "no match";
}
exit;
<?php
$c = (isset($_POST['c'])) ? $_POST['c'] : '';
$possibilities = array("one", "two", "three");
if (in_array($c, $possibilities)) {
// handle accordingly
}
// Build select list from array key/value pairs:
$selectList = ''; // initialize
foreach ($possibilities as $key => $value) {
$selectList .= '<option value="' . $value . '">' . $key . "</option>\n";
}
?>
<select name="c">
<?php print $selectList;?>
</select>
$possibilities = array(
'first' => 'one',
'second' => 'two',
'third' => 'three'
);