Welcome Guest, Not a member yet? Register   Sign In
Fill a drop down box with table data
#1

[eluser]max123[/eluser]
can any one explain me the steps to fill a drop down box with data from a table, from the very beginning, step by step

Thanx
#2

[eluser]stuffradio[/eluser]
Step 1) Decide what content you want to fill

Step 2) Write the model that connects to the table you want and queries it for the rows you want

Step 3) Use the controller to fetch the results and store it in an array, pass the array to the view

Step 4) Setup a drop down box and loop through the array adding it to the option box for each result
#3

[eluser]max123[/eluser]
I'm ok untill step3, but how can I add items to drop down box according to the CI frame work. I know the normal way. I need to know how to do it using frame work
#4

[eluser]iron-man[/eluser]
1. First collect data from database within a model
2. Send value to a controller
3.Now send the array value to a view file
4. Becareful array should be 2 dimentional.




thanks
Rony
#5

[eluser]developer10[/eluser]
[quote author="max123" date="1261575441"]I'm ok untill step3, but how can I add items to drop down box according to the CI frame work. I know the normal way. I need to know how to do it using frame work[/quote]

i had the same problem recently and some very kind users of this forum helped me.

let's start. model:

Code:
function getdropdown_djelatnosti_array($key, $value, $from)
    {
        $result = array();
        $array_keys_values = $this->db->query(' SELECT '.$key.', '.$value.' FROM '.$from.' ORDER BY djelatnost ASC ');
            $result['Sve'] = '-- Odaberi djelatnost --';  // i added this line because i need some default value (in case user doesnt select anything from this box). it of course displays on the top of all rows in the drop-down
            foreach ($array_keys_values->result() as $row)
            {
                $result[$row->$key] = $row->$value;
            }
            
        return $result;
    }

my view in which the drop-down box is displayed:

Code:
echo form_open('prikazi/pretraga', $frmSrc_atts);?>
    
    <div class='stavka'>
        <label>Djelatnost</label>
        &lt;?php echo form_dropdown('djelatnost', $dropdown_djelatnosti, 'Sve'); ?&gt;  // Note the 3rd parameter here ('Sve'). thats the default value for the drop-down, if nothing is selected from the box before submitting the form.
    </div>

hope this helps!
#6

[eluser]max123[/eluser]
Thanx a lot. It works




Theme © iAndrew 2016 - Forum software by © MyBB