Welcome Guest, Not a member yet? Register   Sign In
form_dropdown - how to populate it
#1

[eluser]developer10[/eluser]
Since i want to use CI's alternative PHP syntax all the way long,
i'm faced with this issue while trying to rebuild my search form

this is what i found in CI's manual on this:

Code:
$options = array(
                  'small'  => 'Small Shirt',
                  'med'    => 'Medium Shirt',
                  'large'   => 'Large Shirt',
                  'xlarge' => 'Extra Large Shirt',
                );

$shirts_on_sale = array('small', 'large');

echo form_dropdown('shirts', $options, 'large');

// Would produce:

<select name="shirts">
<option value="small">Small Shirt</option>
<option value="med">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>
</select>

echo form_dropdown('shirts', $options, $shirts_on_sale);

// Would produce:

<select name="shirts" multiple="multiple">
<option value="small" selected="selected">Small Shirt</option>
<option value="med">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>
</select>

since $option should come from database, i dont know how to turn the array of results
from my existing model (the same model is used for pulling categories for left column),
which retrieves the very same rows (categories) i want to use in form_dropdown()

so, here's the end of my model function:

Code:
if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return FALSE;
        }

so how can i throw that result into $option variable needed by form_dropdown()?
i tried using foreach loop to generate array elements (some of you are probably LOL
right now) but hey, i'm pretty new to CI.

thans for any suggestions
#2

[eluser]Colin Williams[/eluser]
Code:
$data['options'] = array();
foreach ($result as $row)
{
   $data['options'][$row->key] = $row->value;
}
// Now if you send $data to your view, the $options var will be available

In fact, it makes more sense to have the model do this formatting before getting back to the controller.
#3

[eluser]developer10[/eluser]
[quote author="Colin Williams" date="1247603996"]
Code:
$data['options'] = array();
foreach ($result as $row)
{
   $data['options'][$row->key] = $row->value;
}
// Now if you send $data to your view, the $options var will be available

In fact, it makes more sense to have the model do this formatting before getting back to the controller.[/quote]

thanks colin, will certainly try this
#4

[eluser]developer10[/eluser]
[quote author="Colin Williams" date="1247603996"]
Code:
$data['options'] = array();
foreach ($result as $row)
{
   $data['options'][$row->key] = $row->value;
}
// Now if you send $data to your view, the $options var will be available

In fact, it makes more sense to have the model do this formatting before getting back to the controller.[/quote]

i cant make this work

here's my model so you can see what it returns:

Code:
function kategorije() {
        
        $query = $this->db->query(' SELECT dje.djelatnost, count(firme.d_id) AS brFirmi
                                    FROM tbl_djelatnosti AS dje
                                    LEFT JOIN _tbl_firme AS firme ON dje.d_id = firme.d_id
                                    WHERE f_vidljivo = 1
                                    GROUP BY dje.d_id
                                    ORDER BY djelatnost ASC ' );
        

        
        if ($query->num_rows() > 0) {
            $result = $query->result();
            
            return $result;
        }
        else {
            return FALSE;
        }
    }

since you suggested it would be better for your code to be in the model, where to put it exactly?
#5

[eluser]Johan André[/eluser]
I use this in my models (usually in MY_Model and then extend my models from it):

Code:
// <option value="$key">$value</option>
// $first_key is the topmost key/value pair of the dropdown (example $key = '-1', $value = 'Select a category').

function convert_to_list($data, $key = null, $value = null, $first_key = '-1', $first_value = 'Nothing selected')
    {
        if ($data)
        {
            
            if($first_key != NULL)
            {
                $keys[] = $first_key;
                $vals[] = $first_value;
            }
            
            foreach ($data as $row)
            {
                $keys[] = $row[$key];
                $vals[] = $row[$value];
            }
                
            if (!empty($keys) && !empty($vals))
            {
                $return = array_combine($keys, $vals);
                return $return;
            }            
        }
        else
        {
            return false;
        }
    }

The $data feeded to the function is a result_array() but can easily be change to use objects instead.
#6

[eluser]B A B U[/eluser]
Here is my code
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");
        }




Theme © iAndrew 2016 - Forum software by © MyBB