1

How can i get dynamic drop down list which have value and label from database table. ie array( 'label1' => 'value1', 'label2' => 'value2' )

<?php
 echo form_open('add_veg/insert_data', 'class="form-horizontal" id="myform" role="form"');
    $sales_date = array(
            'type'  => 'date',
            'name'  => 'date',
            'id'    => 'date',
            'class' => 'form-control'
    );
    $vegnames=array();
    foreach($veg_names as $r)
    {
    $vegnames[]=$r->label;
    //$r->value;
    }
    /*
    $vegnames = array(
            'cauliflower' => 'Caulifower',
            'Brinjal' => 'Brinjal'
    );
    */
    /*$veg_name = array(
            'type'  => 'text',
            'name'  => 'v_name',
            'id'    => 'v_name',
            'class' => 'form-control'
    );
    */
    $wt = array(
            'type'  => 'text',
            'name'  => 'weight',
            'id'    => 'wt',
            'class' => 'form-control'
    );
    $price = array(
            'type'  => 'text',
            'name'  => 'price',
            'id'    => 'price',
            'class' => 'form-control'
    );
    $save=array(
            'name' =>'sub',
            'value' =>'Save',
            'class' =>'btn btn-success btn-sm'
    );

    echo form_label('Sales date', 'sdate', 'class="control-label col-sm-2"');
    echo form_input($sales_date);
    echo form_label('Vegitable Name', 'vname', 'class="control-label col-sm-2"');
    //echo form_input($veg_name);
    echo form_dropdown('v_name', $vegnames,'','class="form-control"');
    echo form_label('Weight', 'wt', 'class="control-label col-sm-2"');
    echo form_input($wt);
    echo form_label('Price', 'price', 'class="control-label col-sm-2"');
    echo form_input($price);
    echo form_submit($save);
    echo form_close();
    ?>

using this

$vegnames=array();
    foreach($veg_names as $r)
    {
    $vegnames[]=$r->label;
    //$r->value;
    }

i'm getting result like

<option value='0'>label1</option>
<option value='1'>label2</option>
<option value='2'>label3</option>

But i want

<option value='value1'>Label1</option>
<option value='value2'>label2</option>
<option value='value3'>label3</option>

how to solve this in codeigniter.

0

2 Answers 2

3

Well then you should use:

$vegnames=array();
foreach($veg_names as $r){
    $vegnames[$r->value]=$r->label;
}
Sign up to request clarification or add additional context in comments.

Comments

1

You need to have name value pairs of database rows.

For that you need to use a multi-dimensional array.

Every element should have a key (value from database) and value (label from database)

The select option will have value to be key and label to be value like this:

$vegnames=array();
foreach($veg_names as $r){
  $vegnames[$r->value]=$r->label;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.