Welcome Guest, Not a member yet? Register   Sign In
How to set a default value Dropdown approcach?
#1

[eluser]noname525[/eluser]
vv
#2

[eluser]PhilTem[/eluser]
Easiest method:

Code:
// Prepend an item to the array
array_unshift($country, "--Select--"); // '--Select--' will get key '0' (or the first key - 1)

// Parse the dropdown
echo form_dropdown('name', $country);

Another method:

Code:
// Create the country-array
$country = array('--Select--');

// Merge it with what you get from the DB
$country = array_merge($country, $country_from_db);

// Parse it
echo form_dropdown('name', $country);
#3

[eluser]noname525[/eluser]
vv
#4

[eluser]aquary[/eluser]
Just to correct yours a little bit. form_drop_down() array doesn't start at 1. It's because you pre-format the array within the model, which make them always has key bigger than 0

Code:
// This one
foreach($query->result_array() as $row)
  {
   $data22[$row['id']]=$row['name'];
  }

Actually, it's faster to do like what PhilTem said, since it's 1 less loop.
#5

[eluser]PhilTem[/eluser]
[quote author="hunter" date="1332332412"]Thanx ...........
But i got a simplest one .......
Code:
<? $country[0] = 'Select Country'; ksort($country);?>

<div id="country"><h5>Select Country</h5>
&lt;?php echo form_dropdown('name', $country); ?&gt; </div>
Cause in case of form_dropdown arrays start with 1 not with zero........[/quote]

You should be careful with ksort since it may screw up your sorting. That's why I tend to do stuff just like I posted above. Even though it's not made in the view but actually put inside MY_Model which creates an array that has the format needed for form_dropdown.

You can soon check my MY_Model code at GitHub if you wanna know more about how I implemented it.
#6

[eluser]CroNiX[/eluser]
Just set it up before you create the rest of the entries in your loop. 1 extra line. I always use an id of 0 since autoincremented db fields start at 1 so it will never interfere with the real data, and then use 'is_natural_no_zero' for form validation if it is a required field.
Code:
function selcountry()
{
  $this->db->select('id,name');
  $query=$this->db->get('country');
  $data22=array(0 => '--Select--'); //just add the default with key of 0 before you populate the array
  foreach($query->result_array() as $row)
  {
   $data22[$row['id']]=$row['name'];
  }
  return ($data22);    
}
#7

[eluser]noname525[/eluser]
vv
#8

[eluser]CroNiX[/eluser]
Where you have $country should be the numeric index of the $country value you want as the default, which is probably 0.

The value is available in $this->input->post('name') with the rest of your form elements in your controller. I hope you are using form validation. You would do it after it passes validation.
#9

[eluser]noname525[/eluser]
vv
#10

[eluser]CroNiX[/eluser]
Well, if you haven't submitted the form yet but are checking the post value, it will return boolean FALSE. If the form is submitted, then 0 must be the value of the option you had selected in the dropdown. 0 is your default.




Theme © iAndrew 2016 - Forum software by © MyBB