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

[eluser]Unknown[/eluser]
Hey, new with CI and I love it. However, I'm at the point in my site where I need a dynamic pulldown menu based on MYSQL results. I have a working script but I'd like for it to be done with form_dropdown()

What I need: Someway to get form_dropdown to read a MySQL table and display options based on the rows of that.

What I have:
Code:
$sql = "SELECT name, id from services";
$res = mysql_query($sql);

echo '<select name="service">';
echo '<option value="">--Select--</option>';
while($opt = mysql_fetch_array($res))
{
echo '<option value="'.$opt['id'].'">'.$opt['name'].'</option>';
}
echo '</select>';
echo "</select>&lt;/form&gt;";
That works fine but it WONT work with WITH my form_open() and my

$data = array(
'name' => 'added_reqs',
'id' => 'added_reqs',
'maxlength' => '100',
'style' => 'width:50%',
);
echo form_input($data);

style text_inputs.
Any help would be appreciated! Thanks.
#2

[eluser]=G-Man=[/eluser]
I'm pretty new my self, but i was looking at the for_dropdown() and it wants an array, the problem is the query, even with query->result_array(), will only return one line in the DB at a time, so you would have to build code to go through the results and make another array of ALL the data, so might be more work then needed.

what I can do is show you my code (for a almost the exact thing), and you can mabey tweak it.

Controller
Code:
&lt;?
class Header extends Controller
{
    function index()
    {
        $data['query'] = $this->db->get("admin_servers");
        $this->load->view('header_view', $data);
    }
}
?&gt;

View
Code:
&lt;form action="&lt;?= site_url("/server/changeserver"); ?&gt;" method="post" name="changeserver"&gt;
        Server:
        &lt;select name="server" onChange="TestServer()"&gt;
          <option value="0">Select Server</option>
          &lt;? foreach($query->result() as $server): ?&gt;
          <option value="&lt;?=$server->id;?&gt;" &lt;? if($this->session->userdata('server') == $server->id) { echo ("selected=\"selected\"");} ?&gt;>
            &lt;?=$server->name;?&gt;
          </option>
          &lt;? endforeach; ?&gt;
        </select>
      &lt;/form&gt;

hope i helps, or there might be a built in function to get the entire result in an array in one command (would be nice Wink)
#3

[eluser]Armchair Samurai[/eluser]
In brief, I'd approach it this:
Code:
/**
| Model
*/
$this->db->select('name, id');
$query = $this->db->get('services');
return $query->result();

/**
| Controller
*/
$result = $this->Your_model->your_function();
$arr[] = '';
foreach ($result as $val)
    $arr[$val->id] = $val->name;

$this->load->view('foo', array($arr));

&lt;!--View--&gt;
&lt;?=form_dropdown('service', $arr);?&gt;
#4

[eluser]Unknown[/eluser]
[quote author="Armchair Samurai" date="1213272443"]In brief, I'd approach it this:
Code:
/**
| Model
*/
$this->db->select('name, id');
$query = $this->db->get('services');
return $query->result();

/**
| Controller
*/
$result = $this->Your_model->your_function();
$arr[] = '';
foreach ($result as $val)
    $arr[$val->id] = $val->name;

$this->load->view('foo', array($arr));

&lt;!--View--&gt;
&lt;?=form_dropdown('service', $arr);?&gt;
[/quote]

Thanks with some modification that got me going however I have another question.
I have two of these selection boxes and they are multiple select. How can I have the second box change which range of id's it'll select from BASED on the first boxes selection?
#5

[eluser]Armchair Samurai[/eluser]
From what you're describing, that sounds like a job for AJAX - when the use selects an option from the first dropdown, send an AJAX request to the server to retrieve the data and then populate the second dropdown. If you're familiar with JS libraries like jQuery, this should be pretty easy to set up.




Theme © iAndrew 2016 - Forum software by © MyBB