ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

How To Create CodeIgniter form_dropdown

October 23, 2010 9:04am

Subscribe [5]
  • #1 / Oct 23, 2010 9:04am

    Ahmed Iqbal

    19 posts

    Hello Friends,

    I’m using Dropdown old code for categorie’s list.
    Look like

    <select name="category" id="category">
    <?php foreach ($categories as $cat): ?>
    <option label="<?=$cat->cat_name; ?>" value="<?=$cat->cat_id; ?>">
    <?=$cat->cat_name; ?>
    </option>
    <?php endforeach; ?>
    </select>

    But i want to be use codeigniter ‘form_dropdown function’.
    By the way, I’ve already selected form helper in me config/autoload.php.
    But i need dropdown code structure for codeingiter…!

    Thanks 😊

  • #2 / Oct 23, 2010 9:26am

    pickupman

    1317 posts

    Try

    echo form_dropdown('category', $categories, FALSE, 'id="category");

    Note: The label attribute is only supported by IE 7+.

  • #3 / Oct 23, 2010 9:46am

    Ahmed Iqbal

    19 posts

    Thanks, but how i can get foreach ‘$cat’ result in this ‘form_dropdown’ function?

  • #4 / Oct 23, 2010 9:51am

    pickupman

    1317 posts

    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");
  • #5 / Oct 23, 2010 12:06pm

    Ahmed Iqbal

    19 posts

    if i dot this

    <?php foreach ($categories as $cat): ?>
    <?=form_dropdown('category', $cat, set_value('category'), 'id="category");
    <?php endforeach; ?>

    it’s generate <select> multi time with <option>. so how i fix :(
    I’m confused with this.

  • #6 / Oct 23, 2010 12:12pm

    pickupman

    1317 posts

    Thats not the code I posted. Double check my syntax on the foreach loop.

  • #7 / Oct 23, 2010 12:23pm

    Ahmed Iqbal

    19 posts

    Controller Code:

    $data[‘cat’] = $this->database->get_cat(); // Try To Catch Categories Result From Model
    foreach($categories as $row){
    $cat[$row->cat_id] = $row->cat_name;
    }
    $this->load->view(‘share_view’, $data);

    View Code:

    <?=form_dropdown(‘category’, $cat, set_value(‘category’), ‘id=“category”’); ?>

    Error in Result:

    A PHP Error was encountered
    Severity: 4096
    Message: Object of class stdClass could not be converted to string
    Filename: helpers/form_helper.php
    Line Number: 331
  • #8 / Oct 23, 2010 12:44pm

    pickupman

    1317 posts

    You need to return the result object back to $categories from your model.

  • #9 / Oct 23, 2010 1:07pm

    Ahmed Iqbal

    19 posts

    ohh, sorry…! I’ve understand your solution.
    your code work for me. 😊

    thanks man.

    Fried, please let me know right solution to access custom build function in controller.
    we need to ‘require_once’ custom functions file in controller?
    or maybe we access my custom function through model?

    like ‘$this->load->model(‘database’);’
    and ‘$this->database->custom_function(argument) ??? this is right solution???

  • #10 / Oct 23, 2010 1:14pm

    pickupman

    1317 posts

    Yes that will be fine.  Remember codeigniter is still php so you can still do the same stuff. If are going to include a custom class check out the user guide for custom libraries. That you can use CI syntax. If it is a set of function you can put into your own helper. Whatever you feel comfortable with is fine. That’s one of the things people like about CI is that it doesn’t force you into anything.

  • #11 / Oct 23, 2010 1:16pm

    Ahmed Iqbal

    19 posts

    Thanks dear…! Great job/

  • #12 / Dec 16, 2010 8:40am

    k2zs

    114 posts

    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->select('pgTypeID,typeName');
            $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.

  • #13 / Dec 17, 2010 7:40am

    bonatoc

    19 posts

    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 :
    http://datamapper.exitecms.org/

    http://ellislab.com/forums/viewthread/149388/

    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…).

  • #14 / Dec 17, 2010 7:43am

    bonatoc

    19 posts

    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 ?

  • #15 / Dec 17, 2010 10:17pm

    k2zs

    114 posts

    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.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases