1
I have a form printing a user’s data. I want the user to be able to edit that data. But it is giving me an error in the view:
Undefined variable: list e Invalid argument supplied for foreach().
My code is this::
Controller:
public function actualizar_perfil()
{
$this->load->helper('url');
//definição da lista de regras a aplicar
$config=array(
array(
'field' => 'nome',
'label' => 'nome',
'rules' => 'required|min_length[0]|max_length[50]'
),
array(
'field' => 'telefone',
'label' => 'telefone',
),
);
$this->load->helper('url');
$this->load->library('form_validation');
$this->form_validation->set_rules($config);
if(($this->form_validation->run())==FALSE)
{
$id = $this->uri->segment(3,0);
$this->load-> model('perfil_model');
$data['username'] = $this->session->userdata('username');
$data['login'] = $this->perfil_model->GetDisplayableByUsernameAP($this->input->post('id'));
$this->load->library('form_validation');
$this->load->view('perfil_view',$data);
}
else {
$this->load->helper('url');
$this->load->model('perfil_model');
$this->perfil_model->actualizarperfil($this->input->post('id'));
redirect('controller/perfil');
}
}
Model:
function actualizarperfil($id)
{ $username = $this->session->userdata('username');
$data['nome'] = $this->input->post('nome');
$data['telefone'] = $this->input->post('telefone');
$this->db->where('id_login', $id);
$this->db->update('login', $data);
}
function GetDisplayableByUsernameAP($id)
{
$this->db->select('id_login, nome, username, password, cod_postal, telefone, email, localidade, rua');
$result = $this->db->get_where('login',array('id_login' => $id));
return $result->row();
}
View:
<?php echo validation_errors() ?>
<?php echo form_open('controller/actualizar_perfil') ?>
<?php foreach ($list as $login): ?>
<div class="form-group">
<label class="col-lg-3 control-label">Nome:</label>
<div class="col-lg-8">
<input class="form-control" id="nome" value="<?php echo $login->nome?>" type="text">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Telefone:</label>
<div class="col-lg-8">
<input class="form-control" id="telefone" value="<?php echo $login->telefone?>" type="text">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<a href="actualizar_perfil/<?php echo $login->id_login?> " style="margin-left: 10px; padding:10px;">Guardar</a>
<span></span>
<input class="btn btn-default" value="Cancelar" type="reset">
</div>
</div>
<?php endforeach ?>
In the Controller
he comes in here: form_validation->run())==FALSE;
. Does not enter the else
.