duanbei7005 2017-06-14 07:14
浏览 43
已采纳

无法填充数据库中的下拉列表

Cannot populate dropdown from database. View is throwing error "undefined variable $acciones". I'm trying to show dropdown list and save selected item into database but i don't know what am i doing wrong here.

View

<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>

Controller

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');
    }

View (Edit)

<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>

Controller (solved)

//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);
    }

Model (query to get acciones)

 public function get_listado_acciones() {

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

3条回答 默认 最新

  • dswu26846 2017-06-14 07:33
    关注

    first get data from your database for $acciones. and keep make variable in your controller $data['acciones'] = $acciones; // data you got from your database through model.

    than pass $data parameter with loading view as 2nd parameter.

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

    in this way your problem will be resolved.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度