Jump to content

Making Dropdown boxes from array.


daveoliveruk

Recommended Posts

I have a load of the array entries below, what I need to know is how to loop through them making the 'internal name' the id for a select box and the 'options' the options for the select box. Any help would be greatly appreciated!!

 

                                                        $data['profile:details'][] = (object)(array(
                                                                    "name" => __gettext("Are you circumsized? "),
                                                                    "internal_name" => "circumsized",
                                                                    "field_type" => "select",
                                                                    "options" => array(
                                                                        'yes'=>__gettext('Yes'),
                                                                        'no'=>__gettext('No'),
                                                                        'na'=>__gettext('Not Applicable')),
                                                                    "description" =>"",
                                                                    "user_type" => "person",
                                                                    "category" => __gettext("Your Personal Information"),
                                                                    "col1" => true,     
                                                                    "invisible" => false,
                                                                    "required" => false,
                                                                    ));
                                                                    
                                                                    
                                                        $data['profile:details'][] = (object)(array(
                                                                    "name" => __gettext("What type of body hair do you have?"),
                                                                    "internal_name" => "body_hair",
                                                                    "field_type" => "select",
                                                                    "options" => array(
                                                                        'yes'=>__gettext('Yes'),
                                                                        'no'=>__gettext('No'),
                                                                        'na'=>__gettext('Not Applicable')),
                                                                    "description" =>"",
                                                                    "user_type" => "person",
                                                                    "category" => __gettext("Your Personal Information"),
                                                                    "col1" => true,     
                                                                    "invisible" => false,
                                                                    "required" => false,
                                                                    ));

Link to comment
Share on other sites

<?php

// example array
$something = array('a' => 1, 'b' => 2, 'c' => 3);

echo "<form action = '' method = ''>";
echo "<select name = 'blah'>";
foreach($something as $key => $val) {
   echo "<option value = '$key'>$val</option>";
} // end foreach $something
echo "</select>";
echo "</form>";
?>

Link to comment
Share on other sites

PHP can loop through objects within an array too, so CV code still applies, example

 

foreach($data['profile:details'] as $key => $obj)
{
    echo '<p><b>' . $obj->name . "</b>\n";

    echo '<select>';
    foreach($obj->options as $key => $value)
    {
        echo "\n   <option value=\"$key\">$value</option>";
    }
    echo "\n</select><p>\n\n";
}

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.