MVC - edit form is not working - codeigniter

1

I have a form to print the data of a user. I want you to be able to edit this data. But it's giving me an error in the view:

  

Undefined variable: list and Invalid argument supplied for foreach ().

My code is as follows:

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

No Controller it enters here: form_validation->run())==FALSE; . It does not enter else .

    
asked by anonymous 12.06.2016 / 13:44

1 answer

0

It has several errors:

View:

You forgot to put the name ( name ) of inputs :

<?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" name="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" name="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; ?>

That is, name="nome" and name="telefone" are the information where PHP can retrieve the information typed in inputs .

Controller:

You have missed the telephone rule check, you forgot to rules in the variable of type array $config . given ) was added to the example below, but you can add the other rules, see example :

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',
            'rules' => 'required'),
        );

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

}

In the code you can have some more errors of required , check all problems correctly and start refactoring your code. These two tips will solve a good part, but, your code is without standard and it is difficult to solve all the problems. Remember that a well-written and organized code helps with maintenance.

Generally speaking, the part of sintaxe and% of_com% of information will work for your form .

    
13.06.2016 / 01:47