Jump to content

How Would Your Write This If Statement Using Php?


Phreekster
 Share

Recommended Posts

On my database I have multiple records of farmers. Each farmer can plant apples or oranges. I want my if statement to output if they plant both, one or the other or neither.

if ($croptype == "apples" or $croptype == "oranges")	 {   	echo "<b>You plant .$croptype.</b><p />";	    } elseif ($croptype == "apples" && $croptype == "oranges")	 {   	echo "<b>You plant both types.</b><p />";	    } else($croptype != "apples" or $croptype != "oranges")	 {   	echo "<b>You plant both types.</b><p />";	    }

That is what I have come up with but it does not do what I want. Is there a way to do this using if/else statements? I am probably going about this completely the wrong way too but I am a novice when it comes to PHP. Any help is appreciated. Thank you.

  • Like 1
Link to comment
Share on other sites

well, what it really comes down to is how you get $croptype back from the database. How would you record it in the DB that they plant both? Would it return "both"? What would be the best way is to make a table of crops, and then make a lookup table that creates a record for every farmer, and for every crop he grows. For now though, it it's just easier to have it return "both", "none", or a crop, you could do this:

echo "You grow" . $croptype;

Link to comment
Share on other sites

well, what it really comes down to is how you get $croptype back from the database. How would you record it in the DB that they plant both? Would it return "both"? What would be the best way is to make a table of crops, and then make a lookup table that creates a record for every farmer, and for every crop he grows. For now though, it it's just easier to have it return "both", "none", or a crop, you could do this:
echo "You grow" . $croptype;

The croptype variable is called here I believe:
while ($dkROWrecord2  = mysql_fetch_array($dkResultSet2,MYSQL_BOTH))   {  $countIt=$countIt+1;        $name=$dkROWrecord2['name'];   $number=$dkROWrecord2['number'];  $city=$dkROWrecord2['city'];     $croptype=$dkROWrecord2['croptype'];

Will the if statements posted above work though? It doesn't seem they do. In the database this is how the data is laid out: crops.png Using their number, a farmer log in to the page and could see which crops they plant. Does both need to be defined somewhere or can it not be determined in the code in cases such as Smith and Williams case? Thank you for the reply. :)

Link to comment
Share on other sites

like I said, it all depends on how you are getting back croptype. In order to know that, we would need to see the query. To do it with a query, I would probably SELECT on the croptype field, WHERE the number equals whatever farmer number you are using, and then make it DISTINCT. from there you should get an array with either apple, orange, both, or none. From there, you could do something like this

//assumes $result is a query result resourceif(in_array("apples", $result) && in_array("oranges", $result)){  //both}else if(in_array("apples", $result) || in_array("oranges", $result)){  //one or the other}else{  //none};

Edited by thescientist
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...