PHP if statement

They have: 47 posts

Joined: Jun 2001

Hi all,
I am trying to make do some simple database search functionality using php / MySQL. .. I am trying to search 3 different tables depending on what the user select in an oprtion field. So I basically have three querries one of which is called when a particular field is selected.

I am trying to use the an if statement to display the appropriate results on the result page but for some reason, it is showing all 3 section instead of one... what I have is something like:
*************************************

<?php
if ($option = 1) {
?>

Html with PHP Querry display for Option1

<?php
}
?>

<?php
if ($option = 2) {
?>

Html with PHP Querry display for Option2

<?php
}
?>

<?php
if ($option = 3) {
?>

Html with PHP Querry display for Option3

<?php
}
?>

***********************************
I would expect that if $option = 1 the page would only display Html with Querry display for Option1... Is that correct? ...if so then what am I doing wrong?
Thanks
Mizzy

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

= is the assignment operator
== is the comparison operator

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

To illustrate:

<?php
if ($option <strong>==</strong> 1) {
?>

Html with PHP Query display for Option1

<?php
}
?>

They have: 47 posts

Joined: Jun 2001

oh...thats it Smiling
thanks

Busy's picture

He has: 6,151 posts

Joined: May 2001

There are also other ways to do what your doing: if elseif else, and switch case, these two would give you a default, so if no option was choosen or the wrong option the main section would show up.

if($option == 1) { do stuff }
elseif ($option == 2) { do other stuff }
elseif ($option == 3) {do more stuff }
else { do the default }

or the other

switch ($option) {
case 0:
print "do something";
break;
case 1:
print "do something else";
break;
case 2:
print "do more somethings";
break;
default:
print "the default something";
}

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.