Jump to content
Larry Ullman's Book Forums

Echoing Sorted Array With <Option> Tag


Recommended Posts

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

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

 Share

×
×
  • Create New...