LivinInChina Posted September 1, 2012 Share Posted September 1, 2012 Hi everyone! I have a form which sets its values from a $_POST of itself or defaults values which I manually set. What I'd like is to build the drop-down list dynamically. So for that, I want it to display in the first option selected either a default value (here large) or the $_POST('fontSize') you'll see in the following code. And if the $_POST('fontSize') or the defaukt value is selected in the first option tag so this value SHOULD NOT appear again in the drop-down list. And that's where I block. <form action="monform.php" method="post"> <p> <label for="fontSize">Taille :</label> <select id="fontSize" name="fontSize"> <?php $post_size = 25; $size_list = array( 20 => 'tiny', 25 => 'small', 30 => 'medium', 40 => 'large' ); foreach ($size_list as $size => $name) { if(intval($post_size) == $size){ echo "<option value='".$size."' selected='selected'>".$name."</option>"; }else{ echo "<option value='".$size."'>".$name."</option>"; } } ?> </select> </p> </form> Thganks a lot in advance for your help! Quote Link to comment https://forums.phpfreaks.com/topic/267884-dynamic-drop-down-list-with-unique-option-selected-name/ Share on other sites More sharing options...
darkfreaks Posted September 1, 2012 Share Posted September 1, 2012 you got some code wrong you are putting the $key where $value should be. <?php $post_size = 25; $size_list = array( 20 => 'tiny', 25 => 'small', 30 => 'medium', 40 => 'large' ); foreach ($size_list as $size => $name) { if(intval($post_size) === $name){ echo "<option value='".$name."' selected='selected'>".$name."</option>"; }else{ echo "<option value='".$name."'>".$name."</option>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/267884-dynamic-drop-down-list-with-unique-option-selected-name/#findComment-1374466 Share on other sites More sharing options...
LivinInChina Posted September 1, 2012 Author Share Posted September 1, 2012 Thanks darkfreaks for you interest. Though it still doesn't work but check this out, this code is cleaner but I still have the duplicate name in the list that I would like to dynamically get rid! <form action="test.php" method="post"> <p> <label for="fontSize">Taille :</label> <select id="fontSize" name="fontSize"> <?php $post_size = intval(isset($_POST['fontSize'])? $_POST['fontSize'] : 25); $size_list = array( 20 => 'tiny', 25 => 'small', 30 => 'medium', 40 => 'large' ); foreach ($size_list as $size => $name) { echo "<option value='".$size."'". ($post_size == $size? " selected='selected'" : "") .">".$name."</option>"; } ?> </select> </p> <p> <input name="submit" type="submit" value="envoyer" /> </p> </form> Quote Link to comment https://forums.phpfreaks.com/topic/267884-dynamic-drop-down-list-with-unique-option-selected-name/#findComment-1374467 Share on other sites More sharing options...
darkfreaks Posted September 1, 2012 Share Posted September 1, 2012 you are still doing it wrong. you should have something like foreach($values as $key => $value) { $selected = ($value= $post_size==$value) ? "selected='selected'" : ""; echo "<option value='$value' $selected>$value</option>"; } Quote Link to comment https://forums.phpfreaks.com/topic/267884-dynamic-drop-down-list-with-unique-option-selected-name/#findComment-1374470 Share on other sites More sharing options...
LivinInChina Posted September 1, 2012 Author Share Posted September 1, 2012 oh ok, so I tried with your last code but it doesn't work, what do you think it comes from? <form action="test.php" method="post"> <p> <label for="fontSize">Taille :</label> <select id="fontSize" name="fontSize"> <?php $post_size = intval(isset($_POST['fontSize'])? $_POST['fontSize'] : 25); $size_list = array( 20 => 'tiny', 25 => 'small', 30 => 'medium', 40 => 'large' ); foreach($size_list as $size => $name) { $selected = ($type= $post_size==$name) ? "selected='selected'" : ""; echo "<option value='".$name."' ".$selected.">".$name."</option>"; } ?> </select> </p> <p> <input name="submit" type="submit" value="envoyer" /> </p> </form> Thanks a lot for your help! Quote Link to comment https://forums.phpfreaks.com/topic/267884-dynamic-drop-down-list-with-unique-option-selected-name/#findComment-1374473 Share on other sites More sharing options...
darkfreaks Posted September 1, 2012 Share Posted September 1, 2012 why are you using dots and doublequotes abit uneeded much look at my code it does not use them. Quote Link to comment https://forums.phpfreaks.com/topic/267884-dynamic-drop-down-list-with-unique-option-selected-name/#findComment-1374474 Share on other sites More sharing options...
LivinInChina Posted September 1, 2012 Author Share Posted September 1, 2012 I use the dots and doublequotes only for a clearer view of my code because this list is only a part or my page so if I can have my variables highlighted it makes it easier to see 'em at a glance. To get back on this code, not only it displays the duplicated selected name in the drop down, but also when I submit the form, the selected element is always large I'm confused, this code is supposed to be easy but I'm relly having a big headache here. I might be tired Quote Link to comment https://forums.phpfreaks.com/topic/267884-dynamic-drop-down-list-with-unique-option-selected-name/#findComment-1374476 Share on other sites More sharing options...
darkfreaks Posted September 1, 2012 Share Posted September 1, 2012 changed your ternary it was abit off..... <?php $post_size = isset($_POST['fontSize']) ? intval($_POST['fontSize']) : 25; $size_list = array( 20 => 'tiny', 25 => 'small', 30 => 'medium', 40 => 'large' ); foreach($size_list as $size => $name) { $selected = ($type= $post_size===$name) ? "selected='selected'" : ""; echo "<option value='".$name."' ".$selected.">".$name."</option>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/267884-dynamic-drop-down-list-with-unique-option-selected-name/#findComment-1374477 Share on other sites More sharing options...
LivinInChina Posted September 1, 2012 Author Share Posted September 1, 2012 Thanks now it's working properly when submitted. However I start to wonder if this is possible to have this for instance: <selected> Large Tiny Small Medium INSTEAD OF THIS: <selected> Tiny Tiny Small Medium Large No more duplicate! But I guess it's not possible, maybe using some js, but in that case I prefer not. Quote Link to comment https://forums.phpfreaks.com/topic/267884-dynamic-drop-down-list-with-unique-option-selected-name/#findComment-1374478 Share on other sites More sharing options...
darkfreaks Posted September 1, 2012 Share Posted September 1, 2012 have you tried using array_unique to remove duplicate values in your array Quote Link to comment https://forums.phpfreaks.com/topic/267884-dynamic-drop-down-list-with-unique-option-selected-name/#findComment-1374479 Share on other sites More sharing options...
Barand Posted September 1, 2012 Share Posted September 1, 2012 There aren't necessarily any duplicates. It's the way dropdowns work. The selected option is displayed in the "box". When you press the mouse button it drops down and displays all the options with the selected one highlighted in the list. The way round is probably to write your own dropdown component, or modify with JS, so that it exhibits a different behaviour Quote Link to comment https://forums.phpfreaks.com/topic/267884-dynamic-drop-down-list-with-unique-option-selected-name/#findComment-1374488 Share on other sites More sharing options...
PFMaBiSmAd Posted September 1, 2012 Share Posted September 1, 2012 All it would take to NOT display the already selected/first entry inside the loop is an if(){} statement to skip outputting the <option> for that entry. Quote Link to comment https://forums.phpfreaks.com/topic/267884-dynamic-drop-down-list-with-unique-option-selected-name/#findComment-1374489 Share on other sites More sharing options...
PFMaBiSmAd Posted September 1, 2012 Share Posted September 1, 2012 <form action="test.php" method="post"> <p> <label for="fontSize">Taille :</label> <select id="fontSize" name="fontSize"> <?php $post_size = intval(isset($_POST['fontSize'])? $_POST['fontSize'] : 25); $size_list = array( 20 => 'tiny', 25 => 'small', 30 => 'medium', 40 => 'large' ); // output the selected/default option - echo "<option value='$post_size' selected='selected'>{$size_list[$post_size]}</option>"; foreach($size_list as $size => $name){ // output the remainider of the non-selected options - if($post_size !=$size){ echo "<option value='$size'>$name</option>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/267884-dynamic-drop-down-list-with-unique-option-selected-name/#findComment-1374490 Share on other sites More sharing options...
LivinInChina Posted September 1, 2012 Author Share Posted September 1, 2012 Thanks a lot to all of you for your help. As I already imagined and as Barand confirms, there is no way around, this is just the way the box is designed. If I want to make it my way, js is my answer but right now I'm not interested in that solution so I'll keep it like that. Again thanks to all of you Quote Link to comment https://forums.phpfreaks.com/topic/267884-dynamic-drop-down-list-with-unique-option-selected-name/#findComment-1374491 Share on other sites More sharing options...
Barand Posted September 1, 2012 Share Posted September 1, 2012 All it would take to NOT display the already selected/first entry inside the loop is an if(){} statement to skip outputting the <option> for that entry. But could it still display the selected option if it were no longer in the list? Quote Link to comment https://forums.phpfreaks.com/topic/267884-dynamic-drop-down-list-with-unique-option-selected-name/#findComment-1374508 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.