条件DropDown Codeigniter

时间:2017-08-01 01:42:30

标签: php mysql codeigniter dropdown

我有一个包含9个国家/地区位置行的数据库,每个数据库都有0和1值用于国家/地区启用和禁用服务,我想在下拉菜单中启用该启用的国家/地区(其中包含值1) 像这样

$options = array(

if(($row->s_loc_london)==1)
{
    'location_lond' => 'London',
}

form_dropdown('service_location', $options, $row->s_type );

2 个答案:

答案 0 :(得分:1)

请通过示例找到以下解决方案。

Codeigniter form_builder具有内置函数,可以从键/值对数组的动态数组生成下拉列表。

  

<强>语法

form_dropdown([$name = ''[, $options = array()[, $selected = array()[, $extra = '']]]]);
$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>
  

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

Would produce:

<select name="shirts" multiple="multiple">
        <option value="small" selected="selected">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>
  

您的解决方案

$options = array(
    'london' => 'London',
    'india' => 'India'
);

if ($row->s_loc_london == 1)
{
    $selected_location = 'london';
}

form_dropdown('service_location', $options, $selected_location );

如果不起作用,请告诉我。

答案 1 :(得分:1)

试试吧

<select name="outlet" class="form-control">
    <option value="">Select Outlet</option>
    <?php
    foreach ($getOutlet as $outlet)
    {
        $selected = '';
        if(!empty($this->input->get('outlet')))
        {
            if($outlet->id == $this->input->get('outlet'))
            {
                $selected = 'selected';
            }
        }
    ?>
    <option <?=$selected?> value="<?=$outlet->id?>"><?=$outlet->name?></option>
    <?php
    }
    ?>
</select>