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
https://forums.phpfreaks.com/topic/114686-making-dropdown-boxes-from-array/
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>";
?>

well that's just an example of looping through a simple array. You're obviously gonna have to apply it to your array. If you know how to echo out an individual target element then you can apply a foreach loop to that array, even if it's nested inside another array.

 

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";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.