How To Create CodeIgniter form_dropdown

[eluser]Ahmed Iqbal[/eluser]
Thanks dear...! Great job/

[quote author="pickupman" date="1287856311"]If you would like to use the form dropdown helper, you need to pass a associative array rather than object. You something in your controller like:
$data['cat'] = array();
foreach($categories as $row){
$cat[$row->cat_id] = $row->cat_name;

$this->load->view('your_view', $data);

//Now in your view
echo form_dropdown('category', $cat, set_value('category'), 'id="category");

I can't seem to get the drop down to select the active ID in my form. I've tried passing both the value and the text as the selectes value but it won't work. Here's where I'm at right now (also I'm using Colin's CRUD example for my form and running the form as a module):

In my model:
function getTypeList()
        $this->db->order_by('typeOrder', 'asc');
        $query = $this->db->get('pageTypes');
        if ($query->num_rows() > 0)
            //create this array for view loop
            $typelist = $query->result();

In my controller:
$getTypesList = $this->form_model->getTypeList();
foreach($getTypesList as $row){
    $typelist[$row->pgTypeID] = $row->typeName;
$form['typelist'] = $typelist;

In my view:
<?php echo form_dropdown('pgTypeID', $typelist, set_value($fields['pgTypeID'])); ?>

The dropdown populates correctly and I have echo'ed my variables in various places and they exist, $fields['pgTypeID'] follows the current active ID

what am I doing wrong?

ps: I even tried setting the selected value manually using set_value('2') and it still won't work.

You should take a look at this if you're managing a relationship model (categories->items->variants...).

This is IMHO the best way to go :

DMZ (DataMapper ORM) is a fantastic add-on to CodeIgniter.
It takes just a little while to grasp on the concept, and it has a fabulous included extensions which is called htmlform.

The only drawback is that you have to respect a precise naming policy for your MySQL tables, but it's just matter of renaming fields and creating some tables (obviously you'll try it on a copy of your existing database...).

As for your problem, make sure you debug first in your model, then in your controller, then your view, using echo or print_r on every data you retrieve/manipulate.

Are you're sure the controller is passing the needed data to your view ?

Yes.... I echoed and print_r'd everything and it exists where it's supposed to I'm just not sure I wrote the drop_down correctly as I have never used the CI form helper, I've always done old-school form elements. I have echoed the uniqueID right at the drop_down element but it doesn't select the drop_down element correctly.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.