带有foreach循环的form_dropdown

时间:2015-05-28 12:43:56

标签: php codeigniter

我正在尝试使用codeigniter form_dropdown在我的选项中填充foreach。

问题:目前,在查看视图时,它不会围绕选项包装<select></select>。是否可以使用带有 codeigniter form_dropdown()

的foreach循环
<?php

  $select = array(
  'class' => 'form-control'
  ); 

  foreach ($categories as $category) { 

    //$option = array(
    //  $category['category_id'] => $category['category_name']
    //);

    $option[$category['category_id']] = $category['category_name'];
  }

  echo form_dropdown('parent_id', $option, '', $select);
?>

<?php
  echo form_error('parent_id', '<div class="text-danger" style="padding-top: 1rem;">', '</div>');
?>

更新:输出视图

未包装 <select> </select> 围绕选项

<div class="form-group">
<label class="col-sm-2">Category Parent</label>
<div class="col-sm-10">
<p class="text-info">If No Category Selected. When Save Form Category Will Be Saved As A Parent Category.</p>
<select class="form-control" name="parent_id"> </select>
<option value="164">Codeigniter > Model</option>
<option value="163">Codeigniter > Controllers</option>
<option value="162">Codeigniter</option>
<option value="165">Codeigniter > View</option>
<option value="166">PHP</option>
<option value="167">PHP > PHP for beginners</option>
</div>
</div>

的print_r

Array ( [0] => Array ( [category_id] => 164 [category_name] => Model [category_description] => [category_meta_title] => [category_meta_description] => [category_meta_keywords] => [image] => [parent_id] => 162 [top] => 1 [column] => 1 [sort_order] => 0 [status] => 1 [date_added] => 2015-05-17 15:38:05 [date_modified] => 0000-00-00 00:00:00 ) [1] => Array ( [category_id] => 163 [category_name] => Controllers [category_description] => [category_meta_title] => [category_meta_description] => [category_meta_keywords] => [image] => [parent_id] => 162 [top] => 1 [column] => 1 [sort_order] => 0 [status] => 1 [date_added] => 2015-05-17 00:41:38 [date_modified] => 0000-00-00 00:00:00 ) [2] => Array ( [category_id] => 162 [category_name] => Codeigniter [category_description] => CodeIgniter is a powerful PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications. [category_meta_title] => [category_meta_description] => [category_meta_keywords] => [image] => [parent_id] => 0 [top] => 1 [column] => 1 [sort_order] => 0 [status] => 1 [date_added] => 2015-05-17 00:38:55 [date_modified] => 0000-00-00 00:00:00 ) [3] => Array ( [category_id] => 165 [category_name] => View [category_description] => Loading views in codeigniter [category_meta_title] => Codeigniter Views [category_meta_description] => How to load codeigniter views [category_meta_keywords] => codeigniter, views [image] => [parent_id] => 162 [top] => 0 [column] => 0 [sort_order] => 0 [status] => 0 [date_added] => 0000-00-00 00:00:00 [date_modified] => 0000-00-00 00:00:00 ) [4] => Array ( [category_id] => 166 [category_name] => PHP [category_description] => [category_meta_title] => [category_meta_description] => [category_meta_keywords] => [image] => [parent_id] => 0 [top] => 0 [column] => 0 [sort_order] => 0 [status] => 0 [date_added] => 0000-00-00 00:00:00 [date_modified] => 0000-00-00 00:00:00 ) [5] => Array ( [category_id] => 167 [category_name] => PHP for beginners [category_description] => [category_meta_title] => [category_meta_description] => [category_meta_keywords] => [image] => [parent_id] => 166 [top] => 1 [column] => 0 [sort_order] => 0 [status] => 0 [date_added] => 0000-00-00 00:00:00 [date_modified] => 0000-00-00 00:00:00 ) ) 

控制器功能

public function index() {
    $data['title'] = "Category Add";

    $data['categories'] = array();

    print_r($this->get_categories());

    $category_results = $this->get_categories();

    foreach ($category_results as $category_result) {
        $data['categories'][] = array(
            'category_id' => $category_result['category_id'],
            'parent_id' => $category_result['parent_id'],
            'category_name' => ($category_result['parent_id'] ? $this->get_cagetory_parent_name($category_result['parent_id']) .' &gt; '. $category_result['category_name'] : $category_result['category_name'])
        );
    }

    $this->form_validation->set_rules('category_name', 'Category Name', 'trim|required|is_unique[category.category_name]');
    $this->form_validation->set_rules('category_meta_title', 'Category Meta Keywords', 'trim');
    $this->form_validation->set_rules('category_meta_description', 'Category Meta Description', 'trim');            
    $this->form_validation->set_rules('parent_id', 'Category Parent', 'trim|required'); 
    $this->form_validation->set_rules('top', 'Category Menu Top');
    $this->form_validation->set_rules('column', 'Number Of Columns In Menu', 'required|numeric');   

    $this->form_validation->set_message('is_unique', 'The %s' .' '. '<b>'. $this->input->post('category_name') .'</b>' .' '. 'is already taken');

    if ($this->form_validation->run() == FALSE) {

        $this->load->view('template/category/category_add_view', $data);

    } else {

        $this->add_category($this->input->post());

        redirect('admin/category/category_list');

    }
}

1 个答案:

答案 0 :(得分:1)

你真的没有必要 - form_dropdown()期望一个关联的选项数组作为它的第二个参数,所以转而专注于准备这样一个数组。

$options = array_combine(
    array_column($categories, 'category_id'),
    array_column($categories, 'category_name')
);
form_dropdown('field_name', $options);