4

codeigniterのドロップダウン配列でset_select()(フォームヘルパーの一部)を使用するにはどうすればよいですか?

これは、ユーザーが選択したオプションを記憶するために使用されます。

$guide_options = array('investments' => 'Investments',
                       'wills' => 'Wills',
                       'tax-planning' => 'Tax planning',
                       'life-insurance' => 'Life insurance',
                       'currency-exchange' => 'Currency exchange',
                       'retirement-planning' => 'Retirement planning',
                       'international-healthcare' => 'International healthcare',
                       'savings-plans' => 'Savings plans',
                       'education-planning' => 'Education planning',
                       'sipps' => 'SIPPS',
                       'qrops' => 'QROPS',
                       'qnups' => 'QNUPS',
                       'mortgages' => 'Mortgages',
                       'other' => 'Other service'                       
                      );
echo form_dropdown('guide', $guide_options, 'investments');

ここにフォームヘルパーガイド:http://codeigniter.com/user_guide/helpers/form_helper.html

4

5 に答える 5

12

set_selectヘルパーではなくhtmlコードで作成する場合に使用できます。

あなたはこのようなことをすることができますが

$selected = ($this->input->post('guide')) ? $this->input->post('guide') : 'investments';                        
echo form_dropdown('guide', $guide_options, $selected);
于 2011-11-08T11:21:19.763 に答える
3

これを試して:

echo form_dropdown('guide', $guide_options,set_value('guide', 'investments'));

http://codeigniter.com/forums/viewthread/97665/

于 2011-11-08T11:19:40.117 に答える
1

from_dropdown()で選択したアイテムをすでに定義しているため、set_select()を使用する必要はありません。

form_dropdown('guide', $guide_options, 'investments');

理解を深めるには、以下のコードと出力を参照してください

$options = array(
                  'small'  => 'Small Shirt',
                  'med'    => 'Medium Shirt',
                  'large'   => 'Large Shirt',
                  'xlarge' => 'Extra Large Shirt',
                );

$shirts_on_sale = array('small', 'large');

echo form_dropdown('shirts', $options, 'large');

// Would produce:

<select name="shirts">
<option value="small">Small Shirt</option>
<option value="med">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>
</select>
于 2011-11-08T11:16:37.207 に答える
1

set_select()の実際の例を次に示します。

<?php echo set_select('my_select', $data['id'])?>

完全なコード:

<select class="form-control" required name="my_select">
    <option value="">- Select -</option>
    <?php foreach ($datas as $data): ?>
        <option value="<?php echo $data['id']?>" <?php echo set_select('my_select', $data['id'])?>><?php echo $data['name']?></option>
    <?php endforeach; ?>
</select>

または、純粋なPHPアプローチを使用できます。

// Use this inside in a <option> generated by a foreach, like the example above.
<?php echo $id_city == $city['id']?' selected ':''?>
于 2017-03-15T20:05:29.493 に答える
0

set_selectを使用するには、問題のフィールドの検証ルールが必要です。ここで同様の質問に対する私の答えを参照してください: form_dropdownのCodeIgniter select_value

于 2014-12-14T23:05:55.600 に答える