Forum Moderators: coopster

Message Too Old, No Replies

If Statement question

should be simple, isn't

         

ineedmoney

3:05 pm on Jul 7, 2006 (gmt 0)

10+ Year Member



Hello everyone

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

barns101

3:34 pm on Jul 7, 2006 (gmt 0)

10+ Year Member



I think that the if statement is testing whether $c is literally the string "two, three..." etc... But I'm not sure why it would evaluate as true if $c was simply "two".

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! ;) )

ineedmoney

3:47 pm on Jul 7, 2006 (gmt 0)

10+ Year Member



thankyou barns101

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

barns101

4:27 pm on Jul 7, 2006 (gmt 0)

10+ Year Member



I don't think that the source of the variable $c makes any difference.

Check out [php.net...]

ineedmoney

4:38 pm on Jul 7, 2006 (gmt 0)

10+ Year Member



Great Link!

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

coopster

4:46 pm on Jul 7, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




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

Yes, that is exactly what is happening. It is comparing it to the entire string. If you set the variable $c to the value "two" and then do the comparison it would work, which would clear this up ...


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;

barns101 probably has the best option here. Put the values into an array and check against that. It is a common practice. As a matter of fact you can also use those values to build your html then, a nice little bonus.
<?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>

ineedmoney

5:02 pm on Jul 7, 2006 (gmt 0)

10+ Year Member



Thanks Barns101 and Coopster,

I have been learning PHP for awhile now, its about time I learned about arrays, I have been avoiding them forever, haven't been able to fully grasp the concept, but now I get it :)

Thanks

coopster

5:20 pm on Jul 7, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You're welcome. I think once you get a grip on how arrays work you will find them quite handy. Especially if I don't confuse you -- I noticed in the code snippet above I did not create the array properly to give you your <option> list the way you wanted it. Should have been like this:
$possibilities = array( 
'first' => 'one',
'second' => 'two',
'third' => 'three'
);