Silver Posted October 18, 2014 Share Posted October 18, 2014 Hi everyone. I'm trying to echo the values of an array inside HTML's <option> tag. The following should be echod: <option value="Moose Jaw,SK,Canada">Moose Jaw,SK,Canada</option> <option value="Prince Albert,SK,Canada">Prince Albert,SK,Canada</option> <option value="Regina,SK,Canada">Regina,SK,Canada</option> <option value="Saskatoon,SK,Canada">Saskatoon,SK,Canada</option> The code I attempted was: <!DOCTYPE html> <html> <body> <?php $sk=array("Saskatoon","Regina","Moose Jaw", "Prince Albert"); sort($sk); foreach ($sk as $value) { echo '<option value="'.$value.',SK,Canada">'.$value.',SK,Canada</option>'; echo "<br>"; } ?> </body> </html> Pls. note that the values of the array are not sorted alphabetically. The output, however, should be sorted in ascending format and be within the <option> tag after adding the province and country. Any help will be highly appreciated. Link to comment Share on other sites More sharing options...
HartleySan Posted October 18, 2014 Share Posted October 18, 2014 You need to wrap the option elements in a select element. Link to comment Share on other sites More sharing options...
Silver Posted October 18, 2014 Author Share Posted October 18, 2014 Dear HartleySan, You are right, however, though the select element will be there in the final code, I ignored it here because I am able to manage it separately. I now want to do what I explained only, which I am unable to yet. Link to comment Share on other sites More sharing options...
HartleySan Posted October 18, 2014 Share Posted October 18, 2014 After your sort function call, do the following then: echo '<pre>'; print_r($sk); echo '</pre>'; 1 Link to comment Share on other sites More sharing options...
Silver Posted October 24, 2014 Author Share Posted October 24, 2014 Many thanks, HartleySan. In fact there is much code to it, but I am good to go now. Link to comment Share on other sites More sharing options...
Recommended Posts