Codeigniter - 表单助手 set_select()

发布于 2024-12-14 04:19:55 字数 1215 浏览 0 评论 0原文

如何在 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

How can I use set_select() (part of the form helper) on my dropdown array in codeigniter:

It is used to remember which option the user has selected.

$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');

Form helper guide here: http://codeigniter.com/user_guide/helpers/form_helper.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烦人精 2024-12-21 04:19:56

试试这个:

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

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

Try this:

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

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

分分钟 2024-12-21 04:19:56

您不必使用 set_select() 因为您已经在 from_dropdown() 中定义了所选项目

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>

You Don't have to Use set_select() as you are already defining selected item in from_dropdown()

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

For better understanding refer below code and output

$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>
愁杀 2024-12-21 04:19:56

这是 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 ':''?>

Here's a working example of set_select()

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

Full code:

<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>

Or you can use a pure PHP approach:

// Use this inside in a <option> generated by a foreach, like the example above.
<?php echo $id_city == $city['id']?' selected ':''?>
久伴你 2024-12-21 04:19:56

要使用 set_select,您需要有相关字段的验证规则。请参阅我对类似问题的回答:
Form_dropdown 中的 CodeIgniter select_value

To use set_select you need to have a validation rule for the field in question. See my answer to a similar question here:
CodeIgniter select_value in form_dropdown

兔姬 2024-12-21 04:19:55

如果您使用 html 代码而不是帮助程序生成,则可以使用 set_select

虽然你可以做这样的事情

$selected = ($this->input->post('guide')) ? $this->input->post('guide') : 'investments';                        
echo form_dropdown('guide', $guide_options, $selected);

set_select you can use if you produce with html code not with helper.

Although you can do something like this

$selected = ($this->input->post('guide')) ? $this->input->post('guide') : 'investments';                        
echo form_dropdown('guide', $guide_options, $selected);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文