Jump to content

Echoing Sorted Array With <Option> Tag


j.silver

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. 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.
Any help will be highly appreciated.
Link to comment
Share on other sites

That's what I see

 

what are your developer tools saying?

Edited by niche
Link to comment
Share on other sites

I just added the SELECT tag. It has 4 options in alpha order.

<!DOCTYPE html><html><body><select><?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>";}//<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>?></select></body></html>
Link to comment
Share on other sites

Many thanks niche for the help and speedy response. Yes, it works fine when <select> is added. However, I am keen to get it work without the <select> tag for a reason too lengthy to address, hence not worth mentioning. I hope it is not impossible to achieve it without <select>, and any assistance will be highly appreciated.

Link to comment
Share on other sites

Another thing is that the above code (with select) makes the output in a drop down menu. The final output I am looking for should be a complete sorted lines of output (not in a drop down menu), exactly as:

 

<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>

Link to comment
Share on other sites

Thanks again niche. It is not radio, nor check box. It is indeed a drop down menu, but not in its final shape. I am looking for a code that will produce the list of options as given earlier, i.e., a complete list of options sorted alphabetically and ready to be inserted within the select tags at a later stage. Now, I want an output in a complete list, sorted alphabetically exactly as given earlier.

Link to comment
Share on other sites

I am having them separate in a stand-alone script to continue updating and modifying them in time and away from the final code. Once complete and in their final list of options, insert them within the select tags of the final code. You might ask: why then you don't include them within the select tags in the final code and keep adding to the final code. The answer is: because other future options will have different value type than their predecessors, so if I will add them to the same select tags it will mess up the whole list.

Link to comment
Share on other sites

Many thanks Ingolme for your input. I never thought PHP might be limited in echoing such a result. I regret that I do not know JS. If you may help me with the code that you think would do the requirement, it would be appreciated and might be my first step towards learning JS.

Link to comment
Share on other sites

Dear niche, I really thank you very much for the sincere efforts. I tried to make it dynamic, but couldn't work due to the different types of data. The only option left for me really is to make it a stand alone script, get it to sort the data separately, then only insert it within the final code.

Link to comment
Share on other sites

are you using a localhost lake wampserver that has a database?

Edited by niche
Link to comment
Share on other sites

Though I have not really thought of or tried to base my inputs on a sql query, I think it will not workas I want it to be because my end result should be a list of <options></options>, both tags inclusive.

Link to comment
Share on other sites

<!DOCTYPE html><html>    <head>        <meta charset="UTF-8">        <title>show html on page using php</title>        <style type="text/css">        </style>    </head>    <body>        <?php        $checked = '';        if (isset($_GET['pre_or_html']) && !empty($_GET['pre_or_html'])) {            $checked = ' checked="checked"';        }        ?>        <form method="get">            <label>HTML select</label><input type="checkbox" <?php echo $checked; ?> name="pre_or_html" value="html">            <input type="submit">        </form>        <?php        $sk = array("Saskatoon", "Regina", "Moose Jaw", "Prince Albert");        sort($sk);        $txt = "";        foreach ($sk as $value) {            $txt.='<option value="' . $value . ',SK,Canada">' . $value . ',SK,Canada</option>' . "n";        }        if (isset($_GET['pre_or_html']) && !empty($_GET['pre_or_html'])) {            echo '<select>' . $txt . '</select>';        } else {            echo '<pre>' . htmlspecialchars($txt) . '</pre>';        }        ?>    </body></html>
Link to comment
Share on other sites

For inputting values for options

<!DOCTYPE html><html>    <head>        <meta charset="UTF-8">        <title>show html on page using php</title>        <style type="text/css">        </style>    </head>    <body>        <?php        $checked = '';        $sk = array("Saskatoon", "Regina", "Moose Jaw", "Prince Albert");        if (isset($_POST['currentArray']) && !empty($_POST['currentArray']) && count(unserialize($_POST['currentArray'])) > count($sk)) {            $sk = unserialize($_POST['currentArray']);        }        if (isset($_POST['new_option']) && !empty($_POST['new_option']) && !in_array($_POST['new_option'], $sk)) {            array_push($sk, $_POST['new_option']);        }        if (isset($_POST['pre_or_html']) && !empty($_POST['pre_or_html'])) {            $checked = ' checked="checked"';        }        $currentArray = $sk;        echo count($currentArray);        if (isset($_POST['currentArray'])) {            echo '<br>values: ';            print_r($currentArray);        }        sort($sk);        ?>        <form method="post">            <input type="hidden" value='<?php echo serialize($currentArray); ?>'  name="currentArray">            <label>HTML select</label><input type="checkbox" <?php echo $checked; ?> name="pre_or_html" value="html">            <label>New option</label><input type="text"  name="new_option" value="">            <input type="submit">        </form>        <?php        $txt = "";        foreach ($sk as $value) {            $txt.='<option value="' . $value . ',SK,Canada">' . $value . ',SK,Canada</option>' . "n";        }        if (isset($_POST['pre_or_html']) && !empty($_POST ['pre_or_html'])) {            echo '<select>' . $txt . '</select>';        } else {            echo '<pre>' . htmlspecialchars($txt) . '</pre>';        }        ?>    </body></html>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...