mrt003003 Posted April 28, 2011 Share Posted April 28, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/235032-listmenu-if-statement-options/ Share on other sites More sharing options...
cssfreakie Posted April 28, 2011 Share Posted April 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235032-listmenu-if-statement-options/#findComment-1207898 Share on other sites More sharing options...
cssfreakie Posted April 28, 2011 Share Posted April 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235032-listmenu-if-statement-options/#findComment-1207905 Share on other sites More sharing options...
mrt003003 Posted April 29, 2011 Author Share Posted April 29, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/235032-listmenu-if-statement-options/#findComment-1208101 Share on other sites More sharing options...
cssfreakie Posted April 29, 2011 Share Posted April 29, 2011 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!'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/235032-listmenu-if-statement-options/#findComment-1208130 Share on other sites More sharing options...
mrt003003 Posted April 29, 2011 Author Share Posted April 29, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/235032-listmenu-if-statement-options/#findComment-1208250 Share on other sites More sharing options...
mrt003003 Posted April 29, 2011 Author Share Posted April 29, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235032-listmenu-if-statement-options/#findComment-1208259 Share on other sites More sharing options...
cssfreakie Posted May 1, 2011 Share Posted May 1, 2011 Good one, hope you also see that instead of directly echoing stuff i set a variable. It keeps your html mark-up a bit more cleaner. Quote Link to comment https://forums.phpfreaks.com/topic/235032-listmenu-if-statement-options/#findComment-1208849 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.