Jump to content

Dropdown list from an array


spertuit
Go to solution Solved by AbraCadaver,

Recommended Posts

I have a function that builds an array an works great.

 

Form

<?php
$name = 'logWeather';
$options = array( 'Fair', 'Windy', 'Rainy','Stormy');
$selected = $logWeather;

echo dropdown( $name, $options, $selected );

?>

Function

<?php
function dropdown( $name, array $options, $selected)
	{
    /*** begin the select ***/
    $dropdown = '<select name="'.$name.'" id="'.$name.'">'."\n";

    $selected = $selected;
    /*** loop over the options ***/
    foreach( $options as $key=>$option )
    {
        /*** assign a selected value ***/
        $select = $selected==$key ? ' selected' : null;

        /*** add each option to the dropdown ***/
        $dropdown .= '<option value="'.$key.'"'.$select.'>'.$option.'</option>'."\n";
    }

    /*** close the select ***/
    $dropdown .= '</select>'."\n";

    /*** and return the completed dropdown ***/
    return $dropdown;
	}
?>

This works perfectly to edit a dropdown built from an array, but I am trying to add in a little more functionality.

How can I add in a hidden value and a displayed value

 

ex in html

	   	<select name="logSeaCondition">
		  <option value="1">0-1 feet</option>
		  <option value="4">2-4 feet</option>
		  <option value="7">5-7 feet</option>
		  <option value="10">8-10 feet</option>
		  <option value="13">11-13 feet</option>
		  <option value="16">14-16 feet</option>
		  <option value="19">17-19 feet</option>
		  <option value="21">20+ feet</option>
		</select> 

Can I possibly pass in an associative array and use value as key and echo the item in the array?

Link to comment
Share on other sites

Geeze, all the things I tried and I didn't even think about it. Thanks a million!

 

Working code to pass in an associative array into a generated dropdown box and echoing stored selection using three parameters.

$name is the name of the drop down to retrieve your post variable, $options is your array, and $selected is your echoed variable from the database or null if it is a new form.

 

Form Side

$name = 'logSeaCondition';
$options = array(1=>'0-1 feet', 4=>'2-4 feet', 7=>'5-7 feet',10=>'8-10 feet',13=>'11-13 feet',16=>'14-16 feet',19=>'17-19 feet',20=>'20+ feet');
$selected = $logSeaCondition;

echo dropdown( $name, $options, $selected );

Function

	function dropdown( $name, array $options, $selected)
	{
    /*** begin the select ***/
    $dropdown = '<select name="'.$name.'" id="'.$name.'">'."\n";

    $selected = $selected;
    /*** loop over the options ***/
    foreach( $options as $key=>$option )
    {
        /*** assign a selected value ***/
        $select = $selected==$key ? ' selected' : null;

        /*** add each option to the dropdown ***/
        $dropdown .= '<option value="'.$key.'"'.$select.'>'.$option.'</option>'."\n";
    }

    /*** close the select ***/
    $dropdown .= '</select>'."\n";

    /*** and return the completed dropdown ***/
    return $dropdown;
	}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.