Return to Snippet

Revision: 34799
at October 28, 2010 08:21 by geilt


Updated Code
function menu_generator($menu_vars="",$menu_postvalue="",$menu_values="", $blank=true, $selected="", $class="")
{
    if($selected)
	{
		$selector=$selected;
	}
	else
	{
		$selector=$_POST[$menu_postvalue];
	}
    if(!$menu_values)
    {
        $menu_values=$menu_vars;
    }
    else
    {
        $menu_values=$menu_values;
    }
    $menu_generate=str_replace(" ", " ", $menu_vars);
    $menu_generate2 = str_replace(" ", " ", $menu_values);	
    echo "<select name=\"$menu_postvalue\" $class >";
	if($blank)
	{
		echo "<option value=\"\"></option>";
	}
    for($i=0; $i < count($menu_generate); $i++)
        {
			$value=$menu_generate[$i];
			$value2=$menu_generate2[$i];
			if($value2 == $selector)
            {
                echo "<option value=\"$value2\" selected=\"selected\">$value</option>";
            }
            else
            {
                echo "<OPTION value=\"$value2\">$value</option>";
            }
        }
    echo "</select>";
}

$menu_vars = array("One","Two","Three");
$menu_postvalue = "Numbers";
menu_generator($menu_vars, $menu_postvalue);

Revision: 34798
at October 28, 2010 07:36 by geilt


Initial Code
function menu_generator($menu_vars="",$menu_postvalue="",$menu_values="", $blank=true, $selected="", $class="")
{
    if($selected)
	{
		$selector=$selected;
	}
	else
	{
		$selector=$_POST[$menu_postvalue];
	}
    if(!$menu_values)
    {
        $menu_values=$menu_vars;
    }
    else
    {
        $menu_values=$menu_values;
    }
    $menu_generate=str_replace(" ", " ", $menu_vars);
    $menu_generate2 = str_replace(" ", " ", $menu_values);	
    echo "<select name=\"$menu_postvalue\" $class >";
	if($blank)
	{
		echo "<option value=\"\"></option>";
	}
    for($i=0; $i < count($menu_generate); $i++)
        {
			$value=$menu_generate[$i];
			$value2=$menu_generate2[$i];
			if($value2 == $selector)
            {
                echo "<option value=\"$value2\" selected=\"selected\">$value</option>";
            }
            else
            {
                echo "<OPTION value=\"$value2\">$value</option>";
            }
        }
    echo "</select>";

Initial URL
http://www.esotech.org

Initial Description
I wrote this little bit of code myself so if you see where you can improve on it, let me know. This is great for making complex menu's and also for making sure your POST data gets saved and reselected when the screen refreshes. I use it in all my programs now, makes menu generation a snap.

In order to use this you need to make sure you set at minimum
$menu_vars as an array of values and $menu_postvalue as a string to the be the name of the POST field. By default, if no $menu_values is set, $menu_vars will be set both to be the display and value.

Example Usage in the Snippet

I look forward to your feedback.

Initial Title
PHP Option Select Menu Generator Function - Dropdown Menu

Initial Tags
php, html, function

Initial Language
PHP