Welcome Guest, Not a member yet? Register   Sign In
Dropdownlist with selected option
#1

[eluser]yorvik[/eluser]
I have a little question. How do you guys make a dropwdown list with records from a database. What I want is a front-name and last-name from a different table. The last-name and front-name are selected on field artitsID in my current table.

So I have the tables:
artSadartID, artistID->foreign key, categorieID->foreign key, price, infoNL, infoEN)
artist: (artistID, front-name, last-name)
category (categorieID, name)

I want all the names of the artist table in my drop down, and the selected option should be the one that equals my artistID in my art Table. the artistID is shown in my
Code:
$this->uri->segment(3)

Code:
"Select front-name, last-name From artist WHERE artistID = '$this->uri->segment(3)'"

I hope you guys now what i mean, but how could I code this best practice, so I can reuse this for other drop down list.

Thanks in advance!
#2

[eluser]Nick_MyShuitings[/eluser]
Code:
SELECT front-name, last-name FROM artist

First part is to only do that, or else in your data set you'll only have the records that match the artist id.

then foreach your record set building the select (preferably in the view, I leave the work of remember how to pass data from your controller to your view up to you).

Code:
<select>
  &lt;? foreach($artists as $artist): ?&gt;
    <option value="&lt;?= $artist->artistID;?&gt;" &lt;?= ($artist->artistID == $selectedArtist)?'selected="selected"':'';?&gt; >&lt;?=$artist->front-name?&gt; &lt;?=$artist->last-name?&gt;</option>
  &lt;? endforeach; ?&gt;
</select>
#3

[eluser]Nick_MyShuitings[/eluser]
If you find you are reusing this a lot, you can use the form helper if that is also your cup of tea:

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

foreach ($artists as $artist) {
  $options[$artist->artistID] = $artist->front-name.' '.$artist->last-name;
}

echo form_dropdown('artists', $options, $this->uri->segment(3));
#4

[eluser]smilie[/eluser]
Hi,

I had to brake my head around this one as well, but got it finally (tho' not sure this is the best solution - but it works for me).

Note: I am only showing you parts of code which are relevant, there is more to it. But here bellow I do:
- get locations from model;
- present found locations in the drop down in view (keeping location_id as dropdown ID (!!) which is important to know which location ID was chosen ;-)

Good, my controller:

Code:
# Get all locations
$this->load->model('location_model');
$data['result']['locations'] = $this->location_model->getLocations();
Note: You could pass extra param's to your model (like ID or something);

My model
Code:
function getLocations()
{
    # Get all locations
    $this->db->select('location_id,location_name');
    $this->db->from('location');
    $this->db->order_by('location_name','ASC');
    $query = $this->db->get();
    $res = $query->result_array();
    
    if($query->num_rows() > 0)
    {
        $result = $res;
    }
    else
    {
        # No results :(
        $result = FALSE;
    }
    # return data
    return $result;
}
Note: if you would pass extra info to model, then you could use it to add WHERE in the query;

My view
Code:
&lt;?php
    $locations = array();
    foreach($result['locations'] as $key=>$val)
    {
        $tmp = array($val['location_id']=>$val['location_name']);
        $locations = $locations + $tmp;
    }
    $extra = "style='width:90%;'";
    echo form_dropdown('stock_location_id', $locations, $result['stock_location_id'], $extra);
?&gt;
Note: Oke, this was tricky part. What I did in here is re-create new array based on the results from the model so it 'fits' the needs of form_dropdown. This way, form_dropdown creates this:

Code:
<select style="width: 90%;" name="stock_location_id">
<option value="5">Location 1</option>
<option value="6">Location 2</option>
<option value="2">Location 3</option>
<option value="4">Location 4</option>
<option selected="selected" value="1">Location 5</option>
<option value="7">Location 6</option>
</select>

Do bare in mind, that I also post $result['stock_location_id'] from the controller which holds the current chosen location in DB (so that this one is "selected" by default on page load).

Hope this helps :-)

Cheers,
Smilie
#5

[eluser]smilie[/eluser]
Blast Nick was faster _and_ with more 'elegant' solution :-)

* El Forum handles beer to Nick :-)

Cheers,
Smilie
#6

[eluser]Nick_MyShuitings[/eluser]
HAHAHA... wooot... if you ever get down to Argentina I'll take you up on that... or if I ever get my wifes Visa finished to get the hell out of here I'll have to go visit.
#7

[eluser]smilie[/eluser]
@Nick: DEAL!

And now back on topic Smile

Cheers,
Smilie
#8

[eluser]yorvik[/eluser]
[quote author="Nick_MyShuitings" date="1294438533"]If you find you are reusing this a lot, you can use the form helper if that is also your cup of tea:

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

foreach ($artists as $artist) {
  $options[$artist->artistID] = $artist->front-name.' '.$artist->last-name;
}

echo form_dropdown('artists', $options, $this->uri->segment(3));
[/quote]

Thanks Nick this was very helpfull!
#9

[eluser]Nick_MyShuitings[/eluser]
NP... I'm enjoying being more active in the forum a lot more then when I was just lurking. If nothing else it keeps me from going bored-crazy while at the office... and its more productive then reddit. Smile




Theme © iAndrew 2016 - Forum software by © MyBB