2012-09-16 9 views
5

Así que tengo 3 tablas a las que deseo unirme.Codeigniter: une 3 tablas y muestra los datos a la vista

Estoy construyendo una aplicación i CodeIgniter y tengo 3 mesas

Cliente:
-id
-phone_number
-hospital_id
-smc_status
-testing_center_id

hospital
-id
-name

Testing_center
-id
-name

En el modelo, tengo esto:

public function get_clients() 
    { 
     if($slug === FALSE) 
     { 
      $this->db->select('clients.*'); 
      $this->db->from('clients'); 
      $this->db->join('hospital', 'clients.id = hospital.id'); 
      $this->db->join('testing_center', 'clients.id = testing_center.id'); 
      $query = $this->db->get(); 

      return $query->result_array(); 
     } 

     $query = $this->db->get_where('clients'); 
     return $query->row_array(); 
    } 

En la vista que tengo:

<tbody> 
    <?php foreach ($clients as $client_item): ?> 
    <tr> 
     <td><?php echo $client_item['phone_number'] ?></td> 
     <td><?php echo $client_item['smc_status'] ?></td> 
     <td><?php echo $client_item['hospital_id'] ?></td> //i wish to have the hospital name here 
     <td><?php echo $client_item['testing_center_id'] ?></td> //i wish to have the testing center name here 
     <td><?php echo $client_item['language'] ?></td> 
     <td><a href="#">View</a></td> 
    </tr> 
    <?php endforeach ?> 
</tbody> 

Pero eso es porque sí No he podido mostrar el nombre del hospital y el nombre del centro de pruebas en el tercer y cuarto td. ¿Cómo puedo hacer eso? Probé algunas técnicas que simplemente no parecían funcionar por alguna razón. Por favor avise

Respuesta

0

¿Qué ocurre si se intenta esto:

$this->db->join('hospital', 'hospital.id = clients.id'); 
$this->db->join('testing_center', 'testing_center.id = clients.id'); 

en lugar de esto:

$this->db->join('hospital', 'clients.id = hospital.id'); 
$this->db->join('testing_center', 'clients.id = testing_center.id'); 

También puedes ver

cliente * s * y el cliente si son los mismos en todas partes

Y también cambian como Ne rd propuesto: $ this-> db-> select ('clients. *'); a:

$this->db->select('*'); 
+0

he hecho todos los cambios necesarios en primer lugar las recomendaciones de NERD, así como la suya pero terminan recibiendo el error HTTP 500. ¿Por qué? – raybesiga

+0

Comprobado y he cometido algunos errores de ortografía. ¡Gracias por el consejo! – raybesiga

+0

Estoy tratando de completar un formulario y enviar los datos a la base de datos en las diferentes tablas. ¿Cómo voy a hacer eso? ¿Enlazo primero el formulario a la base de datos para que entienda las uniones o debo colocar texto de entrada similar a lo que se espera en la base de datos? – raybesiga

3

que sólo está seleccionando los valores de la tabla clients. Es necesario seleccionar las columnas de las otras mesas, así

$this->db->select('clients.id, 
    clients.phone_number, 
    clients.smc_status, 
    clients.language, 
    hospital.name AS hospital_name, 
    testing_center.name AS testing_center_name'); 

A continuación, se puede acceder a ellos por

<?php echo $client_item['hospital_name'] ?> 
<?php echo $client_item['testing_center_name'] ?> 

EDIT: También shouldn't use SELECT *, que clients.* está haciendo. Actualicé mi código

+0

He realizado los cambios pero termino obteniendo el HTTP Error 500. ¿Por qué? – raybesiga

+0

Hay una serie de cosas diferentes que podrían haber salido mal. Verifica tu registro de PHP. –

+0

estoy corriendo en mi localhost y no puedo comprobar cualquier registro, pero simplemente cambiado a: $ this-> db-> select ('clients.id, clients.phone_number, clients.smc_status, clients.language , nombre de hospital AS nombre_del hospital, \t \t \t \t testing_center.name AS test_center_name '); $ this-> db-> from ('clientes'); $ this-> db-> join ('hospital', 'hospital.id = clients.id'); $ this-> db-> join ('testing_center', 'testing_center.id = clients.id'); $ query = $ this-> db-> get(); return $ query-> result_array(); } $ query = $ this-> db-> get_where ('clients', array ('slug' => $ slug)); return $ query-> row_array(); } } – raybesiga

0

Se sholud ser así

$this->db->join('hospital', 'clients.hospital_id = hospital.id'); 
$this->db->join('testing_center', 'clients.testing_center_id = testing_center.id'); 
+0

Hay un error al unir los mismos nombres de columna en ambas tabulaciones como ("hospital_id en clientes, id en el hospital") y ("testing_center_id en clientes, id en testing_center") – Gautam3164