2012-09-16 2 views
5

그래서 내가 가입하고 싶은 테이블이 3 개 있습니다.Codeigniter : 3 개의 테이블을 결합하여보기에 데이터 표시

나는 응용 프로그램 내가 CodeIgniter의 구축, 그리고 난 3 개 테이블

클라이언트가 있습니다
-id
-phone_number을
-hospital_id
-smc_status
-testing_center_id

병원
-id내가 가지고있는보기에서

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

: -name

Testing_center 모델에서
-id
-name

,이이

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

그러나 그것은 becau입니다. 세 번째와 네 번째 td에 병원 이름과 검사 센터 이름을 표시하지 않았습니다. 어떻게해야할까요? 어떤 이유로 작동하지 않는 몇 가지 기법을 시도했습니다. 이 대신

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

: 그들은 같은 경우

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

을 또한

클라이언트 * * 클라이언트 확인

당신이하려고하면 어떻게됩니까

답변

0

을 알려 주시기 바랍니다 어디서나

또한 Ne로 변경 제안 된 제안 : $ this-> db-> select ('clients. *'); 가로 :

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

처럼 sholud하지만 난 HTTP 오류 500 이유는 무엇입니까를 받고 결국? – raybesiga

+0

선택했는데 일부 맞춤법 오류가 발생했습니다. 조언 해주셔서 감사합니다! – raybesiga

+0

양식을 채우고 데이터를 다시 다른 테이블로 푸시하려고합니다. 나는 어떻게 그것에 대해 갈 것인가? 먼저 데이터베이스에 양식을 바인딩하여 조인을 이해하거나 데이터베이스에서 예상되는 것과 비슷한 입력 텍스트를 배치해야합니까? – raybesiga

3

에서만 clients 테이블에서 값을 선택하고 있습니다. 당신은

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

는 그런 다음

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

편집에 의해 액세스 할 수 있습니다뿐만 아니라 다른 테이블에서 열을 선택해야합니다 또한 clients.*가하고있는 당신 shouldn't use SELECT *,있다. 내 코드를 업데이트했습니다.

+0

변경을했지만 HTTP 오류 500이 발생합니다. 이유가 무엇입니까? – raybesiga

+0

잘못되었을 수있는 여러 가지가 있습니다. PHP 로그를 확인하십시오. –

+0

localhost에서 실행 중이며 로그를 확인할 수 없지만 단순히 다음으로 변경했습니다. $ this-> db-> select ('clients.id, clients.phone_number, clients.smc_status, clients.language , hospital.name AS 병원 이름, \t \t \t \t testing_center.name AS testing_center_name '); $ this-> db-> from ('clients'); $ 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

그것은뿐만 아니라 당신 같은, 내가 먼저 얼간이하여 권장 사항을 필요한 모든 변경 한이

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

("병원의 병원 ID", "병원의 ID") 및 ("클라이언트의 testing_center_id, testing_center의 ID")와 같은 두 테이블의 동일한 열 이름을 결합하는 데는 문제가 있습니다 – Gautam3164

관련 문제