无法从数据库中删除下拉列表

时间:2017-06-14 07:14:41

标签: php codeigniter

无法填充数据库中的下拉列表。视图抛出错误"未定义变量$ acciones"。我试图显示下拉列表并将所选项目保存到数据库中,但我不知道我在这里做错了什么。

查看

<div class="form-group">
                        <label class="col-md-4 control-label" for="acciones">Acciones</label>  
                        <div class="col-md-4">
                            <?php foreach ($acciones as $accion) { ?>
                                   <!-- <input id="acciones" name="acciones" placeholder="P.ej: cargar" class="form-control input-md" required="" type="text"> -->
                            <label class="col-md-4 control-label" for="acciones">-Seleccione acción-</label>    
                            <select id="acciones" name="acciones">
                                    <option value="<?php echo $accion->idAcciones; ?>" <?php echo set_select('acciones', $accion->idAcciones); ?>>
                                        <?php echo $class->nombreAccion; ?>
                                    </option>
                                </select>
                            <?php } ?>
                            <span class="help-block"> </span>  
                        </div>
                    </div>

控制器

public function profile() {


        if ($_SESSION['user_logged'] == FALSE) {
            $this->session->set_flashdata("error", "Por favor, regístrese para poder ver esta página.");
            redirect("auth/login");
        }

        if (isset($_POST['register'])) {
            //$data['city_list'] = $this->City_model->get_dropdown_list();
            $this->form_validation->set_rules('datepicker', 'Fecha', 'required|trim');
            $this->form_validation->set_rules('acciones', 'Acciones', 'required');
            $this->form_validation->set_rules('numeroentrega', 'Numero entrega', 'required|trim');
            $this->form_validation->set_rules('cliente', 'Cliente', 'required');
            $this->form_validation->set_rules('origen', 'Orígen', 'required');
            $this->form_validation->set_rules('destino', 'Destino', 'required');


            // if form validation true
            if ($this->form_validation->run() == TRUE) {

                $data1 = array('cita' => $_POST['datepicker']);

                $data = array(
                    'idAcciones' => $_POST['acciones'],
                    'numeroEntrega' => md5($_POST['numeroentrega']),
                    'cliente' => $_POST['cliente'],
                    'origen' => $_POST['origen'],
                    'destino' => md5($_POST['destino'])
                );
                $this->db->insert('citas', $data1);
                $this->db->insert('entregas', $data);

                $this->session->set_flashdata("success", "¡Sus datos han sido guardados exitosamente! "
                        . "Su cita está siendo procesada. Por favor, revise su email con frecuencia. "
                        . "Recibirá la confirmación a la mayor brevedad posible. ");
                redirect("user/profile", "refresh");
            }
        }


        $this->load->view('profile');
    }

查看(编辑)

<div class="col-md-4">

        <!-- <input id="acciones" name="acciones" placeholder="P.ej: cargar" class="form-control input-md" required="" type="text"> -->
                            <label class="col-md-4 control-label" for="acciones">-Seleccione acción-</label>    
                            <select id="acciones" name="acciones">
                                <?php
                                if (isset($acciones) && $acciones != array()) {
                                    foreach ($acciones as $accion) {
                                        ?>
                                        <option value="<?php echo $accion->idAcciones; ?>" <?php echo set_select('acciones', $accion->idAcciones); ?>>
                                        <?php echo $class->nombreAccion; ?>
                                        </option>
    <?php }
} ?>
                            </select>

                            <span class="help-block"> </span>  
                        </div>

控制器(已解决)

//Le pasamos la lista a la vista para poder 
        $query = $this->User_model->get_listado_acciones();
        $item['acciones'] = $query;



        if (isset($_POST['register'])) {

       [...]


        $this->load->view('profile',$item);
    }

模型(查询以获取被控者)

 public function get_listado_acciones() {

        $query = $this->db->get('acciones');
        return $query->result();
    }

3 个答案:

答案 0 :(得分:1)

首先,您必须获得acciones记录然后填充下拉列表。 我认为你在生成下拉列表时犯了错误。 Foreach在选择标记

之后执行
 <!-- <input id="acciones" name="acciones" placeholder="P.ej: cargar" class="form-control input-md" required="" type="text"> -->
                                <label class="col-md-4 control-label" for="acciones">-Seleccione acción-</label>    
                                <select id="acciones" name="acciones">
                            <?php
             if(isset($acciones) && $acciones!=array()){
              foreach ($acciones as $accion) { ?>
                                        <option value="<?php echo $accion->idAcciones; ?>" <?php echo set_select('acciones', $accion->idAcciones); ?>>

                                            <?php echo $class->nombreAccion; ?>
                                        </option>
                          <?php }} ?>
                                    </select>

答案 1 :(得分:1)

你可能错过了两件事。

来自您的控制器:

1.Forgot将数据从控制器传递到您的视图:

    $data['acciones'] = array(); // your data here
    $this->load->view('profile' , $data); //

2.只有在点击注册button时才会查看您的数据:

 $data['acciones'] = array();
 if (isset($_POST['register'])) {
   echo 'test'; //try to echo here
   $data['acciones'] = array('has entry');
 }
 $this->load->view('profile' , $data); //

答案 2 :(得分:1)

首先从您的数据库中获取$ acciones的数据。并在控制器中保留变量$ data [&#39; acciones&#39;] = $ acciones; //通过模型从数据库获得的数据。

将带有加载视图的$ data参数作为第二个参数传递。

$ this-&gt; load-&gt; view(&#39; profile&#39;,$ data);

以这种方式你的问题将得到解决。