Jump to content

Echoing Sorted Array With <Option> Tag


j.silver

Recommended Posts

:-)

<!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'])) {            $multi_value = explode(",", trim($_POST['new_option']));            foreach ($multi_value as $array_item) {                if (!in_array(ucwords($array_item), $sk))                    array_push($sk, trim(ucwords($array_item)));            }        }        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 (separate multiple values with comma) </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...