Problem with Vista in CodeIgniter

0

Hello everyone I have a problem that does not reflect in a view the data of another associated table, if I show the ID with the reference of the column but I want to give me the names, here is the model: It is a CRUD: The view has to add, edit and delete, when I edit if the data appear, but when I visualize them only the ids appear:

function update_asignacion($id,$params){
    $this->db->where('id',$id);
    return $this->db->update('asignacion',$params);
}

The index:

function index(){
    $data['asignacion'] = $this->Asignacion_model->get_all_asignacion();
    $data['_view'] = 'asignacion/index';
    $this->load->view('layouts/main',$data);
}

the edit view handler:

function edit($id){   
    // check if the asignacion exists before trying to edit it
    $data['asignacion'] = $this->Asignacion_model->get_asignacion($id);

    if(isset($data['asignacion']['id'])){
        $this->load->library('form_validation');
        $this->form_validation->set_rules('alumno_id','Alumno Id','required');
        $this->form_validation->set_rules('curso_id','Curso Id','required');

        if($this->form_validation->run())     {   
            $params = array(
                'alumno_id' => $this->input->post('alumno_id'),
                'curso_id' => $this->input->post('curso_id'),
            );

            $this->Asignacion_model->update_asignacion($id,$params);            
            redirect('asignacion/index');
        } else {
            $this->load->model('Alumno_model');
            $data['all_alumnos'] = $this->Alumno_model->get_all_alumnos();

            $this->load->model('Curso_model');
            $data['all_cursos'] = $this->Curso_model->get_all_cursos();

            $data['_view'] = 'asignacion/edit';
            $this->load->view('layouts/main',$data);
        }
    }else{
        show_error('The asignacion you are trying to edit does not exist.');
    }
} 

and the view (this is the view of the index):

<?php foreach($asignacion as $a){ ?>
    <tr>
        <td><?php echo $a['id']; ?></td>
        <td><?php echo $a['alumno_id']; ?></td>
        <td><?php echo $a['curso_id']; ?></td>
        <td>
            <a href="<?php echo site_url('asignacion/edit/'.$a['id']); ?>" class="btn btn-info btn-xs"><span class="fa fa-pencil"></span> Edit</a> 
            <a href="<?php echo site_url('asignacion/remove/'.$a['id']); ?>" class="btn btn-danger btn-xs"><span class="fa fa-trash"></span> Delete</a>
        </td>
    </tr>
<?php } ?>

this is the Edit view:

<div class="form-group">
    <select name="alumno_id" class="form-control">
        <option value="">select alumno</option>
        <?php foreach($all_alumnos as $alumno){
            $selected = ($alumno['id'] == $asignacion['alumno_id']) ? ' selected="selected"' : "";
            echo '<option value="'.$alumno['id'].'" '.$selected.'>'.$alumno['nombre'].'</option>';
        } ?>
    </select>
    <span class="text-danger"><?php echo form_error('alumno_id');?></span>
</div>

But I already try to place it in the same way in the index and I'm wrong. I would appreciate your help please. Greetings

Screenshots

    
asked by WilsonicX 08.05.2018 в 23:55
source

2 answers

1

The fact that the tablas are related does not mean that their attributes will come by themselves when calling their id . You can bring that data in the following way in your Modelo with the syntax of Codeigniter .

public function obtenerDatos(){
    $this->db->select('t.*,a.*,c.*');
    $this->db->from('tabla t');
    $this->db->join('alumno a', 't.alumno_id = a.id');
    $this->db->join('curso c', 't.curso_id = c.id');

    $aResult = $this->db->get();
    if(!$aResult->num_rows() == 1){
        return false;
    }

    return $aResult->result_array();
}

Basically you are bringing all the data from the first table, from the table alumno and the table curso and you are returning them in an array to the controller where you should see what data is returning to send them later in view, in this case I used join to get the data from the relations taking as a parameter that the alumno_id equals the id of the student table and the same way for the course.

If you want to see how it is used in Modelo , Vista and Controlador I invite you to visit the reference, it is a question that answers where I show everything in the simplest way possible and I tell you how make the request from controlador to modelo , receive a response and that response send it to vista .

Reference: Inner Join in Codeigniter

    
answered by 09.05.2018 / 18:01
source
0

Your problem is in the query since you bring all the information of the table in which the information is seen in the following way:

|----|-----------|----------|
| id | alumno_id | curso_id |
|----|-----------|----------|
|  1 |    1      |     1    |
|----|-----------|----------|

Being related does not mean that by default it will bring you the information from the other table. What you should do is a query relating the data in the tables. Your function get_all_asignacion of the model would look like this:

$queryBD = "SELECT a.id, h.nombre as alumno, c.nombre as curso "
           . "FROM asignacion a, alumno h, curso c "
           . "WHERE h.id_alumno = a.id_alumno AND c.id_curso = a.id_curso ";
$query = $this->db->query($queryBD);
return $query;

Assuming that the names of your tables are asignacion , alumno and curso .

    
answered by 09.05.2018 в 00:36