代码之家  ›  专栏  ›  技术社区  ›  Dan Piers

提交后未显示form_dropdown中的选定值

  •  2
  • Dan Piers  · 技术社区  · 13 年前

    我正在使用form_dropdown创建一个下拉框。它的工作原理是,所有的值都被正确地列出,并且当我提交一个选定的选项时,可以实现所需的类别过滤。然而,在结果视图中,下拉列表显示“按类别筛选”,而我希望它显示$selected_Category,这是一个字符串。

    这是代码:

    echo form_dropdown('company_category_ids', array(0 => ' Filter by Category ') + $unique_category_ids, $selected_category, ' id="category"');?>
    

    感谢您的帮助或建议!

    编辑-添加变量的内容:

    $unique_category_ids:

        Array( [58] => Coffee/Tea Accessories [179] => Food Storage [247] => Outdoor Trash)
    

    $selected类别可以是这三种物品中的任何一种(咖啡/茶配件、食品储藏室或户外垃圾)。

    3 回复  |  直到 12 年前
        1
  •  2
  •   Alex    13 年前

    我认为数组中可能存在问题。我认为应该是。。。

    $unique_category_ids = array("value_1" => "Filter by Size", "value_2" => "Filter by Color");
    $selected_category = "value_2"; //just for example
    echo form_dropdown('company_category_ids', array("value_3" => ' Filter by Category ') + $unique_category_ids, $selected_category, ' id="category"');
    
        2
  •  0
  •   CodeGodie    13 年前

    根据我在CI方面的经验,问题似乎出在格式上。这就是你所拥有的:

    echo form_dropdown(
                'company_category_ids',  <----GOOD (this is the name of the Select tag)
                array(0 => ' Filter by Category '), <----WRONG this array should be your options
                $unique_category_ids, <---GOOD options
                $selected_category, <--GOOD
                'id="category"'); <---Good
    

    问题似乎是你有太多的参数。这最多需要4个参数。您的代码应该是这样的:

    $unique_category_ids = array(0 => ' Filter by Category ');
    $selected_category = '0';
    echo form_dropdown(
                'company_category_ids',
                $unique_category_ids, 
                $selected_category,
                'id="category"');
    
        3
  •  0
  •   Dan Piers    12 年前

    @谢谢你的评论。我一开始应该发布更完整的信息。 $selected_category 是一个文本字符串,由 $selected_category_id .

    $selected_category=$unique_category_ids[$selected_category_id];
    

    哪里 $selected_category_id 是58、179或247。问题是我在用 $选择类别 (咖啡/茶配件、食品储藏室或户外垃圾)而不是 $selected_category_id .

    推荐文章