Welcome Guest, Not a member yet? Register   Sign In
form_dropdown and selected
#1

[eluser]nealumney[/eluser]
I have an array of items I want to include as a 'drop down' and so I have (within my view) the following code:

echo form_dropdown('manufacturer', $mftrs);

all is well and presents a list of manufacturers for the user to select from. However, I need the first item to be a 'Please select ... ' item, and have this as an entry in the $mftrs' array. What I want to do is to have this 'selected' rather than just appear in the list somewhere. I could of course manually do the following:
<select name="manufacturer">
<option value="">Please select ...</option>
&lt;?php
foreach ($mftr $key as $row)
{ ?&gt;
<option value="&lt;?php echo $key ;?&gt;" >&lt;?php echo $row ; ?&gt;</option>
&lt;?php } ?&gt;
</select>

Is there anyway of using the form_dropdown method with an array to choose the selected item?

Thanks,

Neal
#2

[eluser]The Wizard[/eluser]
hello, there is a workarround i frequently use, just a sec.
#3

[eluser]The Wizard[/eluser]
Code:
function Dropdown_CreditCard( $name, $user_id, $selected_id = '', $reload = FALSE )
    {

        $this->load->database('c88v2');


        $this->db->select('id, user_id, type, name, number, cvv2, date_month, date_year');

        $this->db->from('premium_creditcard');

        $this->db->where( 'user_id', $user_id ); // <--- C A U T I O N    
        $this->db->where( 'status', 'ACTIVE' );    


        $query = $this->db->get();


        $num_rows = $query->num_rows();

        $result_array = array();

        if ( $num_rows <= 0)
        {
            $result_array['-1'] = ' --------- ';
        }
        else
        {

            $result_array['-1'] = ' - Select - ';

            foreach ($query->result_array() as $row)
            {
                $result_array[ $row['id'] ] = $row['number'] .' - '. $row['type'] .' - '. $row['name'];
            }

        }

        $javascript = $reload == TRUE ? 'onchange="this.form.submit();"' : '';

        $this->load->helper('form');

        return form_dropdown( $name, $result_array, $selected_id, $javascript );

    }

thats the code and
Code:
// in validation
..
$creditcard_id = $this->input->post('creditcard_id');  
$data['creditcard_dropdown']     = $this->model_premium->Dropdown_CreditCard( 'creditcard_id', $user_id, $creditcard_id, TRUE );
..

that would be the use of it.
i hope it helps
#4

[eluser]slowgary[/eluser]
This is another reason why I don't really get using helpers to generate markup. The markup is so simple to begin with, does it really simplify anything? What's the benefit?

If you MUST do it this way, a crappy hack could be to just use JavaScript to insert the first element...
Code:
&lt; script type='text/javascript' &gt;
$('select').prepend("<option selected='selected' disabled='disabled'>Please select...</option>");
< /script>

This example uses jQuery
#5

[eluser]Asinox[/eluser]
Well my way:
Code:
function listCombox(){ //COMBOX
        
        $query = $this->db->get('table');
        if($query->num_rows()>0){
            $data[] = 'Select ONE';
            foreach($query->result_array() as $row){
                $data[$row['id']] = $row['name'];
            }
            return $data;
        }else{
            return FALSE;
        }
        $query->free_result();
    }
maybe is not perfect but working for me




Theme © iAndrew 2016 - Forum software by © MyBB