Jump to content

IF Statements


danmaxito

Recommended Posts

Hey guys,
Problem: I have an html form that has a dropdown list. When the user click on a name from the dropdown list and hits submit, the name is stored in a session. Now, I am trying to write some php code that says:

If name = Freddy
  then $age = 4
elseif
name = Alex
  then $age = 6
elseif
name = Derek
  then $age = 2

How would I go about in doing this?

Thanks
Danny



Link to comment
Share on other sites

[code]
<?php
  $name = isset($_POST['name']) : $_POST['name'] ? '';
  switch ($name) {
    case 'Freddy':
      $age = 4;
      break;
    case 'Alex':
      $age = 6;
      break;
    case 'Derek':
      $age = 2;
      break;
  }
?>
[/code]
Link to comment
Share on other sites

Awesome! Thanks guys for your help.

As I was putting this code together (before I posted it here) I used the SWITCH method. I just wrote the code incorrectly, and since I am new to PHP, I was curious if I was doing things wrong.

Thorpe - I used your code (Thanks by the way), but I had to edit some of the code for it to work:

I changed [code]$name = isset($_POST['name']) : $_POST['name'] ? '';[/code]
to
[code]$name = $_POST["name"];[/code]

Am I doing something wrong here?

Danny
Link to comment
Share on other sites

thorpe, I appreciate all your help. If at anytime, I am bugging you, let me know.

Here is another problem that I am not understanding:
I set everything to run smoothly. User goes to webpage, selects his name, and a session is then created with the users info. Once the users goes back to the homepage, his previous session is destroyed, and he is asked for is name again. Why is it destroying the session when it was not instructed to do so???

I created an if statement on the home page that says, "If a session is created, then tell them that they are logged in as Freddy(Example) and give them the option to continue as Freddy, or to destroy the session and log in as someone else.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.