Welcome Guest, Not a member yet? Register   Sign In
from result_array() to form_dropdown()
#1

[eluser]0plus1[/eluser]
I'm doing a SELECT with active record. From only two fields, the resulting array is:

Code:
Array ( [0] => Array ( [DEC_COD] => 1 [DEC_DESCRIZIONE] => S.p.a. ) [1] => Array ( [DEC_COD] => 2 [DEC_DESCRIZIONE] => S.r.l. ) [2] => Array ( [DEC_COD] => 3 [DEC_DESCRIZIONE] => S.a.s. ) [b]........[/b]

While for form_dropdown I need something like this:
Code:
array('1'  => 'S.p.a.', '2'    => 'S.r.l.', '3'   => 'S.a.s.', [b].........[/b]

Is there any shortcut to this or do I need to make a foreach?
#2

[eluser]Cro_Crx[/eluser]
In your controller you can create a new array from the old one... would be something like this


Code:
foreach ($results as $result)
{
    $data[$result->DEC_COD] = $result->DEC_DESCRIZIONE;
}

//then just pass $datainto your view
$this->load->view('view', $data);
#3

[eluser]0plus1[/eluser]
Ok so I need to do it manually.

Speaking of best practice, since the function in the model that produces that query will only be used to form_dropdown() is it more correct to place the array modification code in the controller or in the model?
#4

[eluser]Cro_Crx[/eluser]
I would say put it in the controller, as your modifying the data that you've received from your database and leave the model strictly for database manipulation code. Although it's sort of a grey area i suppose, so whatever you feel is neater and easier to maintain.
#5

[eluser]0plus1[/eluser]
Thank you!
#6

[eluser]B A B U[/eluser]
If u want dynamic function try this
Code:
/*-----------------------------------------------------------------------------
Converting the two columns query result into single dimentional array
@ $options - options array that having 2 columns & first column should be option value
------------------------------------------------------------------------------*/        

        function mda_to_options_sda($options_mda)
        {
            if (is_array($options_mda))
            {
                $options_sda = $keys = array();
                foreach ($options_mda AS $row)
                {
                    if (count($row)!==2)
                    {
                        show_error("options array having more than 2 or less columns");
                    }
            
                    // Getting column names into array
                    foreach ($row AS $key => $value)
                    {
                        $keys[] = $key;
                    }
                    
                    // Creating SDA using columns array
                    for($i=0 ; $i < count($keys) ; $i++)
                    {
                        $options_sda[$row[$keys[0]]] = $row[$keys[1]];
                    }
                }
                return $options_sda;
            }
            show_error("Passed wrong array options parameter");
        }
#7

[eluser]Phil Sturgeon[/eluser]
Why would you pass in an array? You are over complicating this a great deal.

Code:
function array_to_select($results, $value = 'id', $key = 'title')
{
    // Converts objects to arrays
    if(is_object($results)) $results = get_object_vars($results);

    $options = array();

    // Will only run if results is an array, not a string, int, etc.
    if(!is_array($results))
    {
        foreach($results as $result)
        {
            // Get the two rows specified
            $options[$result[$value]] = $result[$key];
        }
    }

    return $options;
}

You can see if I dont provide either of the last two arguments it defaults to id and title. These are just standard field names so you may wish to change that or remove the default value if you plan to always set them in the function call.
#8

[eluser]B A B U[/eluser]
Your code wont work for my situation. I want to display all company names in dropdown list with cpmpany_id as value. Then only after submitting the form i can store the comapny_id in other table as foreign key.
MySQL query is
Code:
select company_id, company_name from companies

I want a drop down list like this
Code:
<select>
<option value="company_id">company_name</option>

How can u do this one
#9

[eluser]helmutbjorg[/eluser]
His code is great...
Code:
$options = array_to_select($results, 'company_id', 'company_name');

But perhaps would change the line:
Code:
if(!is_array($results))

to

Code:
if(is_array($results))
#10

[eluser]B A B U[/eluser]
[quote author="helmutbjorg" date="1249719767"]His code is great...
Code:
$options = array_to_select($results, 'company_id', 'company_name');

But perhaps would change the line:
Code:
if(!is_array($results))

to

Code:
if(is_array($results))
[/quote]

I dont want to specify column names. What i want is if i send a two column results to a function without passing any column names then it should give options array




Theme © iAndrew 2016 - Forum software by © MyBB