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