Jump to content

List/Menu If statement options


mrt003003

Recommended Posts

Hi there,

 

Im trying to build a drop down menu that has an if statement to determine the results, heres the code below:

 

  <option value="<?php echo $row_Planet['PlanetName']?>"<?php if ($row_Planet['PlanetName'] == 'Mon Calamari'){ 
echo"Yavin";
echo"Denab" ?><?php echo $row_Planet['PlanetName']?></option>

 

Im really unsure of the syntax. If you could help that would be ace! Thanks :)

Link to comment
Share on other sites

well, besides the fact that your missing a semicolon, can you maybe tell what options you want and when so not code just say:

 

if this than that etc. right now i see only 2 potential options in your code:

1 If planet name is equal to mon calimari

2 echo Yavin

3 else

4 echo denab.

 

and are you sure you want a option list, because if you only have 2 options and you only going to show one, its a bit odd.

Link to comment
Share on other sites

in addition to the above is this maybe useful for you?

 

<?php
$row_Planet['PlanetName'] = 'Mon Calamari'; // just some test input

if($row_Planet['PlanetName'] == 'Mon Calamari'){
    $value1 = 'Yavin';
    $value2 = 'Denab';
}else{
    $value1 = 'fatmonkeys';
    $value2 = 'gorilla\'s';
}
?>


<select name="gorilla">
     <option><?php echo $value1; ?></option>
     <option><?php echo $value2; ?></option>
</select>

 

so instead of directly echo-ing a value i first set it to keep the html more readable.

Link to comment
Share on other sites

Hi there thanks for the reply, the idea is that i have 6 planets and each planet has 2 possible static options in the menu.

 

1 If planet name is equal to mon calimari

2 echo Yavin

3 else

4 echo denab.

 

1 If planet name is equal to Yavin

2 echo Yagor Minor

3 else

4 echo Corella.... etc...

 

hope ive explained my self well enough.

 

Thanks :)

Link to comment
Share on other sites

hmm in that case i rather use a swicth case. By the way I have a feeling your Idea of an if-statement is incorrect. If you have 2 static options per planet you need to echo out 2 or set 2 variables. I made something below that works. 6 planets to choose from and you can afterwards choose a race. Hope it's useful, and it encourages you to code the rest yourself a bit. since it's pretty self explanatory.

 

<form action="" method="post">
    <select name="PlanetName" size="6">
        <option>Mon Calimari</option>
        <option>Earth</option>
        <option>Mars</option>
        <option>Venus</option>
        <option>Zoo1</option>
        <option>Zoo2</option>
    </select>
    <input type="submit" value="go" name="submit" />
</form>

<?php

if(isset($_POST['submit']) && !empty($_POST['PlanetName'])){


$row_Planet['PlanetName'] = $_POST['PlanetName'];

        // get the value for this planet somewhere
        switch ($row_Planet['PlanetName']) {
            case 'Mon Calimari': //planet name
                $race1 = 'yavin'; //first static race
                $race2 = 'fatmonkeys'; //second static race
            case 'Earth':
                $race1 = 'Humans';
                $race2 = 'Dolphins';
                break;
            case 'Mars':
                $race1 = 'Snickers';
                $race2 = 'Milky ways';
                break;
            case 'Venus':
                $race1 = 'B**ches';
                $race2 = 'H*OOOs';
                break;
            case 'Zoo1':
                $race1 = 'Gorillas';
                $race2 = 'Banana\'s';
                break;
            case 'Zoo2':
                $race1 = 'Chimps';
                $race2 = 'Melons';
                break;
        }//end switch
$toggleoptions = true; // set a variable, to show or not show the race selector
}

// create a list after a planet has been chosen.
if(!empty($row_Planet['PlanetName'])&& $toggleoptions !== false){
    echo '<select name="race">';
    echo '<option>'.$race1.'</option>';
    echo '<option>'.$race2.'</option>';
    echo '</select>';
}else{

    echo 'select a planet!';
}


?>

Link to comment
Share on other sites

Hi there, thanks for your example.

 

I've put it all together:

 

	 <select name="PlanetName" size="6">
        <option>Corella</option>
        <option>Dantooine</option>
        <option>Denab</option>
        <option>Mon Calamari</option>
        <option>Yagor Minor</option>
        <option>Yavin</option>
    </select> 
<?php

if(isset($_POST['submit']) && !empty($_POST['PlanetName'])){


$row_Planet['PlanetName'] = $_POST['PlanetName'];

        // get the value for this planet somewhere
        switch ($row_Planet['PlanetName']) {
            case 'Corella': //planet name
                $race1 = 'Dantooine'; //first static race
            case 'Dantooine':
                $race1 = 'Corella';
                $race2 = 'Yagor Minor';
                break;
           case 'Denab':
                $race1 = 'Yagor Minor';
                $race2 = 'Mon Calamari';
                break;
            case 'Mon Calamari':
                $race1 = 'Denab';
                $race2 = 'Yavin\'s';
                break;
		case 'Yagor Minor':
                $race1 = 'Dantooine';
                $race2 = 'Denab';
                break;
            case 'Yavin':
                $race1 = 'Mon Calamari';
                
                break;
        }//end switch
$toggleoptions = true; // set a variable, to show or not show the race selector
}

// create a list after a planet has been chosen.
if(!empty($row_Planet['PlanetName'])&& $toggleoptions !== false){
    echo '<select name="race">';
    echo '<option>'.$race1.'</option>';
    echo '<option>'.$race2.'</option>';
    echo '</select>';
}else{

    echo 'select a planet!';
}
?>
      </select>

 

As you can see two of the planets have only 1 option to choose from and ive catered for this.. Its not quite working though and is throwing undefined variable errors for $toggleoptions, $race1 and $race2.. Are they not defined in the switch above?

 

What a, i missing?

 

Thank you :)

Link to comment
Share on other sites

I got it working using your earlier example using if statement:

 

 <?php


if($row_Fleet['PlanetName'] == 'Mon Calamari'){
    $value1 = 'Yavin';
    $value2 = 'Denab';
} elseif ($row_Fleet['PlanetName'] == 'Denab'){
    $value1 = 'Mon Calamari';
    $value2 = 'Yagor Minor';
}
?>


<select name="select">
     <option><?php echo $value1; ?></option>
     <option><?php echo $value2; ?></option>
</select>

 

I can now easily add more planets..

 

Thank you for your help.

 

:D

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.