Hyperjase Posted November 19, 2011 Share Posted November 19, 2011 Hiya, Trying to get this working based on a drop down posted from the step before. Three options are dropoff, collect and ship. This code always defaults to the first option, despite the fact that echoing the session shows the correct choice has been passed through. Here's what I have: if ($_SESSION['collection'] = "dropoff") { echo "Please drop it off with us"; } elseif ($_SESSION['collection'] = "collect") { echo "Please collect it from me"; } else { echo "Please ship it to you"; } Any ideas what I'm missing here? Thanks! Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted November 19, 2011 Share Posted November 19, 2011 You're using an assignment operator = where you should be using a comparison operator == . . . Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 19, 2011 Share Posted November 19, 2011 And, I'll throw this out there . . . even though you only have three options right now, using a series of if . .elseif . . else statements can get messy. This is exactly what the switch() operator is for. This may look to be more work because of the additinoal lines of code, but it will be much more flexible and less error prone. switch($_SESSION['collection']) { case 'dropoff': echo "Please drop it off with us"; break; case 'collect': echo "Please collect it from me"; break; case 'ship': default: echo "Please ship it to you"; break; } Quote Link to comment Share on other sites More sharing options...
Hyperjase Posted November 19, 2011 Author Share Posted November 19, 2011 Perfect! Knew it'd be something simple I'd missed, thanks! Regarding the switch/case, will that work within a switch/case that is already in progress? Cheers! Jason Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 20, 2011 Share Posted November 20, 2011 Regarding the switch/case, will that work within a switch/case that is already in progress? No idea what you mean. But a switch will work wherever you put it - assuming you have coded it correctly. Quote Link to comment Share on other sites More sharing options...
Hyperjase Posted November 20, 2011 Author Share Posted November 20, 2011 I tested out after I posted my reply, it does work, I think I should word it better - it's nested within a case and switch already, so would be a case within a switch which is inside a primary switch and case. Hopefully that makes it sound easier! Sorry for the confuddlement! Jason Quote Link to comment Share on other sites More sharing options...
killervastu Posted November 20, 2011 Share Posted November 20, 2011 try this if ($_SESSION['collection'] == "dropoff") { echo "Please drop it off with us"; } elseif ($_SESSION['collection'] == "collect") { echo "Please collect it from me"; } else { echo "Please ship it to you"; } Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 21, 2011 Share Posted November 21, 2011 I tested out after I posted my reply, it does work, I think I should word it better - it's nested within a case and switch already, so would be a case within a switch which is inside a primary switch and case. Hopefully that makes it sound easier! Sorry for the confuddlement! Jason yes, you can have a switch() inside a switch() just like you can have a while() inside a while(), an if() inside an if(), etc. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.