Welcome Guest, Not a member yet? Register   Sign In
array2dropdown - for all those data needs for select boxes!
#1

[eluser]oddman[/eluser]
Hey all!

If anyone could direct me to a solution for this native to CI, that would be fantastic. As I was unable to find one, I wrote this short method which I threw into a file which extended the form helper. See below:

Code:
/*
* array2select
* Formats a given array for use by select boxes in CI. Example below:
*  - form_dropdown('name', array2dropdown($array_of_values_from_db, 'id', 'name', '- Select -'))
*  - Would return something like:
*    <select name="name">
*      <option value="">- Select -</option>
*      <option value="id1">Name 1</option>
*      <option value="id2">Name 2</option>
*    </select>
*
* @param array $array
* @param string $value_field
* @param string $test_field
* @param string $first Not required
*
* @return array
* @author Kirk Bushell
* @url http://www.kirkbushell.com
*/

function array2dropdown($array, $value_field, $text_field, $first = null)
{
     if (!is_array($array)) {
         throw new Exception('Array expected for 1st argument, '.gettype($array).' received.');
     }

     $return = array();
     if (!is_null($first)) {
         $return[] = $first;
     }

     foreach ($array as $row) {
         if (is_object($row)) {
             $return[$row->$value_field] = $row->$text_field;
         } else {
             $return[$row[$value_field]] = $row[$text_field];
         }
     }

     return $return;
}

Feel free to use and abuse Smile

It will support arrays of data being sent, but will also support arrays of objects (like what DataMapper would provide).
#2

[eluser]Phil Sturgeon[/eluser]
Much better than my similar function. Thanks very much.
#3

[eluser]IamPrototype[/eluser]
Excellent, thanks!
#4

[eluser]Dam1an[/eluser]
Very nice...
Phil, can we see yours for comparison? Or is this one really that much better you don't want to show it Tongue
#5

[eluser]Colin Williams[/eluser]
Would it make sense to overload the form_dropdown() Helper function and provide this functionality? Or am I missing something?
#6

[eluser]IamPrototype[/eluser]
I guess this extension to the form helper was created IF you have an array and need to display the data in a dropdown. If your dropdown does not need to contain data from an array, you would use the default form dropdown (...or that is what I would do).
#7

[eluser]Colin Williams[/eluser]
Quote:form_dropdown()

Lets you create a standard drop-down field. The first parameter will contain the name of the field, the second parameter will contain an associative array of options..

I still must be missing something. The form_dropdown function is dependent on arrays.
#8

[eluser]IamPrototype[/eluser]
Oh yeah.. Now I see.. I must be missing something too!
#9

[eluser]tdktank59[/eluser]
I did something similar but I just got it working for what I needed... Meaning I didn't make it versitial...
As you can see I left some stuff out lol...

Code:
/**
* Preps an object and converts it to the proper array
* to use in the CI's dropdown form helper.
*
* @param object $object the object you want to use
* @param varchar $key the value you want the dropdown to have
* @param array $value the fields in the $object
* @param boolean (TRUE/FALSE) $start_values Set to true to add
*          blank field for no value
* @return options array in key=>value(s) format
*/
function object_prep_dropdown($object,$key,$value=array(),$start_values=TRUE)
{
    $options[-1] = 'Select a Value';
    $options[0] = '';
    // Loop through all of the object for the dropdown
    foreach ($object as $it)
    {
        // Count how many values are in $value
        // So we can setup the right spacing below
        $count = count($value);

        // Initialize the options array
        $options[$it->$key] = '';

        // Now look through the values we want
        foreach ($value as $v)
        {
            // If the value is not the last value
            // in the array add a space to the end
            if ($count > 1) $string = $it->$v.' ';
            else $string = $it->$v;

            $options[$it->$key] .= $string;
            $count--;
        }
    }

    return $options;
}
#10

[eluser]theshiftexchange[/eluser]
I'm confused; are you just trying to show the results of your database in a dropdown menu?

this is my view file:

Code:
&lt;?php echo form_open($this->uri->uri_string()); ?&gt;
&lt;?php echo form_label('Default station?', $company_location_list); ?&gt;<
&lt;?php echo form_dropdown("company_location_list", $company_location_list, $companyLocationID, $this->input->post('company_location_list')); ?&gt;
&lt;?php echo form_submit('submit_complete', 'Save'); ?&gt;
&lt;?php echo form_close(); ?&gt;

$company_location_list is an array from my database passed to the view. $companyLocationID is a 'default' id I want to display if needed (such as the user has already selected a location in the past).




Theme © iAndrew 2016 - Forum software by © MyBB