Welcome Guest, Not a member yet? Register   Sign In
Default value from database populated dropdown
#1

[eluser]RaGe10940[/eluser]
I am able to set up a drop down menu and all from the values in my database, but I am having a little quirk and it is really not major but it is a end user experience boost. Currently the default value for the dropdown is : Rixhers Ajazi

I however want to set it up so that the default is like so : '' => 'Choose Counselor'

how do I set that up with dynamically loaded counselor names from a DB?

I have tried to make a new options variable like so :
Code:
$options3 = array(''=>'Choose Counselor');

<?php echo form_dropdown('counselor', $options, $options3); ?>

and that returns nothing. Again the functionality is their I would rather just have it come with a default value.

This is my entire current code :

Code:
<div id="appointment">
  <fieldset><legend>Only For<font color="#ff0000"><strong> Appointments</strong></font></legend>
      &lt;?php $options1 = array('' => 'Hour', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5',
   '6' => '6', '7' => '7', '8' => '8', '9' => '9', '10' => '10', '11' => '11', '12' => '12')
      ?&gt;
      &lt;?php $options2 = array('' => 'Minute', '00' => '00', '15' => '15', '30' => '30', '45' => '45'); ?&gt;
      &lt;?php $options3 = array('' => ''); ?&gt;
      &lt;?php
      foreach ($staff as $s) {
   $options = array($s['username'] => $s['fname'].' '.$s['lname']);
      }
      ?&gt;
       &lt;?php echo form_dropdown('counselor', $options); ?&gt;
&lt;?php echo form_dropdown('hour', $options1); ?&gt;:&lt;?php echo form_dropdown('minute', $options2); ?&gt;
  </fieldset>
     </div>

Any help would be awesome.
#2

[eluser]TheFuzzy0ne[/eluser]
Code:
&lt;?php echo form_dropdown('counselor', array('' => 'Choose Counselor') + $options); ?&gt;

Note that array_merge() will reindex the resulting array numerically, which is probably not what you want, so we use the '+' operator instead.
#3

[eluser]TheFuzzy0ne[/eluser]
I also humbly suggest you consider changing $options, $options2 and $options3 to more meaningful variable names.

For example, $counselor_dropdown, $hours_dropdown, $minutes_dropdown. They could probably be a little more descriptive than that, but it should be easier to see what's what if you, or someone else, comes back to this code 12 months from now. Smile
#4

[eluser]RaGe10940[/eluser]
*face palm*

I didn't even know you could do that. Thanks a million Fuzzy Smile




Theme © iAndrew 2016 - Forum software by © MyBB