Welcome Guest, Not a member yet? Register   Sign In
dropdown data
#1

[eluser]r@mtbb_[/eluser]
Hi,

Can anyone please give an exemple of how to populate a form_dropdown() with the contents of a mysql table? I cannot manage to do this..

Thanks!
#2

[eluser]tonanbarbarian[/eluser]
yeah i admit that i would prefer if the structure of the options in form_dropdown worked a bit differently

but to get it to work try something like this
Code:
$options = array();
$this->db->select('xxx AS value, yyy AS text');
$this->db->from('zzz');
....
// or use
// $query = $this->db->query('SELECT xxx AS value, yyy AS text FROM zzz');
$query = $this->db->get();

if ($query->num_rows()) {
  foreach ($query->result_array() as $row) {
    $options[$row['value']] = $row['text'];
  }
}
$query->free_result();
unset($query);

The query will return results like
Code:
array(
  array(
    'value'=>1,
    'text'=>Hello
  ),
  array(
    'value'=>2,
    'text'=>Something
  ),
  array(
    'value'=>6,
    'text'=>Different
  ),
  array(
    'value'=>96,
    'text'=>What
  )
);

and then the processing of the results will return something like
Code:
array(
  1=>Hello,
  2=>Something,
  6=>Different,
  96=>What
);
This resulting array is what form_dropdown needs, and if you are hard coding the values this is the easiest to create
However I prefer the first format for the options because it is much easier to retrieve from a query and does not require any processing before being passed to the function.

p.s.
while
Code:
$query->free_result();
unset($query);
is not strictly necessary, it is a good idea to clean up after yourself
#3

[eluser]r@mtbb_[/eluser]
Thanks a lot! I'll try it right away
#4

[eluser]Bharani[/eluser]
hi dear i have used your code..but in form combo not dislayed can you send the view form code for this


Code:
<div class="formArea">
        <div class="errors">&lt;?php echo $this->validation->error_string; ?&gt;</div>
<h2 align="center"> Image Add</h2>
         &lt;?php echo form_open_multipart('image_gallery_controller/index'); ?&gt;
<table align="center"><tr><td>        
        <p class="formBold"><label for="imagename">Imagename</label></p></td>
    <td>&lt;?php echo form_input($imagename); ?&gt;</td></tr>
  <tr><td><p class="formBold"><label for="userfile">Upload Your Photo</label></p></td>
       <td>&lt;?= form_upload($userfile); ?&gt;</td></tr>  
    <tr><td><p class="formBold"><label for="keyword">keyword</label></p></td>
        <td>&lt;?php echo form_input($keyword); ?&gt;</td></tr>
              
              &lt;? echo form_dropdown($options);?&gt;
        
        
<tr><td colspan="2" align="center">&lt;?php echo form_submit('submit', 'Submit'); ?&gt;</td></tr>
</table>
  &lt;?php echo form_close(); ?&gt;
#5

[eluser]mooger[/eluser]
[quote author="Bharani" date="1203167140"]hi dear i have used your code..but in form combo not dislayed can you send the view form code for this
Code:
...
&lt;? echo form_dropdown($options);?&gt;
...

[/quote]

Change it to

Code:
&lt;? echo form_dropdown('field_name', $options);?&gt;
#6

[eluser]Bharani[/eluser]
got result

thanks




Theme © iAndrew 2016 - Forum software by © MyBB