pxxb Posted October 1, 2013 Share Posted October 1, 2013 Hi, I am trying to make a scroll list that is multi-selectable. I am having trouble generating the scroll list and having the array be in it. I am new to php so any help would be nice. Thank you. $fruits = array( "--" => "---Please Select a Fruit ---", "ap"=>"Apple", "ba"=>"Banana", "ga"=>"Grapes", "wa"=>"Watermelon", "pe"=>"Pear"); <fieldset><legend> Fruits</legend><select name = "fruits"><?php foreach ($fruits as $fruit){ ?><option value = ""><?php $fruit ?></option><?php } ?></select></fieldset> Link to comment https://forums.phpfreaks.com/topic/282592-need-help-with-my-plugging-array-into-scroll-list-code/ Share on other sites More sharing options...
Ch0cu3r Posted October 1, 2013 Share Posted October 1, 2013 I assume you have the $fruit = array(...) code wrapped in <?php and ?> tags? If you have then you you've almost got it. The <?php $fruit ?> should be <?php echo $fruit ?>. If you want to set the value="" for each option, eg ap, ba, ga etc. Then the foreach loop construct needs to be foreach ($fruits as $key => $fruit) to retrieve the key value pairs from the $fruits associative array. Now you can do <option value="<?php echo $key; ?>"> to set each options value Corrected code <?php $fruits = array( "--" => "---Please Select a Fruit ---", "ap" =>"Apple", "ba" =>"Banana", "ga" =>"Grapes", "wa" =>"Watermelon", "pe" =>"Pear" ); ?> <fieldset> <legend> Fruits</legend> <select name = "fruits"> <?php foreach ($fruits as $key => $fruit){ ?> <option value = "<?php echo $key ?>"><?php echo $fruit ?></option> <?php } ?> </select> </fieldset> Link to comment https://forums.phpfreaks.com/topic/282592-need-help-with-my-plugging-array-into-scroll-list-code/#findComment-1451996 Share on other sites More sharing options...
pxxb Posted October 1, 2013 Author Share Posted October 1, 2013 Thank you, it worked when I executed it seperately. This code is part of a bigger program that I am working on and for somereason it wont display the $fruits. :\ trying to figure out why haha I assume you have the $fruit = array(...) code wrapped in <?php and ?> tags? If you have then you you've almost got it. The <?php $fruit ?> should be <?php echo $fruit ?>. If you want to set the value="" for each option, eg ap, ba, ga etc. Then the foreach loop construct needs to be foreach ($fruits as $key => $fruit) to retrieve the key value pairs from the $fruits associative array. Now you can do <option value="<?php echo $key; ?>"> to set each options value Corrected code <?php $fruits = array( "--" => "---Please Select a Fruit ---", "ap" =>"Apple", "ba" =>"Banana", "ga" =>"Grapes", "wa" =>"Watermelon", "pe" =>"Pear" ); ?> <fieldset> <legend> Fruits</legend> <select name = "fruits"> <?php foreach ($fruits as $key => $fruit){ ?> <option value = "<?php echo $key ?>"><?php echo $fruit ?></option> <?php } ?> </select> </fieldset> Link to comment https://forums.phpfreaks.com/topic/282592-need-help-with-my-plugging-array-into-scroll-list-code/#findComment-1451999 Share on other sites More sharing options...
pxxb Posted October 1, 2013 Author Share Posted October 1, 2013 Would anyone know why the drop list won't display until I press submit on my page? Link to comment https://forums.phpfreaks.com/topic/282592-need-help-with-my-plugging-array-into-scroll-list-code/#findComment-1452004 Share on other sites More sharing options...
pxxb Posted October 1, 2013 Author Share Posted October 1, 2013 nvm found my error. Link to comment https://forums.phpfreaks.com/topic/282592-need-help-with-my-plugging-array-into-scroll-list-code/#findComment-1452006 Share on other sites More sharing options...
Ch0cu3r Posted October 1, 2013 Share Posted October 1, 2013 Would anyone know why the drop list won't display until I press submit on my page? Post your full code here. The snippet of code you posted doesn't really help to solve the issue you just mentioned. Link to comment https://forums.phpfreaks.com/topic/282592-need-help-with-my-plugging-array-into-scroll-list-code/#findComment-1452007 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.