How to set a default value Dropdown approcach? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: How to set a default value Dropdown approcach? (/showthread.php?tid=50282) Pages:
1
2
|
How to set a default value Dropdown approcach? - El Forum - 03-21-2012 [eluser]noname525[/eluser] vv How to set a default value Dropdown approcach? - El Forum - 03-21-2012 [eluser]PhilTem[/eluser] Easiest method: Code: // Prepend an item to the array Another method: Code: // Create the country-array How to set a default value Dropdown approcach? - El Forum - 03-21-2012 [eluser]noname525[/eluser] vv How to set a default value Dropdown approcach? - El Forum - 03-21-2012 [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 Actually, it's faster to do like what PhilTem said, since it's 1 less loop. How to set a default value Dropdown approcach? - El Forum - 03-21-2012 [eluser]PhilTem[/eluser] [quote author="hunter" date="1332332412"]Thanx ........... But i got a simplest one ....... Code: <? $country[0] = 'Select Country'; ksort($country);?> 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. How to set a default value Dropdown approcach? - El Forum - 03-21-2012 [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() How to set a default value Dropdown approcach? - El Forum - 03-21-2012 [eluser]noname525[/eluser] vv How to set a default value Dropdown approcach? - El Forum - 03-21-2012 [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. How to set a default value Dropdown approcach? - El Forum - 03-21-2012 [eluser]noname525[/eluser] vv How to set a default value Dropdown approcach? - El Forum - 03-21-2012 [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. |