如果在ci中提交时选择了select选项,如何从数据库中提取选项值?

时间:2015-02-16 08:31:23

标签: php codeigniter

如果在ci中提交选中,我想设置选择选项,从数据库中提取选项值? 我试过了,但它没有用! 有谁能告诉我解决方案?

这是我的控制器

 public function add(){

    $this->pageAction = 'Add';

    $error = array ();

    $finalData = array ();

    if($this->input->post('doAct') == strtolower($this->pageAction)){



        $this->form_validation->set_rules('country', 'Country Name', 'required');
        $this->form_validation->set_rules('city', 'City Name', 'required');
        $this->form_validation->set_rules('status', 'Status', 'required');

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

            $finalData['error'] = "error";
        }else{

            $data = array (
             'c_id'=>$this->input->post('country'),
             'name'=>$this->input->post('city'),
             'active'=>$this->input->post('status')
            );



            if($this->main_manager->insert($data, "city") == false){

                $finalData['error'] = 'Cannot save data. Please try again.';
            }else{

                $this->session->set_flashdata('msg', 'The data has been added successfully');

                redirect(SITE_ADMIN_URL . "city_admin/index");
            }
        }
    }
    $finalData['country'] = $this->main_manager->select_all('country');
    $this->load->view('administrator/header');

    $this->load->view('administrator/add_city', $finalData);

    $this->load->view('administrator/footer');
}

这是我的观点

<form class="form-horizontal" name="" id="" enctype="multipart/form-data" method="post" action="<?php echo SITE_ADMIN_URL;?>city_admin/add"/>

                    <div class="control-group">

                        <label class="control-label" for="form-field-1">Country Name</label>
                        <div class="controls">
                            <select name="country">
                                <option value="">Please Select Country</option>
                                <?php
                                for($a = 0; $a < count($country); $a++){
                                    ?>
                                    <option value="<?php echo $country[$a]['id'];?>"><?php echo $country[$a]['name'];?></option>
                                    <?php
                                }
                                $select = "";
                                if($country == "selected"){
                                    $country = $select;
                                    ?>
                                    <option value="<?php echo $select[$a]['id'];?>"><?php echo $select[$a]['name'];?></option>
                                <?php }
                                ?>
                            </select>
                            <!--<input type="text" id="reason" name="country" value="<?php // echo set_value('country');  ?>" />-->

                        </div>

                    </div>

                    <div class="control-group">

                        <label class="control-label" for="form-field-1">City</label>

                        <div class="controls">

                            <input type="text" id="reason" name="city" value="<?php echo set_value('city');?>" />

                        </div>

                    </div>
                    <div class="control-group">

                        <label class="control-label" for="form-field-1">Status</label>

                        <div class="controls">


                            <input type="radio" name="status" id="status" value="1" <?php echo set_radio('status', '1');?>>

                            <span class="lbl">&nbsp;Active</span>

                            <input type="radio" name="status" id="status" value="0" <?php echo set_radio('status', '0');?>>

                            <span class="lbl">&nbsp;Inactive</span>

                        </div>

                    </div>

                    <div class="space-4"></div>

                    <div class="form-actions">

                        <input type="hidden" name="doAct" value="add">

                        <button class="btn btn-info" type="submit" name="submit">

                            <i class="icon-ok bigger-110"></i>

                            Submit

                        </button>

                        &nbsp; &nbsp; &nbsp;

                        <button class="btn" type="reset">

                            <i class="icon-undo bigger-110"></i>

                            Reset

                        </button>

                    </div>

                    </form>

1 个答案:

答案 0 :(得分:0)

在您的情况下,我没有看到您在何处定义默认值,因为您只将$country数组发送到您的视图。 例如,我们假设您的视图中有一个名为$default_value的变量。

如果您想在不使用form_helper的情况下继续构建表单:

<select name="country">
    <option value="">Please Select Country</option>
    <?php foreach ($country as $c): ?>
        <option value="<?php echo $c->id ?>" <?php echo ($c->id == $default_value)?'selected="selected"':''?>><?php echo $c->name?></option>    
    <?php endforeach ?>
</select>

但您也可以使用form_helper:

1 /在你的控制器中准备一个将填充你的选择

的数组
$data["countryArray"] = array(
              '-1'    => 'Please Select Country',
              '1'  => 'Australia',
              '2'  => 'France',
              '3'  => 'Belgium',
              '4' => 'Brasil',
            );

然后在你看来:

echo form_dropdown('country', $countryArray, set_value($default_value', '-1'));
//First param : name attribute | second param : array containing <options> | third param : default value